#!/usr/bin/env node import fs from "node:fs"; import path from "node:path"; import process from "node:process"; import { backup, DatabaseSync } from "node:sqlite"; const source = path.resolve(process.cwd(), "var", "recipe-book.sqlite"); const requestedTarget = process.argv[2]; if (!requestedTarget) { throw new Error("Usage: npm run db:backup -- /path/to/recipe-book.sqlite"); } if (!fs.existsSync(source)) { throw new Error(`Database not found: ${source}`); } const target = path.resolve(requestedTarget); if (target === source) { throw new Error("Backup target must differ from the live database."); } if (fs.existsSync(target)) { throw new Error(`Refusing to overwrite existing backup: ${target}`); } fs.mkdirSync(path.dirname(target), { recursive: true }); const database = new DatabaseSync(source, { readOnly: true }); try { await backup(database, target); } finally { database.close(); } console.log(`Backed up ${source} to ${target}`);