4 Commits
Author SHA1 Message Date
sugoidogo 8eab92841f handle and log unknown items
Main / release (push) Successful in 41s
2026-01-14 00:43:01 -08:00
sugoidogo fb4e872990 fix output piping
Main / release (push) Successful in 42s
2026-01-14 00:30:11 -08:00
sugoidogo e3893f594e fix subcommand output
Main / release (push) Successful in 42s
2026-01-14 00:21:48 -08:00
sugoidogo 2457617bdc redirect subcommand output to file
Main / release (push) Successful in 41s
2026-01-14 00:17:04 -08:00
+20 -5
View File
@@ -7,12 +7,14 @@ import path from "node:path"
import strftime from 'strftime'
import { spawnSync } from "node:child_process"
const logfd = fs.openSync('/etc/snapback/log.txt', 'w')
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');
var access = fs.createWriteStream('',{'fd':logfd});
process.stdout.write = process.stderr.write = access.write.bind(access);
process.on('uncaughtException', function (err) {
@@ -20,11 +22,11 @@ function logToFile() {
});
}
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' })
const result = spawnSync(command.shift()!, command, { stdio: ['ignore',logfd,logfd] })
if (result.error) throw result.error
if (result.status !== 0) process.exit(result.status)
return result
@@ -70,7 +72,7 @@ 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 +81,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)
})
@@ -87,7 +90,15 @@ 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)
@@ -98,6 +109,10 @@ program.command('delete-config-post <subvolume> <fstype>')
.action(function (subvolume, fstype) {
logToFile()
delete index[subvolume]
if (!config[subvolume]) {
console.log(subvolume + ' not configured for snapback')
return
}
const destination = strftime(config[subvolume])
const dirname = path.dirname(destination)
console.log('existing backups remain at '+dirname)