pull/23/head
cupcakearmy 5 years ago
parent a9696bbc0c
commit 417c54db4d

@ -5,10 +5,10 @@ import { tmpdir } from 'os'
import { join, resolve } from 'path' import { join, resolve } from 'path'
import { config, INSTALL_DIR, VERSION } from './autorestic' import { config, INSTALL_DIR, VERSION } from './autorestic'
import { checkAndConfigureBackends, getEnvFromBackend } from './backend' import { checkAndConfigureBackends, getBackendsFromLocations, getEnvFromBackend } from './backend'
import { backupAll } from './backup' import { backupAll } from './backup'
import { forgetAll } from './forget' import { forgetAll } from './forget'
import { Backends, Flags, Locations } from './types' import { Backend, Backends, Flags, Locations } from './types'
import { import {
checkIfCommandIsAvailable, checkIfCommandIsAvailable,
checkIfResticIsAvailable, checkIfResticIsAvailable,
@ -74,12 +74,8 @@ const handlers: Handlers = {
checkIfResticIsAvailable() checkIfResticIsAvailable()
const locations: Locations = parseLocations(flags) const locations: Locations = parseLocations(flags)
const backends = new Set<string>()
for (const to of Object.values(locations).map(location => location.to))
Array.isArray(to) ? to.forEach(t => backends.add(t)) : backends.add(to)
checkAndConfigureBackends( checkAndConfigureBackends(
filterObjectByKey(config.backends, Array.from(backends)), filterObjectByKey(config.backends, getBackendsFromLocations(locations)),
) )
backupAll(locations) backupAll(locations)
@ -88,14 +84,24 @@ const handlers: Handlers = {
restore(args, flags) { restore(args, flags) {
if (!config) throw ConfigError if (!config) throw ConfigError
checkIfResticIsAvailable() checkIfResticIsAvailable()
const locations = parseLocations(flags) const locations = parseLocations(flags)
for (const [name, location] of Object.entries(locations)) { for (const [name, location] of Object.entries(locations)) {
const w = new Writer(name.green + `\t\tRestoring... ⏳`) const baseText = name.green + '\t\t'
const env = getEnvFromBackend( const w = new Writer(baseText + `Starting...`)
config.backends[
Array.isArray(location.to) ? location.to[0] : location.to 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( exec(
'restic', 'restic',
@ -110,14 +116,10 @@ const handlers: Handlers = {
checkIfResticIsAvailable() checkIfResticIsAvailable()
const locations: Locations = parseLocations(flags) const locations: Locations = parseLocations(flags)
const backends = new Set<string>()
for (const to of Object.values(locations).map(location => location.to))
Array.isArray(to) ? to.forEach(t => backends.add(t)) : backends.add(to)
checkAndConfigureBackends( checkAndConfigureBackends(
filterObjectByKey(config.backends, Array.from(backends)), filterObjectByKey(config.backends, getBackendsFromLocations(locations)),
) )
forgetAll(flags['dry-run'], locations) forgetAll(locations, flags)
console.log('\nFinished!'.underline + ' 🎉') console.log('\nFinished!'.underline + ' 🎉')
}, },
@ -238,7 +240,7 @@ export const help = () => {
'\n check [-b, --backend] [-a, --all] Check backends' + '\n check [-b, --backend] [-a, --all] Check backends' +
'\n backup [-l, --location] [-a, --all] Backup all or specified locations' + '\n backup [-l, --location] [-a, --all] Backup all or specified locations' +
'\n forget [-l, --location] [-a, --all] [--dry-run] Forget old snapshots according to declared policies' + '\n forget [-l, --location] [-a, --all] [--dry-run] Forget old snapshots according to declared policies' +
'\n restore [-l, --location] [-- --target <out dir>] Restore all or specified locations' + '\n restore [-l, --location] [--from backend] [-- --target <out dir>] Restore all or specified locations' +
'\n' + '\n' +
'\n exec [-b, --backend] [-a, --all] <command> -- [native options] Execute native restic command' + '\n exec [-b, --backend] [-a, --all] <command> -- [native options] Execute native restic command' +
'\n' + '\n' +

Loading…
Cancel
Save