Files
wekan/tests/ferretdbTextDataBackup.test.cjs
T
Lauri Ojansivu 63c77a27f4 Automatic recovery/remediation for the SQLite text data + Admin Panel Recovery report
Completes the #6492 data-safety system on the WeKan side (FerretDB detection/repair/cap
landed in the fork):

- Decision brain: models/lib/recoveryPlan.js decideRecovery() picks the least-invasive
  recovery when the database is KNOWN corrupt (latest good backup -> previous backup ->
  re-migrate from MongoDB -> manual). It never acts on a healthy/unknown database and
  never restores a known-bad backup.
- Startup restore/re-migrate: every FerretDB launch path (snap, bundled release, Docker)
  can restore the text data BEFORE FerretDB opens the files, on request
  (WEKAN_FORCE_RESTORE env or a RESTORE_REQUESTED marker). It copies a known-good backup
  INTO the live database (dropping stale WAL side-files); backups are never deleted and
  the main wekan.sqlite is only overwritten, never removed. remigrate is delegated to
  migration-control. Attachments/avatars on the filesystem are untouched.
- Recovery events: the scripts append JSON lines to recovery-events.jsonl; the server
  (server/recovery.js) imports them into the recoveryEvents collection on startup, plus
  an admin-only recordRecoveryEvent method.
- Admin Panel / Problems / Recovery: a new admin-only report
  (server/publications/recoveryReport.js, wired in adminReports.js/.jade + i18n) lists
  the remediation history newest-first, readies up front and fetches directly (no hang).
- Docs: docs/Features/Admin-Panel/Problems/Recovery.md.

Tests (wired into npm run test:unit:node), verified by mirror since there is no Node
runtime here to execute them:
  - recoveryPlan.test.cjs (decision logic, positive + negatives)
  - recoveryEventsJsonl.test.cjs (parser: skips junk, normalizes severity, never throws)
  - recoveryReportQuery.test.cjs (search selector escapes regex metacharacters)
  - recoveryReportWiring.test.cjs (report wired; publication/count/method admin-gated)
  - ferretdbTextDataBackup.test.cjs extended: restore copies from a backup, is
    request-gated, and NEVER deletes the live text data or a backup copy

Thanks to bluetopaz1204, mueschel and xet7 !

Fixes #6492,
2026-07-21 10:06:40 +03:00

118 lines
4.7 KiB
JavaScript

'use strict';
// Guards for the WeKan text-data backup safety measure (#6492).
//
// Before FerretDB opens its SQLite files, every FerretDB launch path keeps a rotating
// backup of the TEXT-DATA database (wekan.sqlite*) in a "backup" subfolder of the same
// data dir, so a known copy is ready to restore if the live database is ever detected
// corrupt. Attachments and avatars live on the filesystem and are not copied.
//
// The CRITICAL safety property enforced here: the backup must only ever COPY from the
// live database — it must NEVER move or delete the live wekan.sqlite, and must never
// wipe the whole data dir. A backup that could destroy the data it protects is worse
// than none.
//
// Run: node tests/ferretdbTextDataBackup.test.cjs
const assert = require('assert');
const fs = require('fs');
const path = require('path');
let passed = 0;
function test(name, fn) { fn(); passed += 1; console.log(' ok -', name); }
const read = rel => fs.readFileSync(path.join(__dirname, '..', rel), 'utf8');
console.log('ferretdbTextDataBackup:');
const SCRIPTS = [
'snap-src/bin/ferretdb-control',
'releases/ferretdb/start-wekan.sh',
'releases/ferretdb/wekan-entrypoint.sh',
];
// The live MAIN database file (the top-level one, not a backup subfolder copy, and not
// the -wal/-shm side files). A restore legitimately drops the stale -wal/-shm and
// overwrites the main file by COPY; what must never happen is rm/mv of the main
// wekan.sqlite itself — the "(?!-)" excludes the -wal/-shm side files.
const LIVE_RE = /\$\{?(SQLITE_DIR|FERRETDB_SQLITE_DIR)\}?\/wekan\.sqlite(?!-)/;
for (const rel of SCRIPTS) {
const src = read(rel);
const lines = src.split('\n');
test(`${rel}: copies the live text-data db into a backup dir`, () => {
// A cp of the live wekan.sqlite* into the backup subfolder ($_bk).
assert.ok(
/cp -f [^\n]*wekan\.sqlite\*[^\n]*_bk/.test(src),
'must cp the live wekan.sqlite* into the backup dir',
);
// Backups live in a "backup" subfolder of the data dir.
assert.ok(/backup/.test(src) && /_bk=/.test(src), 'must use a backup subfolder');
});
test(`${rel}: keeps a previous backup generation`, () => {
assert.ok(/\/prev/.test(src), 'must keep a previous backup generation under backup/prev');
});
test(`${rel}: backup is guarded (WEKAN_SQLITE_BACKUP default on, file must exist)`, () => {
assert.ok(/WEKAN_SQLITE_BACKUP:-true/.test(src), 'backup must default on and be overridable');
assert.ok(
/\[ -f "\$\{?(SQLITE_DIR|FERRETDB_SQLITE_DIR)\}?\/wekan\.sqlite" \]/.test(src),
'backup must only run when the live database exists',
);
});
test(`${rel}: NEGATIVE — the live text data is never moved or deleted`, () => {
for (const line of lines) {
const t = line.trim();
if (t.startsWith('#')) continue; // ignore comments
// No rm/mv may target the LIVE wekan.sqlite (top-level data dir). Deleting or
// moving the live database is exactly what a backup must never do.
if (/\b(rm|mv)\b/.test(t)) {
assert.ok(
!LIVE_RE.test(t),
`rm/mv must never target the live wekan.sqlite: ${t}`,
);
// And never wipe the whole data dir.
assert.ok(
!/rm\s+-[a-z]*r[a-z]*\s+["']?\$?\{?(SQLITE_DIR|FERRETDB_SQLITE_DIR)\}?["']?\s*$/.test(t),
`rm must not remove the whole data dir: ${t}`,
);
}
}
});
test(`${rel}: restore copies a backup INTO the live db, request-gated, no backup deleted`, () => {
// Restore copies from the chosen backup source into the live data dir.
assert.ok(
/cp -f "\$\{?_rsrc\}?"\/wekan\.sqlite\* "\$\{?(SQLITE_DIR|FERRETDB_SQLITE_DIR)\}?\/"/.test(src),
'restore must copy the backup INTO the live database dir',
);
// A restore is only ever REQUESTED (env or marker), never automatic guesswork.
assert.ok(
/WEKAN_FORCE_RESTORE/.test(src) && /RESTORE_REQUESTED/.test(src),
'restore must be request-gated (WEKAN_FORCE_RESTORE / RESTORE_REQUESTED)',
);
// NEGATIVE: no rm/mv may target a backup copy's wekan.sqlite — the backups that
// recovery depends on must never be destroyed.
for (const line of lines) {
const t = line.trim();
if (t.startsWith('#')) continue;
if (/\b(rm|mv)\b/.test(t) && /(_rbk|_bk|_rsrc)/.test(t)) {
assert.ok(!/wekan\.sqlite/.test(t), `rm/mv must never target a backup copy: ${t}`);
}
}
});
test(`${rel}: NEGATIVE — attachments/avatars are not copied into the backup`, () => {
// Only wekan.sqlite* is backed up here; attachments/avatars stay on the filesystem.
assert.ok(
!/cp[^\n]*(attachments|avatars)[^\n]*backup/.test(src),
'the text-data backup must not copy attachments/avatars',
);
});
}
console.log(`\n${passed} tests passed`);