Files
wekan/server/lib/backupPrune.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

37 lines
1.5 KiB
JavaScript

'use strict';
// BACKUP_KEEP_DAYS on disk. models/lib/backupRuns.js decides WHICH backups go
// (same scope, older than N days, never the newest); this removes them, and
// only ever a directory inside the backup root that holds a backup.zip.
// Plain CommonJS so tests/backupRuns.test.cjs runs it against a real directory.
const fs = require('fs');
const path = require('path');
const { backupsToPrune } = require('../../models/lib/backupRuns');
function pruneLocalBackups({ backups, backupRoot, keepDays, orgId = null, now = new Date(), log = console }) {
const doomed = backupsToPrune({ backups, keepDays, orgId, now });
if (!doomed.length) return 0;
let root;
try { root = fs.realpathSync(backupRoot); } catch (_) { return 0; }
let removed = 0;
for (const b of doomed) {
try {
const dir = fs.realpathSync(path.dirname(b.path));
if (!dir.startsWith(root + path.sep) || !fs.existsSync(path.join(dir, 'backup.zip'))) continue;
fs.rmSync(dir, { recursive: true, force: true });
removed += 1;
// Tidy the now-empty date directories above it, never the root itself.
let parent = path.dirname(dir);
while (parent.startsWith(root + path.sep) && fs.readdirSync(parent).length === 0) {
fs.rmdirSync(parent);
parent = path.dirname(parent);
}
} catch (e) {
log.error('[backup] could not delete old backup', b.path, e && e.message);
}
}
if (removed) log.log(`[backup] BACKUP_KEEP_DAYS=${keepDays}: deleted ${removed} old backup(s)`);
return removed;
}
module.exports = { pruneLocalBackups };