autorestic/src/forget.ts

61 lines
1.9 KiB
TypeScript
Raw Normal View History

import { Writer } from 'clitastic'
import { config, VERBOSE } from './autorestic'
import { getEnvFromBackend } from './backend'
import { Locations, Location, Flags } from './types'
2019-12-05 00:23:49 +01:00
import { exec, ConfigError, pathRelativeToConfigFile, getFlagsFromLocation, makeArrayIfIsNot } from './utils'
2019-12-03 23:37:55 +01:00
export const forgetSingle = (name: string, to: string, location: Location, dryRun: boolean) => {
2019-12-03 23:37:55 +01:00
if (!config) throw ConfigError
const base = name + to.blue + ' : '
const writer = new Writer(base + 'Removing old snapshots… ⏳')
2019-12-03 23:37:55 +01:00
const backend = config.backends[to]
const path = pathRelativeToConfigFile(location.from)
const flags = getFlagsFromLocation(location, 'forget')
if (flags.length == 0) {
writer.done(base + 'skipping, no policy declared')
return
2019-12-03 23:37:55 +01:00
}
if (dryRun) flags.push('--dry-run')
writer.replaceLn(base + 'Forgetting old snapshots… ⏳')
const cmd = exec(
'restic',
['forget', '--path', path, '--prune', ...flags],
{ env: getEnvFromBackend(backend) },
)
2019-12-03 23:37:55 +01:00
if (VERBOSE) console.log(cmd.out, cmd.err)
writer.done(base + 'Done ✓'.green)
}
export const forgetLocation = (name: string, backup: Location, dryRun: boolean) => {
2019-12-03 23:37:55 +01:00
const display = name.yellow + ' ▶ '
const filler = new Array(name.length + 3).fill(' ').join('')
let first = true
2019-12-05 00:23:49 +01:00
for (const t of makeArrayIfIsNot(backup.to)) {
const nameOrBlankSpaces: string = first ? display : filler
forgetSingle(nameOrBlankSpaces, t, backup, dryRun)
if (first) first = false
2019-12-03 23:37:55 +01:00
}
2019-12-03 23:30:53 +01:00
}
2019-12-04 20:38:14 +01:00
export const forgetAll = (backups?: Locations, flags?: Flags) => {
2019-12-03 23:37:55 +01:00
if (!config) throw ConfigError
if (!backups) {
backups = config.locations
}
console.log('\nRemoving old snapshots according to policy'.underline.grey)
2019-12-04 20:38:14 +01:00
const dryRun = flags ? flags['dry-run'] : false
2019-12-03 23:37:55 +01:00
if (dryRun) console.log('Running in dry-run mode, not touching data\n'.yellow)
for (const [name, backup] of Object.entries(backups))
forgetLocation(name, backup, dryRun)
}