// Automatic daily snapshots of the whole database (every account), kept next to it in DATA_DIR/backups. // A snapshot is a complete, consistent copy made while the app keeps running, and is restored with // scripts/restore-data.sh. BACKUP_KEEP sets how many to keep (default 7); BACKUP_DISABLED=1 turns it off. import { existsSync, mkdirSync, readdirSync, statSync, unlinkSync } from 'fs'; import { join } from 'path'; import { backupDatabase, dataDir } from './db.js'; const DAY = 24 * 60 * 60 * 1000; export const backupDir = () => join(dataDir(), 'backups'); const stamp = (d) => d.toISOString().replace(/[:T]/g, '-').slice(0, 16); const isSnapshot = (name) => /^projects-\d{4}-\d{2}-\d{2}-\d{2}-\d{2}\.db$/.test(name); export function listSnapshots(dir = backupDir()) { if (!existsSync(dir)) return []; return readdirSync(dir).filter(isSnapshot).sort().reverse() .map((name) => ({ name, bytes: statSync(join(dir, name)).size, at: statSync(join(dir, name)).mtimeMs })); } // Deletes all but the newest `keep` snapshots; returns the names removed. export function pruneSnapshots(keep = 7, dir = backupDir()) { const old = listSnapshots(dir).slice(Math.max(1, keep)); old.forEach((s) => unlinkSync(join(dir, s.name))); return old.map((s) => s.name); } export async function runBackup(now = new Date()) { const dir = backupDir(); mkdirSync(dir, { recursive: true }); const file = join(dir, `projects-${stamp(now)}.db`); await backupDatabase(file); pruneSnapshots(Number(process.env.BACKUP_KEEP) || 7, dir); return file; } // Takes a snapshot when the newest is more than a day old (checked hourly, and once at start-up). export function startBackupScheduler() { if (process.env.BACKUP_DISABLED === '1') return; const tick = () => { const latest = listSnapshots()[0]; if (latest && Date.now() - latest.at < DAY) return; runBackup().then((f) => console.log(`Database snapshot saved: ${f}`)).catch((err) => console.error('Database snapshot failed:', err.message)); }; setTimeout(tick, 30_000).unref(); setInterval(tick, 60 * 60 * 1000).unref(); }