diff --git a/src/janitor.js b/src/janitor.js index 9cf2d5f27..a0c596eca 100644 --- a/src/janitor.js +++ b/src/janitor.js @@ -115,16 +115,17 @@ function cleanupBackups(callback) { settings.getBackupConfig(function (error, backupConfig) { if (error) return callback(error); - // nothing to do here - if (backupConfig.provider !== 'filesystem') return callback(); + if (backupConfig.retentionSecs < 0) return callback(); backups.getPaged(1, 1000, function (error, result) { if (error) return callback(error); - // sort with latest backups first in the array and slice 2 - var toCleanup = result.sort(function (a, b) { return b.creationTime.getTime() - a.creationTime.getTime(); }).slice(2); + var now = new Date(); + var toCleanup = result.filter(function (a) { + return (now - a.creationTime) > (backupConfig.retentionSecs * 1000); + }); - debug('cleanupBackups: about to clean: ', toCleanup); + debug('cleanupBackups: about to clean: %j', toCleanup); async.each(toCleanup, function (backup, callback) { backups.removeBackup(backup.id, backup.dependsOn, function (error) { diff --git a/src/routes/settings.js b/src/routes/settings.js index 25308cdfd..d5950fe3c 100644 --- a/src/routes/settings.js +++ b/src/routes/settings.js @@ -191,6 +191,7 @@ function setBackupConfig(req, res, next) { assert.strictEqual(typeof req.body, 'object'); if (typeof req.body.provider !== 'string') return next(new HttpError(400, 'provider is required')); + if (typeof req.body.retentionSecs !== 'number') return next(new HttpError(400, 'retentionSecs is required')); if ('key' in req.body && typeof req.body.key !== 'string') return next(new HttpError(400, 'key must be a string')); settings.setBackupConfig(req.body, function (error) { diff --git a/webadmin/src/views/settings.html b/webadmin/src/views/settings.html index d72c0c599..99523bbe3 100644 --- a/webadmin/src/views/settings.html +++ b/webadmin/src/views/settings.html @@ -191,6 +191,11 @@ +