Acknowledge and hand over alerts, incident timelines, and built-in database backups

- Alerts can be acknowledged (note, assignee, duration); acknowledged alerts are left out of what
  needs attention (sidebar, dashboard strip, cards) but stay raised and counted by /metrics
- Incidents: each time an alert is raised and goes away is recorded (alert_acks, alert_incidents,
  incident_notes, migration 0032), with a timeline of acks, maintenance, changes, update runs,
  related alerts and notes; Incidents page and a digest section
- Backups of Homelab Manager's own database: daily checked VACUUM INTO copies, retention, download,
  check, upload, delete, and restore staged and applied at start-up before the database opens
  (pre-restore copy kept); alerts and /metrics for stale or failing backups
- Docs: README, BACKUP, DATABASE, NOTIFICATIONS, ROLES, API; tests

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
bobbanandClaude Sonnet 5.5 committed 2026-10-18 02:21:08 +02:00
1 parent 7a5b623444
commit 2744a84719
41 files changed
+6573 -26

No files matched your search

+71
View File
@@ -0,0 +1,71 @@
/**
* Finishes a restore that was staged from Settings → 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"));