2017-10-21 21:10:33 -04:00
|
|
|
"use strict";
|
|
|
|
|
2018-04-02 20:46:46 -04:00
|
|
|
const dateUtils = require('./date_utils');
|
2018-04-01 21:27:46 -04:00
|
|
|
const optionService = require('./options');
|
2017-10-14 23:31:44 -04:00
|
|
|
const fs = require('fs-extra');
|
2017-10-23 23:30:23 -04:00
|
|
|
const dataDir = require('./data_dir');
|
2017-10-24 22:17:48 -04:00
|
|
|
const log = require('./log');
|
2018-04-02 21:25:20 -04:00
|
|
|
const sqlInit = require('./sql_init');
|
2018-04-01 21:27:46 -04:00
|
|
|
const syncMutexService = require('./sync_mutex');
|
2018-03-28 23:41:22 -04:00
|
|
|
const cls = require('./cls');
|
2017-10-14 23:31:44 -04:00
|
|
|
|
|
|
|
async function regularBackup() {
|
2018-07-30 16:40:50 +02:00
|
|
|
await periodBackup('lastDailyBackupDate', 'daily', 24 * 3600);
|
|
|
|
|
|
|
|
await periodBackup('lastWeeklyBackupDate', 'weekly', 7 * 24 * 3600);
|
|
|
|
|
|
|
|
await periodBackup('lastMonthlyBackupDate', 'monthly', 30 * 24 * 3600);
|
|
|
|
}
|
|
|
|
|
|
|
|
async function periodBackup(optionName, fileName, periodInSeconds) {
|
2017-12-10 12:56:59 -05:00
|
|
|
const now = new Date();
|
2018-07-30 16:40:50 +02:00
|
|
|
const lastDailyBackupDate = dateUtils.parseDateTime(await optionService.getOption(optionName));
|
2017-10-14 23:31:44 -04:00
|
|
|
|
2018-07-30 16:40:50 +02:00
|
|
|
if (now.getTime() - lastDailyBackupDate.getTime() > periodInSeconds * 1000) {
|
|
|
|
await backupNow(fileName);
|
2017-12-10 12:56:59 -05:00
|
|
|
|
2019-03-13 22:43:59 +01:00
|
|
|
await optionService.setOption(optionName, dateUtils.utcNowDateTime());
|
2017-10-14 23:31:44 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-31 10:24:59 +02:00
|
|
|
const BACKUP_ATTEMPT_COUNT = 50;
|
|
|
|
|
2018-07-30 16:40:50 +02:00
|
|
|
async function backupNow(name) {
|
2020-05-29 21:55:08 +02:00
|
|
|
const sql = require('./sql');
|
|
|
|
|
2018-01-04 21:37:36 -05:00
|
|
|
// we don't want to backup DB in the middle of sync with potentially inconsistent DB state
|
2020-05-29 21:55:08 +02:00
|
|
|
return await syncMutexService.doExclusively(async () => {
|
2018-07-30 16:40:50 +02:00
|
|
|
const backupFile = `${dataDir.BACKUP_DIR}/backup-${name}.db`;
|
2017-10-24 22:17:48 -04:00
|
|
|
|
2020-05-29 21:55:08 +02:00
|
|
|
try {
|
|
|
|
fs.unlinkSync(backupFile);
|
|
|
|
}
|
|
|
|
catch (e) {} // unlink throws exception if the file did not exist
|
|
|
|
|
|
|
|
let success = false;
|
|
|
|
let attemptCount = 0
|
|
|
|
|
2020-05-31 10:24:59 +02:00
|
|
|
for (; attemptCount < BACKUP_ATTEMPT_COUNT && !success; attemptCount++) {
|
2020-05-29 21:55:08 +02:00
|
|
|
try {
|
|
|
|
await sql.executeNoWrap(`VACUUM INTO '${backupFile}'`);
|
|
|
|
success++;
|
|
|
|
}
|
2020-05-31 10:24:59 +02:00
|
|
|
catch (e) {
|
|
|
|
log.info(`Backup attempt ${attemptCount + 1} failed with "${e.message}", retrying...`);
|
|
|
|
}
|
2020-05-29 21:55:08 +02:00
|
|
|
// we re-try since VACUUM is very picky and it can't run if there's any other query currently running
|
|
|
|
// which is difficult to guarantee so we just re-try
|
|
|
|
}
|
|
|
|
|
2020-05-31 10:24:59 +02:00
|
|
|
if (attemptCount === BACKUP_ATTEMPT_COUNT) {
|
2020-05-29 21:55:08 +02:00
|
|
|
log.error(`Creating backup ${backupFile} failed`);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
log.info("Created backup at " + backupFile);
|
|
|
|
}
|
2017-10-14 23:31:44 -04:00
|
|
|
|
2020-05-29 21:55:08 +02:00
|
|
|
return backupFile;
|
2017-10-14 23:31:44 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-10-28 12:23:11 -04:00
|
|
|
if (!fs.existsSync(dataDir.BACKUP_DIR)) {
|
|
|
|
fs.mkdirSync(dataDir.BACKUP_DIR, 0o700);
|
|
|
|
}
|
|
|
|
|
2018-04-02 21:25:20 -04:00
|
|
|
sqlInit.dbReady.then(() => {
|
2020-05-31 10:24:59 +02:00
|
|
|
setInterval(cls.wrap(regularBackup), 4 * 60 * 60 * 1000);
|
2017-10-28 12:23:11 -04:00
|
|
|
|
2020-05-31 10:24:59 +02:00
|
|
|
// kickoff first backup soon after start up
|
|
|
|
setTimeout(cls.wrap(regularBackup), 5 * 60 * 1000);
|
2017-12-03 19:18:33 -05:00
|
|
|
});
|
2017-10-28 12:23:11 -04:00
|
|
|
|
2017-10-14 23:31:44 -04:00
|
|
|
module.exports = {
|
|
|
|
backupNow
|
2020-05-29 21:55:08 +02:00
|
|
|
};
|