trilium/services/backup.js

65 lines
1.6 KiB
JavaScript
Raw Normal View History

2017-10-22 09:10:33 +08:00
"use strict";
const utils = require('./utils');
2017-11-03 08:48:02 +08:00
const options = require('./options');
const fs = require('fs-extra');
const dataDir = require('./data_dir');
2017-10-25 10:17:48 +08:00
const log = require('./log');
async function regularBackup() {
const now = utils.nowTimestamp();
2017-11-03 08:48:02 +08:00
const last_backup_date = parseInt(await options.getOption('last_backup_date'));
if (now - last_backup_date > 43200) {
await backupNow();
}
await cleanupOldBackups();
}
async function backupNow() {
const now = utils.nowTimestamp();
const date_str = new Date().toISOString().substr(0, 19).replace(/:/g, '');
2017-10-25 10:17:48 +08:00
const backupFile = dataDir.BACKUP_DIR + "/" + "backup-" + date_str + ".db";
fs.copySync(dataDir.DOCUMENT_PATH, backupFile);
log.info("Created backup at " + backupFile);
2017-11-03 08:48:02 +08:00
await options.setOption('last_backup_date', now);
}
async function cleanupOldBackups() {
const now = new Date();
fs.readdirSync(dataDir.BACKUP_DIR).forEach(file => {
const match = file.match(/backup-([0-9 -:]+)\.db/);
if (match) {
const date_str = match[1];
const date = Date.parse(date_str);
if (now.getTime() - date.getTime() > 30 * 24 * 3600 * 1000) {
2017-10-25 10:17:48 +08:00
log.info("Removing old backup - " + file);
fs.unlink(dataDir.BACKUP_DIR + "/" + file);
}
}
});
}
if (!fs.existsSync(dataDir.BACKUP_DIR)) {
fs.mkdirSync(dataDir.BACKUP_DIR, 0o700);
}
setInterval(regularBackup, 60 * 60 * 1000);
// kickoff backup immediately
setTimeout(regularBackup, 1000);
module.exports = {
backupNow
};