logigate/server/utils/backup.ts

76 lines
3.1 KiB
TypeScript
Raw Permalink Normal View History

// Nightly backup of the whole data/ folder into backups/, one gzip file per
// day (one JSON line per stored file). Restore with scripts/restore-backup.mjs.
import { createWriteStream, existsSync } from 'node:fs'
import { mkdir, readdir, readFile, rename, unlink } from 'node:fs/promises'
import { join, relative, sep } from 'node:path'
import { createGzip } from 'node:zlib'
export const backupName = (date: Date) => `logigate-${date.toISOString().slice(0, 10)}.ndjson.gz`
async function files(dir: string, out: string[] = []): Promise<string[]> {
for (const e of await readdir(dir, { withFileTypes: true }).catch(() => [])) {
const p = join(dir, e.name)
if (e.isDirectory()) await files(p, out)
else out.push(p)
}
return out
}
/** Writes today's backup (if missing). Returns its path and how many files it holds. */
export async function writeBackup(dataDir: string, backupDir: string, date = new Date()): Promise<{ file: string; count: number }> {
await mkdir(backupDir, { recursive: true })
const file = join(backupDir, backupName(date))
const tmp = `${file}.tmp`
const gzip = createGzip()
const out = createWriteStream(tmp)
const done = new Promise<void>((resolve, reject) => {
out.on('finish', resolve)
out.on('error', reject)
gzip.on('error', reject)
})
gzip.pipe(out)
const write = (line: unknown) =>
new Promise<void>(resolve => (gzip.write(`${JSON.stringify(line)}\n`) ? resolve() : gzip.once('drain', resolve)))
await write({ logigate: 'backup', version: 1, created: date.toISOString() })
let count = 0
const all = await files(dataDir)
const present = new Set(all)
for (const f of all) {
if (f.includes('.tmp-')) continue
// A .bak only matters when its file is gone (a crash between two renames).
if (f.endsWith('.bak') && present.has(f.slice(0, -4))) continue
const real = f.endsWith('.bak') ? f.slice(0, -4) : f
const data = await readFile(f, 'utf8')
try {
JSON.parse(data)
} catch {
continue // Damaged file: leave it out rather than preserve the damage.
}
await write({ path: relative(dataDir, real).split(sep).join('/'), data })
count++
}
gzip.end()
await done
await rename(tmp, file)
return { file, count }
}
/** Keeps the newest 14 backups plus the first backup of each of the last 12 months. */
export async function pruneBackups(backupDir: string) {
const names = (await readdir(backupDir).catch(() => [])).filter(n => /^logigate-\d{4}-\d{2}-\d{2}\.ndjson\.gz$/.test(n)).sort().reverse()
const keep = new Set(names.slice(0, 14))
const months = new Map<string, string>()
for (const n of names) months.set(n.slice(9, 16), n) // ends on the earliest of each month
for (const n of [...months.values()].slice(0, 12)) keep.add(n)
for (const n of names) if (!keep.has(n)) await unlink(join(backupDir, n)).catch(() => {})
}
export async function backupIfDue(dataDir: string, backupDir: string) {
if (!existsSync(dataDir) || existsSync(join(backupDir, backupName(new Date())))) return
const { file, count } = await writeBackup(dataDir, backupDir)
await pruneBackups(backupDir)
console.log(`[backup] ${count} files to ${file}`)
}