autorestic/src/forget.ts

76 lines
2.1 KiB
TypeScript
Raw Normal View History

import { Writer } from 'clitastic'
import { config, VERBOSE } from './autorestic'
import { getEnvFromBackend } from './backend'
import { LocationFromPrefixes } from './config'
import { Locations, Location, Flags } from './types'
import {
exec,
pathRelativeToConfigFile,
getFlagsFromLocation,
makeArrayIfIsNot,
fill, decodeLocationFromPrefix, getPathFromVolume,
} from './utils'
2019-12-03 23:37:55 +01:00
export const forgetSingle = (name: string, to: string, location: Location, dryRun: boolean) => {
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 flags = getFlagsFromLocation(location, 'forget')
const [type, value] = decodeLocationFromPrefix(location.from)
let path: string
switch (type) {
case LocationFromPrefixes.Filesystem:
path = pathRelativeToConfigFile(value)
break
case LocationFromPrefixes.DockerVolume:
path = getPathFromVolume(value)
break
}
if (flags.length == 0) {
2019-12-24 16:52:01 +01:00
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 = fill(name.length + 3)
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 (!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)
}