|
|
|
@@ -7,21 +7,8 @@ import path from "node:path"
|
|
|
|
|
import strftime from 'strftime'
|
|
|
|
|
import { spawnSync } from "node:child_process"
|
|
|
|
|
|
|
|
|
|
function logToFile() {
|
|
|
|
|
// Source - https://stackoverflow.com/a/35542360
|
|
|
|
|
// Posted by Anatol - user3173842
|
|
|
|
|
// Retrieved 2026-01-14, License - CC BY-SA 3.0
|
|
|
|
|
|
|
|
|
|
var access = fs.createWriteStream('/etc/snapback/log.txt');
|
|
|
|
|
process.stdout.write = process.stderr.write = access.write.bind(access);
|
|
|
|
|
|
|
|
|
|
process.on('uncaughtException', function (err) {
|
|
|
|
|
console.error((err && err.stack) ? err.stack : err);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let config: { [key: string]: string } = {}
|
|
|
|
|
let index: { [key: string]: { [key: string]: string } } = {}
|
|
|
|
|
let config: { [key: string]: string|undefined } = {}
|
|
|
|
|
let index: { [key: string]: { [key: string]: string|undefined }|undefined } = {}
|
|
|
|
|
|
|
|
|
|
function sugoiSpawn(...command: string[]) {
|
|
|
|
|
const result = spawnSync(command.shift()!, command, { stdio: 'inherit' })
|
|
|
|
@@ -69,8 +56,7 @@ program.command('install').description('installs snapback on your system')
|
|
|
|
|
program.command('create-snapshot-post <subvolume> <fstype> <number>')
|
|
|
|
|
.description('create a backup after a snapshot')
|
|
|
|
|
.action(function (subvolume, fstype, number) {
|
|
|
|
|
logToFile()
|
|
|
|
|
if (!config[subvolume]) return
|
|
|
|
|
if (!config[subvolume]) {console.log(subvolume + ' not configured for snapback'); return}
|
|
|
|
|
const destination = strftime(config[subvolume])
|
|
|
|
|
const dirname = path.dirname(destination)
|
|
|
|
|
try { fs.mkdirSync(dirname, { 'recursive': true }) } catch {}
|
|
|
|
@@ -79,6 +65,7 @@ program.command('create-snapshot-post <subvolume> <fstype> <number>')
|
|
|
|
|
'--create', '--file', destination,
|
|
|
|
|
'--auto-compress', '--dereference', '.'
|
|
|
|
|
)
|
|
|
|
|
if(!index[subvolume]) index[subvolume]={}
|
|
|
|
|
index[subvolume][number] = destination
|
|
|
|
|
console.log('created backup at '+destination)
|
|
|
|
|
})
|
|
|
|
@@ -86,23 +73,20 @@ program.command('create-snapshot-post <subvolume> <fstype> <number>')
|
|
|
|
|
program.command('delete-snapshot-post <subvolume> <fstype> <number>')
|
|
|
|
|
.description('delete a backup after deleting a snapshot')
|
|
|
|
|
.action(function (subvolume, fstype, number) {
|
|
|
|
|
logToFile()
|
|
|
|
|
if (!index[subvolume]) {
|
|
|
|
|
console.log('no backups indexed for ' + subvolume)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
const destination = index[subvolume][number]
|
|
|
|
|
if (!destination) {
|
|
|
|
|
console.log('no backup indexed for ' + subvolume + ':' + number)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
fs.unlinkSync(destination)
|
|
|
|
|
delete index[subvolume][number]
|
|
|
|
|
console.log('deleted backup at '+destination)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
program.command('delete-config-post <subvolume> <fstype>')
|
|
|
|
|
.description('delete metadata of deleted subvolume, retaining backups')
|
|
|
|
|
.action(function (subvolume, fstype) {
|
|
|
|
|
logToFile()
|
|
|
|
|
delete index[subvolume]
|
|
|
|
|
const destination = strftime(config[subvolume])
|
|
|
|
|
const dirname = path.dirname(destination)
|
|
|
|
|
console.log('existing backups remain at '+dirname)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
program.parse()
|
|
|
|
|
|
|
|
|
|
if (Object.keys(index).length) fs.writeFileSync(program.opts().indexFile,stringifyJSON(index), 'utf8')
|
|
|
|
|
fs.writeFileSync(program.opts().indexFile,stringifyJSON(index), 'utf8')
|