feat(backup): add containerarchive-backed backup storage, restore, download, and pruning support
This commit is contained in:
@@ -59,6 +59,15 @@ export class BackupScheduler {
|
||||
await this.registerTask(schedule);
|
||||
}
|
||||
|
||||
// Add periodic archive prune task (runs daily at 3 AM)
|
||||
const pruneTask = new plugins.taskbuffer.Task({
|
||||
name: 'backup-archive-prune',
|
||||
taskFunction: async () => {
|
||||
await this.pruneArchive();
|
||||
},
|
||||
});
|
||||
this.taskManager.addAndScheduleTask(pruneTask, '0 3 * * *');
|
||||
|
||||
// Start the task manager (activates cron scheduling)
|
||||
await this.taskManager.start();
|
||||
|
||||
@@ -436,9 +445,11 @@ export class BackupScheduler {
|
||||
if (!toKeep.has(backup.id!)) {
|
||||
try {
|
||||
await this.oneboxRef.backupManager.deleteBackup(backup.id!);
|
||||
logger.info(`Deleted backup ${backup.filename} (retention policy)`);
|
||||
const backupRef = backup.snapshotId || backup.filename;
|
||||
logger.info(`Deleted backup ${backupRef} (retention policy)`);
|
||||
} catch (error) {
|
||||
logger.warn(`Failed to delete old backup ${backup.filename}: ${getErrorMessage(error)}`);
|
||||
const backupRef = backup.snapshotId || backup.filename;
|
||||
logger.warn(`Failed to delete old backup ${backupRef}: ${getErrorMessage(error)}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -647,4 +658,48 @@ export class BackupScheduler {
|
||||
private getRetentionDescription(retention: IRetentionPolicy): string {
|
||||
return `H:${retention.hourly} D:${retention.daily} W:${retention.weekly} M:${retention.monthly}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Prune the containerarchive repository to reclaim storage.
|
||||
* Uses the most generous retention policy across all schedules.
|
||||
*/
|
||||
private async pruneArchive(): Promise<void> {
|
||||
const archive = this.oneboxRef.backupManager.archive;
|
||||
if (!archive) return;
|
||||
|
||||
try {
|
||||
// Compute the most generous retention across all schedules
|
||||
const schedules = this.oneboxRef.database.getAllBackupSchedules();
|
||||
|
||||
// Default minimums if no schedules exist
|
||||
let maxDays = 7;
|
||||
let maxWeeks = 4;
|
||||
let maxMonths = 12;
|
||||
|
||||
for (const schedule of schedules) {
|
||||
if (schedule.retention.daily > maxDays) maxDays = schedule.retention.daily;
|
||||
if (schedule.retention.weekly > maxWeeks) maxWeeks = schedule.retention.weekly;
|
||||
if (schedule.retention.monthly > maxMonths) maxMonths = schedule.retention.monthly;
|
||||
}
|
||||
|
||||
const result = await archive.prune(
|
||||
{
|
||||
keepDays: maxDays,
|
||||
keepWeeks: maxWeeks,
|
||||
keepMonths: maxMonths,
|
||||
},
|
||||
false, // not dry run
|
||||
);
|
||||
|
||||
if (result.removedSnapshots > 0 || result.freedBytes > 0) {
|
||||
const freedMB = Math.round(result.freedBytes / (1024 * 1024) * 10) / 10;
|
||||
logger.info(
|
||||
`Archive prune: removed ${result.removedSnapshots} snapshot(s), ` +
|
||||
`${result.removedPacks} pack(s), freed ${freedMB} MB`
|
||||
);
|
||||
}
|
||||
} catch (error) {
|
||||
logger.warn(`Archive prune failed: ${getErrorMessage(error)}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user