backup: rename back backuplisting.js to backups.js

this was a transitional rename till I figured out how to split
it in backuptargets.js
This commit is contained in:
Girish Ramakrishnan
2025-07-25 01:34:29 +02:00
parent 3aafbd2ccb
commit 62017b3ff5
14 changed files with 146 additions and 145 deletions
+22 -22
View File
@@ -12,7 +12,7 @@ const apps = require('./apps.js'),
archives = require('./archives.js'),
assert = require('assert'),
backupFormat = require('./backupformat.js'),
backupListing = require('./backuplisting.js'),
backups = require('./backups.js'),
backupTargets = require('./backuptargets.js'),
constants = require('./constants.js'),
debug = require('debug')('box:backupcleaner'),
@@ -30,9 +30,9 @@ function applyBackupRetention(allBackups, retention, referencedBackupIds) {
const now = new Date();
for (const backup of allBackups) {
if (backup.state === backupListing.BACKUP_STATE_ERROR) {
if (backup.state === backups.BACKUP_STATE_ERROR) {
backup.discardReason = 'error';
} else if (backup.state === backupListing.BACKUP_STATE_CREATING) {
} else if (backup.state === backups.BACKUP_STATE_CREATING) {
if ((now - backup.creationTime) < 48*60*60*1000) backup.keepReason = 'creating';
else backup.discardReason = 'creating-too-long';
} else if (referencedBackupIds.includes(backup.id)) { // could also be in archives
@@ -71,7 +71,7 @@ function applyBackupRetention(allBackups, retention, referencedBackupIds) {
}
if (retention.keepLatest) {
const latestNormalBackup = allBackups.find(b => b.state === backupListing.BACKUP_STATE_NORMAL);
const latestNormalBackup = allBackups.find(b => b.state === backups.BACKUP_STATE_NORMAL);
if (latestNormalBackup && !latestNormalBackup.keepReason) latestNormalBackup.keepReason = 'latest';
}
@@ -105,7 +105,7 @@ async function removeBackup(target, backup, progressCallback) {
const [pruneError] = await safe(storage.api(target.provider).remove(target.config, path.dirname(backupFilePath)));
if (pruneError) debug(`removeBackup: unable to prune backup directory ${path.dirname(backupFilePath)}: ${pruneError.message}`);
const [delError] = await safe(backupListing.del(backup.id));
const [delError] = await safe(backups.del(backup.id));
if (delError) debug(`removeBackup: error removing ${backup.id} from database. %o`, delError);
else debug(`removeBackup: removed ${backup.remotePath}`);
}
@@ -121,7 +121,7 @@ async function cleanupAppBackups(target, referencedBackupIds, progressCallback)
const allAppIds = allApps.map(a => a.id);
// high number, try to get all app backups as we had a cloudron with over 100 apps with 4 daily backups for one month!
const appBackups = await backupListing.getByTypePaged(backupListing.BACKUP_TYPE_APP, 1, 100000);
const appBackups = await backups.getByTypePaged(backups.BACKUP_TYPE_APP, 1, 100000);
// collate the backups by app id. note that the app could already have been uninstalled
const appBackupsById = {};
@@ -157,7 +157,7 @@ async function cleanupMailBackups(target, referencedBackupIds, progressCallback)
const removedMailBackupPaths = [];
const mailBackups = await backupListing.getByTypePaged(backupListing.BACKUP_TYPE_MAIL, 1, 100000);
const mailBackups = await backups.getByTypePaged(backups.BACKUP_TYPE_MAIL, 1, 100000);
applyBackupRetention(mailBackups, Object.assign({ keepLatest: true }, target.retention), referencedBackupIds);
@@ -182,7 +182,7 @@ async function cleanupBoxBackups(target, progressCallback) {
// We need to fetch all box backups to be able to compile a list of all referenced app backupTargets.
// Otherwise if we miss some app backups, they will get purged!
// 100000 here should be seen as infinity
const boxBackups = await backupListing.getByTypePaged(backupListing.BACKUP_TYPE_BOX, 1, 100000);
const boxBackups = await backups.getByTypePaged(backups.BACKUP_TYPE_BOX, 1, 100000);
applyBackupRetention(boxBackups, Object.assign({ keepLatest: true }, target.retention), [] /* references */);
@@ -215,10 +215,10 @@ async function cleanupMissingBackups(target, progressCallback) {
let page = 1, result = [];
do {
result = await backupListing.list(page, perPage);
result = await backups.list(page, perPage);
for (const backup of result) {
if (backup.state !== backupListing.BACKUP_STATE_NORMAL) continue; // note: errored and incomplete backups are cleaned up by the backup retention logic
if (backup.state !== backups.BACKUP_STATE_NORMAL) continue; // note: errored and incomplete backups are cleaned up by the backup retention logic
let backupFilePath = backupFormat.api(target.format).getBackupFilePath(target, backup.remotePath);
if (target.format === 'rsync') backupFilePath = backupFilePath + '/'; // add trailing slash to indicate directory
@@ -228,7 +228,7 @@ async function cleanupMissingBackups(target, progressCallback) {
await progressCallback({ message: `Removing missing backup ${backup.remotePath}`});
const [delError] = await safe(backupListing.del(backup.id));
const [delError] = await safe(backups.del(backup.id));
if (delError) debug(`cleanupMissingBackups: error removing ${backup.id} from database. %o`, delError);
missingBackupPaths.push(backup.remotePath);
@@ -278,38 +278,38 @@ async function run(targetId, progressCallback) {
assert.strictEqual(typeof targetId, 'string');
assert.strictEqual(typeof progressCallback, 'function');
const target = await backupTargets.get(targetId);
if (!target) throw new BoxError(BoxError.EXTERNAL_ERROR, 'Target not found');
const backupTarget = await backupTargets.get(targetId);
if (!backupTarget) throw new BoxError(BoxError.EXTERNAL_ERROR, 'Target not found');
debug(`run: retention is ${JSON.stringify(target.retention)}`);
debug(`run: retention is ${JSON.stringify(backupTarget.retention)}`);
const status = await backupTargets.ensureMounted(target);
const status = await backupTargets.ensureMounted(backupTarget);
debug(`run: mount point status is ${JSON.stringify(status)}`);
if (status.state !== 'active') throw new BoxError(BoxError.MOUNT_ERROR, `Backup endpoint is not mounted: ${status.message}`);
if (target.retention.keepWithinSecs < 0) {
if (backupTarget.retention.keepWithinSecs < 0) {
debug('run: keeping all backups');
return {};
}
await progressCallback({ percent: 10, message: 'Cleaning box backups' });
const { removedBoxBackupPaths, referencedBackupIds } = await cleanupBoxBackups(target, progressCallback); // references is app or mail backup ids
const { removedBoxBackupPaths, referencedBackupIds } = await cleanupBoxBackups(backupTarget, progressCallback); // references is app or mail backup ids
await progressCallback({ percent: 20, message: 'Cleaning mail backups' });
const removedMailBackupPaths = await cleanupMailBackups(target, referencedBackupIds, progressCallback);
const removedMailBackupPaths = await cleanupMailBackups(backupTarget, referencedBackupIds, progressCallback);
await progressCallback({ percent: 40, message: 'Cleaning app backups' });
const archivedBackupIds = await archives.listBackupIds();
const removedAppBackupPaths = await cleanupAppBackups(target, referencedBackupIds.concat(archivedBackupIds), progressCallback);
const removedAppBackupPaths = await cleanupAppBackups(backupTarget, referencedBackupIds.concat(archivedBackupIds), progressCallback);
await progressCallback({ percent: 70, message: 'Checking storage backend and removing stale entries in database' });
const missingBackupPaths = await cleanupMissingBackups(target, progressCallback);
const missingBackupPaths = await cleanupMissingBackups(backupTarget, progressCallback);
await progressCallback({ percent: 80, message: 'Cleaning snapshots' });
await cleanupSnapshots(target);
await cleanupSnapshots(backupTarget);
await progressCallback({ percent: 80, message: 'Cleaning storage artifacts' });
await storage.api(target.provider).cleanup(target.config, progressCallback);
await storage.api(backupTarget.provider).cleanup(backupTarget.config, progressCallback);
return { removedBoxBackupPaths, removedMailBackupPaths, removedAppBackupPaths, missingBackupPaths };
}