Files
wekan/server/lib/backupRuns.js
T
Lauri Ojansivu 06b7ce994a Backup status: every run recorded, failures and overdue backups shown, old local backups pruned - in Admin Panel too.
The result of an Admin Panel backup lived only in the memory of the server
process that ran it. After a restart, or on another pod of a multi-pod
deployment, the Backup pane said "idle", so nobody could see whether last
night's backup had worked. There was no history, no warning when a scheduled
backup failed or did not run, and local backups were never cleaned up.

- Every run, scheduled or "Backup now", is recorded in backupRuns: start and
  end, who or the schedule, scope, storage, status, size, duration and error.
  Records are kept 400 days. A run still "running" after 24 hours counts as
  failed, and a scheduled run skipped for another backup is recorded as failed.
- Admin Panel / Problems / Backup shows the last success (overdue when its
  period has passed), the last failure with its error, the next run and the
  last 30 runs. The same status is above the schedule in Attachments / Backup.
- Problems / Summary lists "Backup failed" and "Backup overdue". /metrics adds
  wekan_backup_last_success_timestamp_seconds,
  wekan_backup_last_run_timestamp_seconds, wekan_backup_last_run_success and
  wekan_backup_overdue for alerting.
- BACKUP_KEEP_DAYS: after a successful backup, local backups of the same scope
  older than N days are deleted. The newest is always kept, and only a
  directory inside the backup root holding a backup.zip is removed.

From the patch by Raymond-Voogt, and in Admin Panel as well:
- BACKUP_KEEP_DAYS is the catalog's backup section
  (models/lib/authConfigCatalog.js), overridable beside the schedule in
  Admin Panel / Attachments / Backup and resolved through authEnv at every
  backup.
- It is listed on every platform: the compose files, start-wekan.sh and .bat,
  the snap config and help, and the Dockerfile, which the patch's version
  had missed. Its platform hunks, written before #6789, were re-applied by
  hand.

tests/backupRuns.test.cjs (21): the run records, overdue and pruning against a
real directory, and the Admin Panel override, including that nothing reads the
variable around it. tests/authConfigCatalog.test.cjs checks every platform.
admin-attachment-retention.e2e.js checks the Backup pane shows the field.

Thanks to Raymond-Voogt and xet7 !

Fixes #6790.
2026-10-20 13:30:00 +03:00

53 lines
1.9 KiB
JavaScript

// Storage of backup runs (models/lib/backupRuns.js has the rules). Recording
// must never change the outcome of a backup: every write here is best-effort.
import { Mongo } from 'meteor/mongo';
import os from 'os';
const {
HISTORY_DAYS, DAY_MS, STATUS_RUNNING, runStartDoc, runFinishSet, backupHealth,
normalizeKeepDays,
} = require('/models/lib/backupRuns');
export const BackupRuns = new Mongo.Collection('backupRuns');
export async function recordRunStart(fields) {
try {
// Keep the log bounded.
await BackupRuns.removeAsync({ startedAt: { $lt: new Date(Date.now() - HISTORY_DAYS * DAY_MS) } });
} catch (_) { /* best-effort */ }
try {
return await BackupRuns.insertAsync(runStartDoc({ ...fields, host: os.hostname() }));
} catch (e) {
console.error('[backup] could not record the run start:', e && e.message); // eslint-disable-line no-console
return null;
}
}
export async function recordRunFinish(runId, startedAt, result) {
if (!runId) return;
try {
await BackupRuns.updateAsync({ _id: runId, status: STATUS_RUNNING },
{ $set: runFinishSet({ ...result, startedAt }) });
} catch (e) {
console.error('[backup] could not record the run result:', e && e.message); // eslint-disable-line no-console
}
}
export async function recentRuns(selector = {}, limit = 30) {
return BackupRuns.find(selector, { sort: { startedAt: -1 }, limit }).fetchAsync();
}
export async function currentBackupHealth(schedule, now = new Date()) {
// Enough instance runs to find the last success behind a run of failures.
const runs = await recentRuns({ orgId: null }, 200);
return backupHealth({ runs, schedule, now });
}
// Admin Panel / Attachments / Backup first, then the environment variable
// (models/lib/authConfigCatalog.js), read at every backup.
export function keepDaysSetting() {
return normalizeKeepDays(require('/server/lib/authConfig').authEnv('BACKUP_KEEP_DAYS'));
}
export { pruneLocalBackups } from './backupPrune';