backups: split listing and targets

This commit is contained in:
Girish Ramakrishnan
2025-07-24 18:09:33 +02:00
parent bf315258c5
commit 5e456f378b
15 changed files with 357 additions and 317 deletions
+54 -53
View File
@@ -8,6 +8,7 @@
const archives = require('../archives.js'),
backupCleaner = require('../backupcleaner.js'),
backupListing = require('../backuplisting.js'),
backups = require('../backups.js'),
common = require('./common.js'),
expect = require('expect.js'),
@@ -27,8 +28,8 @@ describe('backup cleaner', function () {
remotePath: 'somepath',
encryptionVersion: 2,
packageVersion: '1.0.0',
type: backups.BACKUP_TYPE_BOX,
state: backups.BACKUP_STATE_NORMAL,
type: backupListing.BACKUP_TYPE_BOX,
state: backupListing.BACKUP_STATE_NORMAL,
identifier: 'box',
dependsOn: [ 'dep1' ],
manifest: null,
@@ -38,13 +39,13 @@ describe('backup cleaner', function () {
describe('retention', function () {
it('keeps latest', function () {
const backup = Object.assign({}, backupTemplate, { creationTime: moment().subtract(5, 's').toDate(), state: backups.BACKUP_STATE_NORMAL });
const backup = Object.assign({}, backupTemplate, { creationTime: moment().subtract(5, 's').toDate(), state: backupListing.BACKUP_STATE_NORMAL });
backupCleaner._applyBackupRetention([backup], { keepWithinSecs: 1, keepLatest: true }, []);
expect(backup.keepReason).to.be('latest');
});
it('does not keep latest', function () {
const backup = { creationTime: moment().subtract(5, 's').toDate(), state: backups.BACKUP_STATE_NORMAL };
const backup = { creationTime: moment().subtract(5, 's').toDate(), state: backupListing.BACKUP_STATE_NORMAL };
backupCleaner._applyBackupRetention([backup], { keepWithinSecs: 1, keepLatest: false }, []);
expect(backup.keepReason).to.be(undefined);
});
@@ -63,11 +64,11 @@ describe('backup cleaner', function () {
it('1 daily', function () {
const b = [
{ id: '0', state: backups.BACKUP_STATE_NORMAL, creationTime: moment().toDate() },
{ id: '1', state: backups.BACKUP_STATE_NORMAL, creationTime: moment().subtract(1, 'h').toDate() },
{ id: '2', state: backups.BACKUP_STATE_NORMAL, creationTime: moment().subtract(3, 'h').toDate() },
{ id: '3', state: backups.BACKUP_STATE_NORMAL, creationTime: moment().subtract(20, 'h').toDate() },
{ id: '4', state: backups.BACKUP_STATE_NORMAL, creationTime: moment().subtract(5, 'd').toDate() }
{ id: '0', state: backupListing.BACKUP_STATE_NORMAL, creationTime: moment().toDate() },
{ id: '1', state: backupListing.BACKUP_STATE_NORMAL, creationTime: moment().subtract(1, 'h').toDate() },
{ id: '2', state: backupListing.BACKUP_STATE_NORMAL, creationTime: moment().subtract(3, 'h').toDate() },
{ id: '3', state: backupListing.BACKUP_STATE_NORMAL, creationTime: moment().subtract(20, 'h').toDate() },
{ id: '4', state: backupListing.BACKUP_STATE_NORMAL, creationTime: moment().subtract(5, 'd').toDate() }
];
backupCleaner._applyBackupRetention(b, { keepDaily: 1, keepLatest: true }, []);
expect(b[0].keepReason).to.be('keepDaily');
@@ -80,13 +81,13 @@ describe('backup cleaner', function () {
// if you are debugging this test, it's because of some timezone issue with all the hour substraction!
it('2 daily, 1 weekly', function () {
const b = [
{ id: '0', state: backups.BACKUP_STATE_NORMAL, creationTime: moment().toDate() },
{ id: '1', state: backups.BACKUP_STATE_NORMAL, creationTime: moment().subtract(1, 'h').toDate() },
{ id: '2', state: backups.BACKUP_STATE_NORMAL, creationTime: moment().subtract(3, 'h').toDate() },
{ id: '3', state: backups.BACKUP_STATE_NORMAL, creationTime: moment().subtract(26, 'h').toDate() },
{ id: '4', state: backups.BACKUP_STATE_ERROR, creationTime: moment().subtract(32, 'h').toDate() },
{ id: '5', state: backups.BACKUP_STATE_CREATING, creationTime: moment().subtract(50, 'h').toDate() },
{ id: '6', state: backups.BACKUP_STATE_NORMAL, creationTime: moment().subtract(5, 'd').toDate() }
{ id: '0', state: backupListing.BACKUP_STATE_NORMAL, creationTime: moment().toDate() },
{ id: '1', state: backupListing.BACKUP_STATE_NORMAL, creationTime: moment().subtract(1, 'h').toDate() },
{ id: '2', state: backupListing.BACKUP_STATE_NORMAL, creationTime: moment().subtract(3, 'h').toDate() },
{ id: '3', state: backupListing.BACKUP_STATE_NORMAL, creationTime: moment().subtract(26, 'h').toDate() },
{ id: '4', state: backupListing.BACKUP_STATE_ERROR, creationTime: moment().subtract(32, 'h').toDate() },
{ id: '5', state: backupListing.BACKUP_STATE_CREATING, creationTime: moment().subtract(50, 'h').toDate() },
{ id: '6', state: backupListing.BACKUP_STATE_NORMAL, creationTime: moment().subtract(5, 'd').toDate() }
];
backupCleaner._applyBackupRetention(b, { keepDaily: 2, keepWeekly: 1, keepLatest: false }, []);
expect(b[0].keepReason).to.be('keepDaily'); // today
@@ -100,16 +101,16 @@ describe('backup cleaner', function () {
it('2 daily, 3 monthly, 1 yearly', function () {
const b = [
{ id: '0', state: backups.BACKUP_STATE_CREATING, creationTime: moment().toDate() },
{ id: '1', state: backups.BACKUP_STATE_ERROR, creationTime: moment().toDate() },
{ id: '2', state: backups.BACKUP_STATE_NORMAL, creationTime: moment().toDate() },
{ id: '3', state: backups.BACKUP_STATE_NORMAL, creationTime: moment().subtract(1, 'h').toDate() },
{ id: '4', state: backups.BACKUP_STATE_NORMAL, creationTime: moment().subtract(3, 'h').toDate() },
{ id: '5', state: backups.BACKUP_STATE_NORMAL, creationTime: moment().subtract(26, 'h').toDate() },
{ id: '6', state: backups.BACKUP_STATE_CREATING, creationTime: moment().subtract(49, 'h').toDate() },
{ id: '7', state: backups.BACKUP_STATE_NORMAL, creationTime: moment().subtract(51, 'd').toDate() },
{ id: '8', state: backups.BACKUP_STATE_NORMAL, creationTime: moment().subtract(84, 'd').toDate() },
{ id: '9', state: backups.BACKUP_STATE_NORMAL, creationTime: moment().subtract(97, 'd').toDate() },
{ id: '0', state: backupListing.BACKUP_STATE_CREATING, creationTime: moment().toDate() },
{ id: '1', state: backupListing.BACKUP_STATE_ERROR, creationTime: moment().toDate() },
{ id: '2', state: backupListing.BACKUP_STATE_NORMAL, creationTime: moment().toDate() },
{ id: '3', state: backupListing.BACKUP_STATE_NORMAL, creationTime: moment().subtract(1, 'h').toDate() },
{ id: '4', state: backupListing.BACKUP_STATE_NORMAL, creationTime: moment().subtract(3, 'h').toDate() },
{ id: '5', state: backupListing.BACKUP_STATE_NORMAL, creationTime: moment().subtract(26, 'h').toDate() },
{ id: '6', state: backupListing.BACKUP_STATE_CREATING, creationTime: moment().subtract(49, 'h').toDate() },
{ id: '7', state: backupListing.BACKUP_STATE_NORMAL, creationTime: moment().subtract(51, 'd').toDate() },
{ id: '8', state: backupListing.BACKUP_STATE_NORMAL, creationTime: moment().subtract(84, 'd').toDate() },
{ id: '9', state: backupListing.BACKUP_STATE_NORMAL, creationTime: moment().subtract(97, 'd').toDate() },
];
backupCleaner._applyBackupRetention(b, { keepDaily: 2, keepMonthly: 3, keepYearly: 1, keepLatest: true }, []);
expect(b[0].keepReason).to.be('creating');
@@ -132,8 +133,8 @@ describe('backup cleaner', function () {
identifier: 'box',
encryptionVersion: null,
packageVersion: '1.0.0',
type: backups.BACKUP_TYPE_BOX,
state: backups.BACKUP_STATE_NORMAL,
type: backupListing.BACKUP_TYPE_BOX,
state: backupListing.BACKUP_STATE_NORMAL,
dependsOn: [ 'backup-app-00', 'backup-app-01' ],
manifest: null,
preserveSecs: 0,
@@ -146,8 +147,8 @@ describe('backup cleaner', function () {
identifier: app.id,
encryptionVersion: null,
packageVersion: '1.0.0',
type: backups.BACKUP_TYPE_APP,
state: backups.BACKUP_STATE_NORMAL,
type: backupListing.BACKUP_TYPE_APP,
state: backupListing.BACKUP_STATE_NORMAL,
dependsOn: [],
manifest: null,
preserveSecs: 0,
@@ -160,8 +161,8 @@ describe('backup cleaner', function () {
identifier: 'app1',
encryptionVersion: null,
packageVersion: '1.0.0',
type: backups.BACKUP_TYPE_APP,
state: backups.BACKUP_STATE_NORMAL,
type: backupListing.BACKUP_TYPE_APP,
state: backupListing.BACKUP_STATE_NORMAL,
dependsOn: [],
manifest: null,
preserveSecs: 0,
@@ -173,8 +174,8 @@ describe('backup cleaner', function () {
remotePath: 'backup-box-1',
encryptionVersion: null,
packageVersion: '1.0.0',
type: backups.BACKUP_TYPE_BOX,
state: backups.BACKUP_STATE_NORMAL,
type: backupListing.BACKUP_TYPE_BOX,
state: backupListing.BACKUP_STATE_NORMAL,
identifier: 'box',
dependsOn: [ 'backup-app-10', 'backup-app-11' ],
manifest: null,
@@ -187,8 +188,8 @@ describe('backup cleaner', function () {
remotePath: 'backup-app-10',
encryptionVersion: null,
packageVersion: '1.0.0',
type: backups.BACKUP_TYPE_APP,
state: backups.BACKUP_STATE_NORMAL,
type: backupListing.BACKUP_TYPE_APP,
state: backupListing.BACKUP_STATE_NORMAL,
identifier: app.id,
dependsOn: [],
manifest: null,
@@ -201,8 +202,8 @@ describe('backup cleaner', function () {
remotePath: 'backup-app-11',
encryptionVersion: null,
packageVersion: '1.0.0',
type: backups.BACKUP_TYPE_APP,
state: backups.BACKUP_STATE_NORMAL,
type: backupListing.BACKUP_TYPE_APP,
state: backupListing.BACKUP_STATE_NORMAL,
identifier: 'app1',
dependsOn: [],
manifest: null,
@@ -215,8 +216,8 @@ describe('backup cleaner', function () {
remotePath: 'backup-app-2',
encryptionVersion: null,
packageVersion: '2.0.0',
type: backups.BACKUP_TYPE_APP,
state: backups.BACKUP_STATE_NORMAL,
type: backupListing.BACKUP_TYPE_APP,
state: backupListing.BACKUP_STATE_NORMAL,
identifier: 'app2',
dependsOn: [],
manifest: null,
@@ -256,56 +257,56 @@ describe('backup cleaner', function () {
});
it('add the backups', async function () {
BACKUP_0_APP_0.id = await backups.add(BACKUP_0_APP_0);
BACKUP_0_APP_1.id = await backups.add(BACKUP_0_APP_1);
BACKUP_0_APP_0.id = await backupListing.add(BACKUP_0_APP_0);
BACKUP_0_APP_1.id = await backupListing.add(BACKUP_0_APP_1);
BACKUP_0_BOX.dependsOn = [ BACKUP_0_APP_0.id, BACKUP_0_APP_1.id ];
BACKUP_0_BOX.id = await backups.add(BACKUP_0_BOX);
BACKUP_0_BOX.id = await backupListing.add(BACKUP_0_BOX);
await timers.setTimeout(2000); // space out backups
BACKUP_1_APP_0.id = await backups.add(BACKUP_1_APP_0);
BACKUP_1_APP_1.id = await backups.add(BACKUP_1_APP_1);
BACKUP_1_APP_0.id = await backupListing.add(BACKUP_1_APP_0);
BACKUP_1_APP_1.id = await backupListing.add(BACKUP_1_APP_1);
BACKUP_1_BOX.dependsOn = [ BACKUP_1_APP_0.id, BACKUP_1_APP_1.id ];
BACKUP_1_BOX.id = await backups.add(BACKUP_1_BOX);
BACKUP_1_BOX.id = await backupListing.add(BACKUP_1_BOX);
BACKUP_2_APP_2.id = await backups.add(BACKUP_2_APP_2);
BACKUP_2_APP_2.id = await backupListing.add(BACKUP_2_APP_2);
await archives.add(BACKUP_2_APP_2.id, {}, common.auditSource);
});
it('succeeds with box backups, keeps latest', async function () {
await cleanupBackups();
const results = await backups.getByTypePaged(backups.BACKUP_TYPE_BOX, 1, 1000);
const results = await backupListing.getByTypePaged(backupListing.BACKUP_TYPE_BOX, 1, 1000);
expect(results.length).to.equal(1);
expect(results[0].id).to.equal(BACKUP_1_BOX.id);
// check that app backups are gone as well. only backup_1 will remain
const result = await backups.get(BACKUP_0_APP_0.id);
const result = await backupListing.get(BACKUP_0_APP_0.id);
expect(result).to.be(null);
});
it('does not remove expired backups if only one left', async function () {
await cleanupBackups();
const results = await backups.getByTypePaged(backups.BACKUP_TYPE_BOX, 1, 1000);
const results = await backupListing.getByTypePaged(backupListing.BACKUP_TYPE_BOX, 1, 1000);
expect(results[0].id).to.equal(BACKUP_1_BOX.id);
// check that app backups are also still there. backup_1 is still there
const result = await backups.get(BACKUP_1_APP_0.id);
const result = await backupListing.get(BACKUP_1_APP_0.id);
expect(result.id).to.equal(BACKUP_1_APP_0.id);
});
it('succeeds for app backups not referenced by a box backup', async function () {
// add two dangling app backups not referenced by box backup. app1 is uninstalled. app0 is there
for (const backup of [BACKUP_0_APP_0, BACKUP_0_APP_1]) {
backup.id = await backups.add(backup);
backup.id = await backupListing.add(backup);
}
await timers.setTimeout(2000); // wait for expiration
await cleanupBackups();
let result = await backups.getByTypePaged(backups.BACKUP_TYPE_APP, 1, 1000);
let result = await backupListing.getByTypePaged(backupListing.BACKUP_TYPE_APP, 1, 1000);
expect(result.length).to.equal(4);
result = result.sort((r1, r2) => r1.remotePath.localeCompare(r2.remotePath));
expect(result[0].id).to.be(BACKUP_0_APP_0.id); // because app is installed, latest backup is preserved