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
+24 -6
View File
@@ -7,6 +7,7 @@ import express from 'express';
import type { Ctx } from '../context.js';
import { requireRole } from '../auth/sessions.js';
import { backupPath, listBackups, retainCount, rotate, runBackup } from '../backups/backups.js';
import { listUploadsSnapshots, rotateUploads, runUploadsBackup } from '../backups/uploads.js';
import { ApiError, handle, sendError } from '../http.js';
import { audit } from '../lib/audit.js';
import { JobError, jobStatus, runNow, setOverride } from '../jobs.js';
@@ -29,6 +30,12 @@ export function maintenanceRouter(ctx: Ctx, who: (req: express.Request) => Who)
nightly: await ctx.settings.bool('backup_nightly', true),
retain: await retainCount(ctx),
lastError: await ctx.settings.get('backup_last_error'),
// 1.5.0: the photos and files.
uploads: {
enabled: await ctx.settings.bool('backup_uploads', true),
lastError: await ctx.settings.get('backup_uploads_last_error'),
items: (await listUploadsSnapshots(ctx)).map((s) => ({ name: s.name, createdAt: s.createdAt, files: s.files, bytes: s.bytes, newFiles: s.newFiles, newBytes: s.newBytes })),
},
})));
r.post('/backups/run', admin, handle(async (req) => {
@@ -38,20 +45,31 @@ export function maintenanceRouter(ctx: Ctx, who: (req: express.Request) => Who)
} catch (e) {
throw new ApiError(502, 'BACKUP_FAILED', `The backup didn't work: ${(e as Error).message}`);
}
let uploads: { name: string; newFiles: number } | null = null;
let uploadsError: string | null = null;
if (await ctx.settings.bool('backup_uploads', true)) {
try {
const s = await runUploadsBackup(ctx);
uploads = { name: s.name, newFiles: s.newFiles };
} catch (e) {
uploadsError = (e as Error).message;
}
}
const me = who(req);
await audit(ctx, { actorId: me.id, actorRole: me.role, action: 'backup_made', targetType: 'backup', targetId: b.filename });
return { filename: b.filename, size: b.size, createdAt: b.createdAt };
await audit(ctx, { actorId: me.id, actorRole: me.role, action: 'backup_made', targetType: 'backup', targetId: b.filename, metadata: { uploads: uploads?.name ?? null } });
return { filename: b.filename, size: b.size, createdAt: b.createdAt, uploads, uploadsError };
}));
r.put('/backups/settings', admin, json, handle(async (req) => {
const retain = Number(req.body?.retain);
if (!Number.isInteger(retain) || retain < 1 || retain > 365) throw new ApiError(400, 'VALIDATION', 'Keep between 1 and 365 backups.', { retain: 'Between 1 and 365' });
const nightly = req.body?.nightly !== false;
await ctx.settings.setMany({ backup_retain_count: String(retain), backup_nightly: nightly ? 'true' : 'false' });
const deleted = await rotate(ctx);
const uploadsOn = req.body?.uploads !== false;
await ctx.settings.setMany({ backup_retain_count: String(retain), backup_nightly: nightly ? 'true' : 'false', backup_uploads: uploadsOn ? 'true' : 'false' });
const deleted = (await rotate(ctx)) + (await rotateUploads(ctx));
const me = who(req);
await audit(ctx, { actorId: me.id, actorRole: me.role, action: 'backup_settings_changed', targetType: 'setting', targetId: 'backup_retain_count', metadata: { retain, nightly, deleted } });
return { retain, nightly, deleted };
await audit(ctx, { actorId: me.id, actorRole: me.role, action: 'backup_settings_changed', targetType: 'setting', targetId: 'backup_retain_count', metadata: { retain, nightly, uploads: uploadsOn, deleted } });
return { retain, nightly, uploads: uploadsOn, deleted };
}));
// A download holds the whole site's content and its editors: audited, never cached.