app: backups of the database and the uploaded images (APP-1.0-PLAN §10)

As the Website's: a gzipped pg_dump into backend/backups every night (02:40,
after Accounts' and the Website's) and on Back up now, named as the
crosscode script names them (app-YYYYMMDD-HHMMSS.sql.gz); a failed dump
leaves no file; the newest kept (14 by default). After each, a snapshot of
backend/uploads, hard-linking what hasn't changed. Settings → Backups lists
both, backs up now, downloads (audited) and sets how many to keep and what's
nightly; Owner and Church admin only. The heartbeat reports the newest
backup.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-12-01 14:50:17 +02:00
co-authored by Claude Opus 5.5
parent c934f47b28
commit 62cfb18c0b
9 changed files with 603 additions and 3 deletions
@@ -0,0 +1,16 @@
import type { Metadata } from "next";
import { AdminBackups } from "@/components/admin/AdminBackups";
import { requireAdmin } from "@/lib/admin";
export const metadata: Metadata = { title: "Backups · Admin", robots: { index: false, follow: false } };
/** Settings → Backups (APP-1.0-PLAN §10): Owner and Church admin. */
export default async function BackupsPage() {
const { app } = await requireAdmin([]);
return (
<div className="space-y-6">
<h1 className="text-2xl font-semibold">Backups</h1>
<AdminBackups timezone={app.timezone} />
</div>
);
}
@@ -0,0 +1,98 @@
"use client";
import { useCallback, useEffect, useState } from "react";
import { Download } from "lucide-react";
import { adminCall, Field, input, Notice, Panel, type Msg } from "./kit";
type Snapshot = { name: string; createdAt: string; files: number; bytes: number; newFiles: number; newBytes: number };
type BackupList = {
items: { filename: string; size: number; createdAt: string }[]; nightly: boolean; retain: number; lastError: string | null;
uploads: { enabled: boolean; lastError: string | null; items: Snapshot[] };
};
const size = (n: number) => (n < 1024 ? `${n} B` : n < 1024 * 1024 ? `${(n / 1024).toFixed(1)} KB` : `${(n / (1024 * 1024)).toFixed(1)} MB`);
/**
* Settings → Backups (APP-1.0-PLAN §10, as Accounts', Events' and the Website's): the database
* backups on this server, every night and before every update, and snapshots of the uploaded
* images. Back up now, download one, how many to keep. A backup holds every prayer request: keep
* a download somewhere safe, and delete it when you're done.
*/
export function AdminBackups({ timezone }: { timezone: string }) {
const [d, setD] = useState<BackupList | null>(null);
const [msg, setMsg] = useState<Msg>(null);
const [busy, setBusy] = useState(false);
const [form, setForm] = useState<{ nightly: boolean; retain: string; uploads: boolean } | null>(null);
const load = useCallback(() => {
adminCall<BackupList>("GET", "/backups").then((r) => {
if (!r.ok) return setMsg({ kind: "error", text: r.error });
setD(r.data);
setForm({ nightly: r.data.nightly, retain: String(r.data.retain), uploads: r.data.uploads.enabled });
});
}, []);
useEffect(load, [load]);
const when = (iso: string) => new Intl.DateTimeFormat("en-ZA", { timeZone: timezone, dateStyle: "medium", timeStyle: "short" }).format(new Date(iso));
async function backUpNow() {
setBusy(true);
setMsg(null);
const r = await adminCall<{ filename: string; uploadsError: string | null }>("POST", "/backups/run", {});
setBusy(false);
if (!r.ok) return setMsg({ kind: "error", text: r.error });
setMsg(r.data.uploadsError ? { kind: "error", text: `The database was backed up, but the images weren't: ${r.data.uploadsError}` } : { kind: "ok", text: `Backed up: ${r.data.filename}.` });
load();
}
async function save(e: React.FormEvent) {
e.preventDefault();
if (!form) return;
setBusy(true);
const r = await adminCall<{ deleted: number }>("PUT", "/backups/settings", { nightly: form.nightly, retain: Number(form.retain), uploads: form.uploads });
setBusy(false);
if (!r.ok) return setMsg({ kind: "error", text: r.error });
setMsg({ kind: "ok", text: `Saved.${r.data.deleted ? ` ${r.data.deleted} older backups deleted.` : ""}` });
load();
}
if (!d || !form) return <Notice msg={msg} />;
return (
<div className="space-y-6">
<Notice msg={msg} />
<Panel title="The database" aside={<button type="button" className="cca-btn" disabled={busy} onClick={backUpNow}>{busy ? "Backing up…" : "Back up now"}</button>}>
<p className="mb-3 text-sm text-gray-600">Made every night and before every update, and kept on this server. A backup holds everything in the App, prayer requests included: keep a download somewhere safe, and delete it when you&apos;re done with it.</p>
{d.lastError && <p className="mb-3 rounded-md bg-red-50 p-3 text-sm text-red-800">The last backup failed: {d.lastError}</p>}
{d.items.length === 0 ? <p className="text-sm text-gray-600">No backups yet.</p> : (
<ul className="divide-y divide-gray-100 text-sm">
{d.items.map((b) => (
<li key={b.filename} className="flex flex-wrap items-center justify-between gap-2 py-2">
<span><span className="font-mono">{b.filename}</span> <span className="text-gray-500">· {when(b.createdAt)} · {size(b.size)}</span></span>
<a href={`/api/v1/admin/backups/${encodeURIComponent(b.filename)}/download`} className="inline-flex items-center gap-1 font-semibold underline-offset-4 hover:underline"><Download className="h-4 w-4" aria-hidden />Download</a>
</li>
))}
</ul>
)}
</Panel>
<Panel title="Uploaded images">
<p className="mb-3 text-sm text-gray-600">A snapshot after each database backup. Only new images take space: the rest are shared with the snapshot before.</p>
{d.uploads.lastError && <p className="mb-3 rounded-md bg-red-50 p-3 text-sm text-red-800">The last snapshot failed: {d.uploads.lastError}</p>}
{d.uploads.items.length === 0 ? <p className="text-sm text-gray-600">No snapshots yet.</p> : (
<ul className="divide-y divide-gray-100 text-sm">
{d.uploads.items.map((s) => <li key={s.name} className="py-2"><span className="font-mono">{s.name}</span> <span className="text-gray-500">· {when(s.createdAt)} · {s.files} images, {size(s.bytes)} ({s.newFiles} new)</span></li>)}
</ul>
)}
</Panel>
<Panel title="Settings">
<form onSubmit={save} className="space-y-3">
<label className="flex items-center gap-2 text-sm"><input type="checkbox" checked={form.nightly} onChange={(e) => setForm({ ...form, nightly: e.target.checked })} />Back up every night (02:40)</label>
<label className="flex items-center gap-2 text-sm"><input type="checkbox" checked={form.uploads} onChange={(e) => setForm({ ...form, uploads: e.target.checked })} />Snapshot the uploaded images too</label>
<Field label="How many to keep" hint="The oldest are deleted beyond this (1 to 365)." className="max-w-xs"><input type="number" min={1} max={365} className={input} value={form.retain} onChange={(e) => setForm({ ...form, retain: e.target.value })} /></Field>
<button type="submit" className="cca-btn" disabled={busy}>Save</button>
</form>
</Panel>
</div>
);
}