Files
formulation/scripts/db-backup.mjs
T
nicholasandnicholas 2a1e16ed30
Build & Deploy Formulation / Build & Push Image (push) Failing after 15s
Build & Deploy Formulation / deploy (push) Skipped
add core features (#14)
Reviewed-on: #14
Co-authored-by: Nicholas Ward <nicholaspward@outlook.com>
2026-08-18 18:22:34 -05:00

34 lines
961 B
JavaScript

#!/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}`);