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:
@@ -8,7 +8,8 @@ import path from 'node:path';
|
||||
import { Readable } from 'node:stream';
|
||||
import zlib from 'node:zlib';
|
||||
import request from 'supertest';
|
||||
import { BACKUP_FILE, backupName, backupPath, listBackups, runBackup, runBackupJob, type Dumper } from '../src/backups/backups.js';
|
||||
import { BACKUP_FILE, backupName, backupPath, listBackups, runBackup, runBackupJob, useDumperForTest, type Dumper } from '../src/backups/backups.js';
|
||||
import { listUploadsSnapshots, runNightlyBackups, runUploadsBackup, UPLOADS_SNAPSHOT } from '../src/backups/uploads.js';
|
||||
import { health } from '../src/hub/client.js';
|
||||
import { JOBS, cronFor, isEnabled, jobsHealth, runNow, useEnvForTest } from '../src/jobs.js';
|
||||
import { hashToken } from '../src/lib/crypto.js';
|
||||
@@ -19,7 +20,7 @@ type Role = 'owner' | 'admin' | 'editor';
|
||||
const dumpOf = (sql: string): Dumper => () => ({ stdout: Readable.from([sql]), done: Promise.resolve() });
|
||||
const failing: Dumper = () => ({ stdout: Readable.from(['-- half a dump']), done: Promise.reject(new Error('pg_dump stopped (exit 1): connection refused')) });
|
||||
|
||||
after(() => useEnvForTest(process.env));
|
||||
after(() => { useEnvForTest(process.env); useDumperForTest(null); });
|
||||
|
||||
async function site() {
|
||||
const h = await harness({ linked: false });
|
||||
@@ -103,7 +104,7 @@ describe('backups (1.4.0)', () => {
|
||||
|
||||
assert.equal((await admin.put('/backups/settings', { retain: 0, nightly: true })).status, 400);
|
||||
const saved = await admin.put('/backups/settings', { retain: 30, nightly: false });
|
||||
assert.deepEqual(saved.body, { retain: 30, nightly: false, deleted: 0 });
|
||||
assert.deepEqual(saved.body, { retain: 30, nightly: false, uploads: true, deleted: 0 });
|
||||
assert.equal(await h.ctx.settings.bool('backup_nightly', true), false);
|
||||
});
|
||||
|
||||
@@ -173,3 +174,80 @@ describe('background jobs (1.4.0)', () => {
|
||||
assert.deepEqual(ran.body, { queued: false, output: { added: 0, skipped: 'no source' } });
|
||||
});
|
||||
});
|
||||
|
||||
describe('backups of the photos and files (1.5.0)', () => {
|
||||
useDumperForTest(dumpOf('-- the database\n'));
|
||||
async function withUploads() {
|
||||
const h = await site();
|
||||
const up = fs.mkdtempSync(path.join(os.tmpdir(), 'ccw-up-'));
|
||||
h.ctx.config.uploadDir = up;
|
||||
fs.mkdirSync(path.join(up, 'demo'));
|
||||
fs.mkdirSync(path.join(up, '.thumbnails'));
|
||||
fs.writeFileSync(path.join(up, 'a.jpg'), 'photo a');
|
||||
fs.writeFileSync(path.join(up, 'demo', 'b.svg'), '<svg/>');
|
||||
fs.writeFileSync(path.join(up, '.thumbnails', 't.jpg'), 'made again on demand');
|
||||
return { ...h, up };
|
||||
}
|
||||
const ino = (p: string) => fs.statSync(p).ino;
|
||||
|
||||
it('a snapshot is a real copy of the uploads, without .thumbnails', async () => {
|
||||
const h = await withUploads();
|
||||
const s = await runUploadsBackup(h.ctx);
|
||||
assert.match(s.name, UPLOADS_SNAPSHOT);
|
||||
const snap = path.join(h.dir, s.name);
|
||||
assert.equal(fs.readFileSync(path.join(snap, 'a.jpg'), 'utf8'), 'photo a');
|
||||
assert.equal(fs.readFileSync(path.join(snap, 'demo', 'b.svg'), 'utf8'), '<svg/>');
|
||||
assert.ok(!fs.existsSync(path.join(snap, '.thumbnails')));
|
||||
assert.deepEqual([s.files, s.newFiles], [2, 2]);
|
||||
assert.notEqual(ino(path.join(snap, 'a.jpg')), ino(path.join(h.up, 'a.jpg')), 'not the live file itself');
|
||||
// Deleting the live file leaves the backup.
|
||||
fs.rmSync(path.join(h.up, 'a.jpg'));
|
||||
assert.equal(fs.readFileSync(path.join(snap, 'a.jpg'), 'utf8'), 'photo a');
|
||||
});
|
||||
|
||||
it('the next one links what\'s unchanged and copies only what\'s new', async () => {
|
||||
const h = await withUploads();
|
||||
const first = await runUploadsBackup(h.ctx);
|
||||
fs.writeFileSync(path.join(h.up, 'c.png'), 'new photo');
|
||||
const second = await runUploadsBackup(h.ctx);
|
||||
assert.notEqual(second.name, first.name);
|
||||
assert.deepEqual([second.files, second.newFiles, second.newBytes], [3, 1, 'new photo'.length]);
|
||||
assert.equal(ino(path.join(h.dir, second.name, 'a.jpg')), ino(path.join(h.dir, first.name, 'a.jpg')), 'unchanged: a hard link');
|
||||
assert.equal((await listUploadsSnapshots(h.ctx))[0]!.name, second.name);
|
||||
});
|
||||
|
||||
it('only the newest are kept, as the database backups; a crash leaves no half snapshot behind', async () => {
|
||||
const h = await withUploads();
|
||||
await h.ctx.settings.set('backup_retain_count', '2');
|
||||
fs.mkdirSync(path.join(h.dir, '.uploads-20200101-000000-partial'));
|
||||
for (let i = 0; i < 3; i++) await runUploadsBackup(h.ctx);
|
||||
assert.equal((await listUploadsSnapshots(h.ctx)).length, 2);
|
||||
assert.ok(!fs.existsSync(path.join(h.dir, '.uploads-20200101-000000-partial')));
|
||||
});
|
||||
|
||||
it('the nightly job backs up both, and leaves the uploads out when that\'s off', async () => {
|
||||
const h = await withUploads();
|
||||
const ran = await runNightlyBackups(h.ctx) as { database: { filename?: string }; uploads: { name?: string; skipped?: boolean } };
|
||||
assert.ok(ran.database.filename && ran.uploads.name, JSON.stringify(ran));
|
||||
await h.ctx.settings.set('backup_uploads', 'false');
|
||||
assert.deepEqual((await runNightlyBackups(h.ctx) as { uploads: unknown }).uploads, { skipped: true });
|
||||
await h.ctx.settings.set('backup_nightly', 'false');
|
||||
assert.deepEqual(await runNightlyBackups(h.ctx), { database: { skipped: true } });
|
||||
});
|
||||
|
||||
it('Back up now makes both; the page lists the snapshots; the switch is saved', async () => {
|
||||
const h = await withUploads();
|
||||
const admin = await h.as('admin');
|
||||
const made = await admin.post('/backups/run');
|
||||
assert.equal(made.status, 200, JSON.stringify(made.body));
|
||||
assert.equal(made.body.uploads.newFiles, 2);
|
||||
assert.equal(made.body.uploadsError, null);
|
||||
const list = await admin.get('/backups');
|
||||
assert.equal(list.body.uploads.enabled, true);
|
||||
assert.equal(list.body.uploads.items.length, 1);
|
||||
assert.equal(list.body.uploads.items[0].files, 2);
|
||||
const off = await admin.put('/backups/settings', { retain: 14, nightly: true, uploads: false });
|
||||
assert.equal(off.body.uploads, false);
|
||||
assert.equal(await h.ctx.settings.bool('backup_uploads', true), false);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user