diff --git a/src/cron.js b/src/cron.js index c6b19dca7..f27b932d6 100644 --- a/src/cron.js +++ b/src/cron.js @@ -25,6 +25,7 @@ var gAutoupdaterJob = null, gHeartbeatJob = null, gBackupJob = null, gCleanupTokensJob = null, + gCleanupBackupsJob = null, gDockerVolumeCleanerJob = null, gSchedulerSyncJob = null, gCertificateRenewJob = null, @@ -106,6 +107,14 @@ function recreateJobs(unusedTimeZone, callback) { timeZone: allSettings[settings.TIME_ZONE_KEY] }); + if (gCleanupBackupsJob) gCleanupBackupsJob.stop(); + gCleanupBackupsJob = new CronJob({ + cronTime: '00 */30 * * * *', // every 30 minutes + onTick: janitor.cleanupBackups, + start: true, + timeZone: allSettings[settings.TIME_ZONE_KEY] + }); + if (gCleanupEventlogJob) gCleanupEventlogJob.stop(); gCleanupEventlogJob = new CronJob({ cronTime: '00 */30 * * * *', // every 30 minutes @@ -204,6 +213,9 @@ function uninitialize(callback) { if (gCleanupTokensJob) gCleanupTokensJob.stop(); gCleanupTokensJob = null; + if (gCleanupBackupsJob) gCleanupBackupsJob.stop(); + gCleanupBackupsJob = null; + if (gCleanupEventlogJob) gCleanupEventlogJob.stop(); gCleanupEventlogJob = null; diff --git a/src/janitor.js b/src/janitor.js index ff9e51758..e6880b039 100644 --- a/src/janitor.js +++ b/src/janitor.js @@ -3,13 +3,16 @@ var assert = require('assert'), async = require('async'), authcodedb = require('./authcodedb.js'), + backups = require('./backups.js'), debug = require('debug')('box:src/janitor'), docker = require('./docker.js').connection, + settings = require('./settings.js'), tokendb = require('./tokendb.js'); exports = module.exports = { cleanupTokens: cleanupTokens, - cleanupDockerVolumes: cleanupDockerVolumes + cleanupDockerVolumes: cleanupDockerVolumes, + cleanupBackups: cleanupBackups }; var NOOP_CALLBACK = function () { }; @@ -101,3 +104,26 @@ function cleanupDockerVolumes(callback) { }, callback); }); } + +function cleanupBackups(callback) { + assert(!callback || typeof callback === 'function'); // callback is null when called from cronjob + + callback = callback || NOOP_CALLBACK; + + debug('Cleaning backups'); + + settings.getBackupConfig(function (error, backupConfig) { + if (error) return callback(error); + + // nothing to do here + if (backupConfig.provider !== 'filesystem') return callback(); + + backups.getPaged(1, 1000, function (error, result) { + if (error) return callback(error); + + debug('Current backups:', result); + + callback(); + }); + }); +}