safer backup to file using VACUUM INTO + possibility to explicitly ask for backup now

This commit is contained in:
zadam
2020-05-29 21:55:08 +02:00
parent 1911d64c1c
commit 13f9d037dc
5 changed files with 68 additions and 13 deletions

View File

@@ -29,13 +29,38 @@ async function periodBackup(optionName, fileName, periodInSeconds) {
}
async function backupNow(name) {
const sql = require('./sql');
// we don't want to backup DB in the middle of sync with potentially inconsistent DB state
await syncMutexService.doExclusively(async () => {
return await syncMutexService.doExclusively(async () => {
const backupFile = `${dataDir.BACKUP_DIR}/backup-${name}.db`;
fs.copySync(dataDir.DOCUMENT_PATH, backupFile);
try {
fs.unlinkSync(backupFile);
}
catch (e) {} // unlink throws exception if the file did not exist
log.info("Created backup at " + backupFile);
let success = false;
let attemptCount = 0
for (; attemptCount < 50 && !success; attemptCount++) {
try {
await sql.executeNoWrap(`VACUUM INTO '${backupFile}'`);
success++;
}
catch (e) {}
// 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
}
if (attemptCount === 10) {
log.error(`Creating backup ${backupFile} failed`);
}
else {
log.info("Created backup at " + backupFile);
}
return backupFile;
});
}
@@ -52,4 +77,4 @@ sqlInit.dbReady.then(() => {
module.exports = {
backupNow
};
};