/** * Finishes a restore that was staged from Settings → Data → Backup. It has to happen before the database is opened, which is why it is a module * of its own that index.ts imports first: by the time anything else loads, the file is already the one that was chosen. * * Nothing is deleted. The database that was in use is moved into the backups folder (as "…-pre-restore.sqlite") before the chosen one is put * in its place, so a restore that wasn't what was wanted can itself be undone by restoring that file. If anything goes wrong half way, the * old file is put back and the staged one is left where it is. */ import "../loadEnv.js"; import { existsSync, mkdirSync, readFileSync, renameSync, rmSync, statSync, writeFileSync } from "node:fs"; import { dirname, join } from "node:path"; import { resolveDataPath } from "../paths.js"; import { PENDING_FILE, PENDING_NOTE, RESULT_NOTE, type RestoreResult } from "./restoreFiles.js"; const stamp = (d: Date) => d.toISOString().slice(0, 19).replace(/[:T]/g, "-"); /** Puts a staged database in place, if one is waiting. Returns what it did, or null if nothing was staged. */ export function applyPendingRestore(dbFile: string, now: Date = new Date()): RestoreResult | null { const dataDir = dirname(dbFile); const pending = join(dataDir, PENDING_FILE); if (!existsSync(pending)) return null; // Only a file is ever put in place of the database. if (!statSync(pending).isFile()) { console.error(`[restore] ${PENDING_FILE} isn't a file, so it was left alone and the database is as it was.`); return null; } let note: { from?: string; by?: string | null } = {}; try { note = JSON.parse(readFileSync(join(dataDir, PENDING_NOTE), "utf8")); } catch { // the note only says where it came from; the file is what matters } const backups = join(dataDir, "backups"); let keptAs: string | null = null; let moved = false; try { mkdirSync(backups, { recursive: true }); if (existsSync(dbFile)) { let name = `homelab-manager-${stamp(now)}-pre-restore.sqlite`; for (let n = 2; existsSync(join(backups, name)); n++) name = `homelab-manager-${stamp(now)}-pre-restore-${n}.sqlite`; renameSync(dbFile, join(backups, name)); keptAs = name; moved = true; } // A journal left by the old file would be taken for the new one's. for (const suffix of ["-wal", "-shm", "-journal"]) rmSync(dbFile + suffix, { force: true }); renameSync(pending, dbFile); } catch (err) { if (moved && keptAs && !existsSync(dbFile)) { try { renameSync(join(backups, keptAs), dbFile); } catch { // nothing more can be done; the old file is still in the backups folder under its new name } } console.error("[restore] couldn't put the staged backup in place; the database is as it was:", err instanceof Error ? err.message : err); return null; } const result: RestoreResult = { restoredAt: now.toISOString(), from: note.from ?? "an uploaded backup", keptAs, by: note.by ?? null }; writeFileSync(join(dataDir, RESULT_NOTE), JSON.stringify(result)); rmSync(join(dataDir, PENDING_NOTE), { force: true }); console.log(`[restore] restored the database from ${result.from}${keptAs ? `; the one that was in use is kept as backups/${keptAs}` : ""}.`); return result; } // Run it now, on the database this process is about to open. applyPendingRestore(resolveDataPath(process.env.DATABASE_PATH ?? "../data/homelab-manager.sqlite"));