src/runtime/backup.ts
966 B - 元のファイルを表示
// Backups (lab 12): copy the data folder to a timestamped folder.
//
// Safe while Steward runs: session logs and the audit log are append-only, so a copy taken
// mid-write can only have a torn LAST line, and lab 02's recovery already handles that. Index
// files are replaced atomically (temp file + rename), so a copy sees the old or the new version.
// The lock file is skipped.
import { cp, mkdir } from 'node:fs/promises';
import path from 'node:path';
export async function backupDataDir(
dataDir: string,
targetParent: string,
now = new Date(),
): Promise<string> {
const stamp = now.toISOString().replace(/[-:]/g, '').replace('T', '-').slice(0, 15);
const target = path.join(path.resolve(targetParent), `steward-backup-${stamp}`);
await mkdir(target, { recursive: true });
await cp(dataDir, target, {
recursive: true,
filter: (source) => path.basename(source) !== 'steward.pid' && !source.endsWith('.tmp'),
});
return target;
}