ordered gitignore

pull/23/head
cupcakearmy 5 years ago
parent 2fd9e2dd22
commit e80db74af4

13
.gitignore vendored

@ -1,10 +1,19 @@
# Editors
.idea
# Node
node_modules/ node_modules/
package-lock.json package-lock.json
.idea
yarn.lock yarn.lock
# Build & Runtime
bin bin
lib lib
data data
restore
docker
Dockerfile
.autorestic.yml # Config
.autorestic.yml
.docker.yml

@ -25,7 +25,7 @@ export const { _: commands, ...flags } = minimist(process.argv.slice(2), {
string: ['l', 'b'], string: ['l', 'b'],
}) })
export const VERSION = '0.12' export const VERSION = '0.13'
export const INSTALL_DIR = '/usr/local/bin' export const INSTALL_DIR = '/usr/local/bin'
export const VERBOSE = flags.verbose export const VERBOSE = flags.verbose

@ -2,7 +2,7 @@ import { Writer } from 'clitastic'
import { config, VERBOSE } from './autorestic' import { config, VERBOSE } from './autorestic'
import { Backend, Backends, Locations } from './types' import { Backend, Backends, Locations } from './types'
import { exec, ConfigError } from './utils' import { exec, ConfigError, pathRelativeToConfigFile } from './utils'
@ -11,7 +11,7 @@ const ALREADY_EXISTS = /(?=.*already)(?=.*config).*/
export const getPathFromBackend = (backend: Backend): string => { export const getPathFromBackend = (backend: Backend): string => {
switch (backend.type) { switch (backend.type) {
case 'local': case 'local':
return backend.path return pathRelativeToConfigFile(backend.path)
case 'b2': case 'b2':
case 'azure': case 'azure':
case 'gs': case 'gs':

@ -1,8 +1,10 @@
import { Writer } from 'clitastic' import { Writer } from 'clitastic'
import { mkdirSync } from 'fs'
import { config, VERBOSE } from './autorestic' import { config, VERBOSE } from './autorestic'
import { getEnvFromBackend } from './backend' import { getEnvFromBackend } from './backend'
import { Locations, Location } from './types' import { LocationFromPrefixes } from './config'
import { Locations, Location, Backend } from './types'
import { import {
exec, exec,
ConfigError, ConfigError,
@ -10,27 +12,67 @@ import {
getFlagsFromLocation, getFlagsFromLocation,
makeArrayIfIsNot, makeArrayIfIsNot,
execPlain, execPlain,
MeasureDuration, fill, MeasureDuration,
fill,
decodeLocationFromPrefix,
hash, checkIfDockerVolumeExistsOrFail,
} from './utils' } from './utils'
export const backupFromFilesystem = (from: string, location: Location, backend: Backend, tags?: string[]) => {
const path = pathRelativeToConfigFile(from)
const cmd = exec(
'restic',
['backup', '.', ...getFlagsFromLocation(location, 'backup')],
{ env: getEnvFromBackend(backend), cwd: path },
)
if (VERBOSE) console.log(cmd.out, cmd.err)
}
export const backupFromVolume = (volume: string, location: Location, backend: Backend) => {
const tmp = pathRelativeToConfigFile(hash(volume))
try {
mkdirSync(tmp)
checkIfDockerVolumeExistsOrFail(volume)
// For incremental backups. Unfortunately due to how the docker mounts work the permissions get lost.
// execPlain(`docker run --rm -v ${volume}:/data -v ${tmp}:/backup alpine cp -aT /data /backup`)
execPlain(`docker run --rm -v ${volume}:/data -v ${tmp}:/backup alpine tar cf /backup/archive.tar -C /data .`)
backupFromFilesystem(tmp, location, backend)
} finally {
execPlain(`rm -rf ${tmp}`)
}
}
export const backupSingle = (name: string, to: string, location: Location) => { export const backupSingle = (name: string, to: string, location: Location) => {
if (!config) throw ConfigError if (!config) throw ConfigError
const delta = new MeasureDuration() const delta = new MeasureDuration()
const writer = new Writer(name + to.blue + ' : ' + 'Backing up... ⏳') const writer = new Writer(name + to.blue + ' : ' + 'Backing up... ⏳')
const backend = config.backends[to] try {
const path = pathRelativeToConfigFile(location.from) const backend = config.backends[to]
const [type, value] = decodeLocationFromPrefix(location.from)
const cmd = exec( switch (type) {
'restic',
['backup', path, ...getFlagsFromLocation(location, 'backup')],
{ env: getEnvFromBackend(backend) },
)
if (VERBOSE) console.log(cmd.out, cmd.err) case LocationFromPrefixes.Filesystem:
writer.done(`${name}${to.blue} : ${'Done ✓'.green} (${delta.finished(true)})`) backupFromFilesystem(value, location, backend)
break
case LocationFromPrefixes.DockerVolume:
backupFromVolume(value, location, backend)
break
}
writer.done(`${name}${to.blue} : ${'Done ✓'.green} (${delta.finished(true)})`)
} catch (e) {
writer.done(`${name}${to.blue} : ${'Failed!'.red} (${delta.finished(true)}) ${e.message}`)
}
} }
export const backupLocation = (name: string, location: Location) => { export const backupLocation = (name: string, location: Location) => {
@ -40,8 +82,8 @@ export const backupLocation = (name: string, location: Location) => {
if (location.hooks && location.hooks.before) if (location.hooks && location.hooks.before)
for (const command of makeArrayIfIsNot(location.hooks.before)) { for (const command of makeArrayIfIsNot(location.hooks.before)) {
const cmd = execPlain(command) const cmd = execPlain(command, {})
if (cmd) console.log(cmd.out, cmd.err) console.log(cmd.out, cmd.err)
} }
for (const t of makeArrayIfIsNot(location.to)) { for (const t of makeArrayIfIsNot(location.to)) {
@ -52,7 +94,7 @@ export const backupLocation = (name: string, location: Location) => {
if (location.hooks && location.hooks.after) if (location.hooks && location.hooks.after)
for (const command of makeArrayIfIsNot(location.hooks.after)) { for (const command of makeArrayIfIsNot(location.hooks.after)) {
const cmd = execPlain(command) const cmd = execPlain(command)
if (cmd) console.log(cmd.out, cmd.err) console.log(cmd.out, cmd.err)
} }
} }

@ -10,6 +10,12 @@ import { makeArrayIfIsNot, makeObjectKeysLowercase, rand } from './utils'
export enum LocationFromPrefixes {
Filesystem,
DockerVolume
}
export const normalizeAndCheckBackends = (config: Config) => { export const normalizeAndCheckBackends = (config: Config) => {
config.backends = makeObjectKeysLowercase(config.backends) config.backends = makeObjectKeysLowercase(config.backends)

@ -10,6 +10,7 @@ import { checkAndConfigureBackends, getBackendsFromLocations, getEnvFromBackend
import { backupAll } from './backup' import { backupAll } from './backup'
import { forgetAll } from './forget' import { forgetAll } from './forget'
import showAll from './info' import showAll from './info'
import { restoreSingle } from './restore'
import { Backends, Flags, Locations } from './types' import { Backends, Flags, Locations } from './types'
import { import {
checkIfCommandIsAvailable, checkIfCommandIsAvailable,
@ -86,36 +87,12 @@ const handlers: Handlers = {
if (!config) throw ConfigError if (!config) throw ConfigError
checkIfResticIsAvailable() checkIfResticIsAvailable()
if (!flags.to) {
console.log(`You need to specify the restore path with --to`.red)
return
}
const locations = parseLocations(flags) const locations = parseLocations(flags)
for (const [name, location] of Object.entries(locations)) { const keys = Object.keys(locations)
const baseText = name.green + '\t\t' if (keys.length < 1) throw new Error(`You need to specify the location to restore with --location`.red)
const w = new Writer(baseText + `Starting...`) if (keys.length > 2) throw new Error(`Only one location is supported at a time when restoring`.red)
let backend: string = Array.isArray(location.to) ? location.to[0] : location.to
if (flags.from) {
if (!location.to.includes(flags.from)) {
w.done(baseText + `Backend ${flags.from} is not a valid location for ${name}`.red)
continue
}
backend = flags.from
w.replaceLn(baseText + `Restoring from ${backend.blue}...`)
} else if (Array.isArray(location.to) && location.to.length > 1) {
w.replaceLn(baseText + `Restoring from ${backend.blue}...\tTo select a specific backend pass the ${'--from'.blue} flag`)
}
const env = getEnvFromBackend(config.backends[backend])
exec( restoreSingle(keys[0], flags.from, flags.to)
'restic',
['restore', 'latest', '--path', resolve(location.from), '--target', flags.to],
{ env },
)
w.done(name.green + '\t\tDone 🎉')
}
}, },
forget(args, flags) { forget(args, flags) {
if (!config) throw ConfigError if (!config) throw ConfigError
@ -140,7 +117,7 @@ const handlers: Handlers = {
console.log(out, err) console.log(out, err)
} }
}, },
async info() { info() {
showAll() showAll()
}, },
async install() { async install() {
@ -224,7 +201,7 @@ const handlers: Handlers = {
w.replaceLn('Downloading binary... 🌎') w.replaceLn('Downloading binary... 🌎')
await downloadFile(dl.browser_download_url, to) await downloadFile(dl.browser_download_url, to)
exec('chmod', ['+x', to]) chmodSync(to, 0o755)
} }
w.done('All up to date! 🚀') w.done('All up to date! 🚀')

@ -0,0 +1,82 @@
import { Writer } from 'clitastic'
import { resolve } from 'path'
import { config } from './autorestic'
import { getEnvFromBackend } from './backend'
import { LocationFromPrefixes } from './config'
import { Backend } from './types'
import {
checkIfDockerVolumeExistsOrFail,
ConfigError,
decodeLocationFromPrefix,
exec, execPlain,
hash,
pathRelativeToConfigFile,
} from './utils'
export const restoreToFilesystem = (from: string, to: string, backend: Backend) => {
exec(
'restic',
['restore', 'latest', '--path', resolve(from), '--target', to],
{ env: getEnvFromBackend(backend) },
)
}
export const restoreToVolume = (volume: string, backend: Backend) => {
const tmp = pathRelativeToConfigFile(hash(volume))
try {
restoreToFilesystem(tmp, tmp, backend)
try {
checkIfDockerVolumeExistsOrFail(volume)
} catch {
execPlain(`docker volume create ${volume}`)
}
// For incremental backups. Unfortunately due to how the docker mounts work the permissions get lost.
// execPlain(`docker run --rm -v ${volume}:/data -v ${tmp}:/backup alpine cp -aT /backup /data`)
execPlain(`docker run --rm -v ${volume}:/data -v ${tmp}:/backup alpine tar xf /backup/archive.tar -C /data`)
} finally {
execPlain(`rm -rf ${tmp}`)
}
}
export const restoreSingle = (locationName: string, from: string, to?: string) => {
if (!config) throw ConfigError
const location = config.locations[locationName]
const baseText = locationName.green + '\t\t'
const w = new Writer(baseText + `Starting...`)
let backendName: string = Array.isArray(location.to) ? location.to[0] : location.to
if (from) {
if (!location.to.includes(from)) {
w.done(baseText + `Backend ${from} is not a valid location for ${locationName}`.red)
return
}
backendName = from
w.replaceLn(baseText + `Restoring from ${backendName.blue}...`)
} else if (Array.isArray(location.to) && location.to.length > 1) {
w.replaceLn(baseText + `Restoring from ${backendName.blue}...\tTo select a specific backend pass the ${'--from'.blue} flag`)
}
const backend = config.backends[backendName]
const [type, value] = decodeLocationFromPrefix(location.from)
switch (type) {
case LocationFromPrefixes.Filesystem:
if (!to) throw new Error(`You need to specify the restore path with --to`.red)
restoreToFilesystem(value, to, backend)
break
case LocationFromPrefixes.DockerVolume:
restoreToVolume(value, backend)
break
}
w.done(locationName.green + '\t\tDone 🎉')
}

@ -1,23 +1,18 @@
import axios from 'axios'
import { spawnSync, SpawnSyncOptions } from 'child_process' import { spawnSync, SpawnSyncOptions } from 'child_process'
import { randomBytes } from 'crypto' import { createHash, randomBytes } from 'crypto'
import { createWriteStream, unlinkSync, renameSync } from 'fs' import { createWriteStream, renameSync, unlinkSync } from 'fs'
import { dirname, isAbsolute, join, resolve } from 'path'
import { homedir, tmpdir } from 'os' import { homedir, tmpdir } from 'os'
import { dirname, isAbsolute, join, resolve } from 'path'
import axios from 'axios'
import { Duration, Humanizer } from 'uhrwerk' import { Duration, Humanizer } from 'uhrwerk'
import { CONFIG_FILE } from './config' import { CONFIG_FILE, LocationFromPrefixes } from './config'
import { Location } from './types' import { Location } from './types'
export const exec = ( export const exec = (command: string, args: string[], { env, ...rest }: SpawnSyncOptions = {}) => {
command: string, const { stdout, stderr, status } = spawnSync(command, args, {
args: string[],
{ env, ...rest }: SpawnSyncOptions = {},
) => {
const cmd = spawnSync(command, args, {
...rest, ...rest,
env: { env: {
...process.env, ...process.env,
@ -25,18 +20,15 @@ export const exec = (
}, },
}) })
const out = cmd.stdout && cmd.stdout.toString().trim() const out = stdout && stdout.toString().trim()
const err = cmd.stderr && cmd.stderr.toString().trim() const err = stderr && stderr.toString().trim()
return { out, err } return { out, err, status }
} }
export const execPlain = (command: string, opt: SpawnSyncOptions = {}) => { export const execPlain = (command: string, opt: SpawnSyncOptions = {}) => {
const split = command.split(' ') const split = command.split(' ')
if (split.length < 1) { if (split.length < 1) throw new Error(`The command ${command} is not valid`.red)
console.log(`The command ${command} is not valid`.red)
return
}
return exec(split[0], split.slice(1), opt) return exec(split[0], split.slice(1), opt)
} }
@ -175,3 +167,31 @@ export class MeasureDuration {
} }
} }
export const decodeLocationFromPrefix = (from: string): [LocationFromPrefixes, string] => {
const firstDelimiter = from.indexOf(':')
if (firstDelimiter === -1) return [LocationFromPrefixes.Filesystem, from]
const type = from.substr(0, firstDelimiter)
const value = from.substr(firstDelimiter + 1)
switch (type.toLowerCase()) {
case 'volume':
return [LocationFromPrefixes.DockerVolume, value]
case 'path':
return [LocationFromPrefixes.Filesystem, value]
default:
throw new Error(`Could not decode the location from: ${from}`.red)
}
}
export const hash = (plain: string): string => createHash('sha1').update(plain).digest().toString('hex')
export const checkIfDockerVolumeExistsOrFail = (volume: string) => {
const cmd = exec('docker', [
'volume', 'inspect', volume,
])
if (cmd.err.length > 0)
throw new Error('Volume not found')
}
Loading…
Cancel
Save