Website 1.5.0: back up the photos and files too

The nightly backup (and Back up now) snapshots backend/uploads after the
database, into backups/uploads-YYYYMMDD-HHMMSS/. Unchanged files are hard
links to the last snapshot, so after the first full copy a night costs
only the new files. Built in a partial folder and renamed when complete,
refused up front when the disk hasn't room, kept to the same count as the
database backups, with its own switch on Admin -> Backups.

crosscode script: an uninstall keeps the Website's backups with rsync -aH,
so the snapshots' shared files stay shared.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-27 10:04:38 +02:00
co-authored by Claude Opus 5.5
parent 48abc06db7
commit 8c213d6d84
13 changed files with 383 additions and 28 deletions
@@ -2,15 +2,19 @@
/**
* Settings → Backups (1.4.0, as Accounts and Cross Events): the database backups on this server,
* every night and before every update. Back up now, download one, how many to keep. Owners and
* Admins only.
* every night and before every update. Back up now, download one, how many to keep. 1.5.0: the
* photos and files too, as snapshots on the server. Owners and Admins only.
*/
import { useState } from "react";
import { DatabaseBackup, Download } from "lucide-react";
import { api } from "@/admin/api";
import { Btn, Field, Loading, Notice, Panel, Title, inputClass, useAction, useAdmin, when } from "@/admin/ui";
type BackupList = { items: { filename: string; size: number; createdAt: string }[]; nightly: boolean; retain: number; lastError: string | null };
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`);
@@ -20,38 +24,50 @@ export default function Backups() {
const save = useAction();
const [nightly, setNightly] = useState<boolean | null>(null);
const [retain, setRetain] = useState<string | null>(null);
const [withUploads, setWithUploads] = useState<boolean | null>(null);
if (!list.data) return <Loading error={list.error} />;
const d = list.data;
const nightlyOn = nightly ?? d.nightly;
const keep = retain ?? String(d.retain);
const uploadsOn = withUploads ?? d.uploads.enabled;
return (
<>
<Title icon={DatabaseBackup} sub="Copies of your website's database, kept on this server only and never sent anywhere.">Backups</Title>
<div className="space-y-6">
<Panel title="Database backups" sub="Every night at 02:20, and before every update of your website. Your photos and files (the media library) aren't in them.">
{d.lastError && <div className="mb-4"><Notice tone="warning">The last backup didn&apos;t work: {d.lastError}</Notice></div>}
<Panel title="Backups" sub="Every night at 02:20: your website's database, and your photos and files (the media library). The database is also backed up before every update.">
{d.lastError && <div className="mb-4"><Notice tone="warning">The last database backup didn&apos;t work: {d.lastError}</Notice></div>}
{d.uploads.lastError && <div className="mb-4"><Notice tone="warning">The last backup of the photos and files didn&apos;t work: {d.uploads.lastError}</Notice></div>}
{now.message && <div className="mb-4"><Notice tone={now.message.tone}>{now.message.text}</Notice></div>}
<Btn kind="primary" busy={now.busy} onClick={async () => {
const b = await now.run(() => api<{ filename: string }>("/backups/run", { method: "POST" }));
if (b) now.setMessage({ tone: "success", text: `Backup made: ${b.filename}` });
const b = await now.run(() => api<{ filename: string; uploads: { newFiles: number } | null; uploadsError: string | null }>("/backups/run", { method: "POST" }));
if (b) {
now.setMessage(b.uploadsError
? { tone: "error", text: `The database was backed up (${b.filename}), but the photos and files weren't: ${b.uploadsError}` }
: { tone: "success", text: `Backup made: ${b.filename}${b.uploads ? `, and the photos and files (${b.uploads.newFiles} new since the last time)` : ""}.` });
}
list.reload();
}}>Back up now</Btn>
<form className="mt-6 space-y-4 border-t border-gray-200 pt-4" onSubmit={async (e) => {
e.preventDefault();
const r = await save.run(() => api<{ deleted: number }>("/backups/settings", { method: "PUT", body: { retain: Number(keep), nightly: nightlyOn } }));
const r = await save.run(() => api<{ deleted: number }>("/backups/settings", { method: "PUT", body: { retain: Number(keep), nightly: nightlyOn, uploads: uploadsOn } }));
if (r) save.setMessage({ tone: "success", text: r.deleted ? `Saved. ${r.deleted} older backup${r.deleted === 1 ? " was" : "s were"} deleted.` : "Saved." });
setNightly(null);
setRetain(null);
setWithUploads(null);
list.reload();
}}>
<label className="flex items-start gap-3">
<input type="checkbox" className="mt-1 h-4 w-4 rounded border-gray-300" checked={nightlyOn} onChange={(e) => setNightly(e.target.checked)} />
<span><span className="block text-sm font-medium text-gray-800">Back up every night</span><span className="block text-xs text-gray-500">Backups before each update are made either way.</span></span>
</label>
<label className="flex items-start gap-3">
<input type="checkbox" className="mt-1 h-4 w-4 rounded border-gray-300" checked={uploadsOn} onChange={(e) => setWithUploads(e.target.checked)} />
<span><span className="block text-sm font-medium text-gray-800">Include the photos and files</span><span className="block text-xs text-gray-500">The first backup copies them all; after that, only what&apos;s new is copied.</span></span>
</label>
<div className="max-w-xs">
<Field label="Keep the newest" hint="Older backups are deleted (1 to 365)." error={save.fields.retain}>
<Field label="Keep the newest" hint="Of each kind; older backups are deleted (1 to 365)." error={save.fields.retain}>
<input type="number" min={1} max={365} className={inputClass} value={keep} onChange={(e) => setRetain(e.target.value)} />
</Field>
</div>
@@ -60,7 +76,7 @@ export default function Backups() {
</form>
</Panel>
<Panel title="Backups on this server" sub="A backup holds your whole site and the names and email addresses of its editors: keep a downloaded copy somewhere safe, and delete it when you're done.">
<Panel title="Database backups on this server" sub="A backup holds your whole site and the names and email addresses of its editors: keep a downloaded copy somewhere safe, and delete it when you're done.">
<div className="overflow-x-auto">
<table className="w-full text-left text-sm">
<thead className="text-gray-600"><tr><th className="py-2 pr-4 font-medium">Made</th><th className="py-2 pr-4 font-medium">Size</th><th className="py-2 font-medium"><span className="sr-only">Download</span></th></tr></thead>
@@ -81,6 +97,25 @@ export default function Backups() {
</table>
</div>
</Panel>
<Panel title="Photos and files" sub="Kept on this server. To put them back, Cross Code copies a backup into your media library.">
<div className="overflow-x-auto">
<table className="w-full text-left text-sm">
<thead className="text-gray-600"><tr><th className="py-2 pr-4 font-medium">Made</th><th className="py-2 pr-4 font-medium">Files</th><th className="py-2 pr-4 font-medium">Size</th><th className="py-2 font-medium">New since the one before</th></tr></thead>
<tbody className="divide-y divide-gray-200">
{d.uploads.items.map((s) => (
<tr key={s.name}>
<td className="whitespace-nowrap py-2 pr-4">{when(s.createdAt)}</td>
<td className="py-2 pr-4">{s.files}</td>
<td className="py-2 pr-4 text-gray-600">{size(s.bytes)}</td>
<td className="py-2 text-gray-600">{s.newFiles} ({size(s.newBytes)})</td>
</tr>
))}
{!d.uploads.items.length && <tr><td colSpan={4} className="py-3 text-gray-600">{uploadsOn ? "None yet: the first is made tonight, or with Back up now." : "Off: the photos and files aren't backed up."}</td></tr>}
</tbody>
</table>
</div>
</Panel>
</div>
</>
);