From e4e8744269b4c322323751da66eff9220ddd92e1 Mon Sep 17 00:00:00 2001 From: Tor Andersson Date: Fri, 5 May 2023 12:18:00 +0200 Subject: Trigger backup from main database process. Sqlite backups from the same database connection are much more efficient! Externally triggered backups restart every time the database changes during a backup, or need to lock the database exclusively for a long time. We periodically look for a "backup.request" file, and if it exists will write a backup. We write to "backup.tmp" first, and when finished rename the file to "backup.db". A crontab job can touch the "backup.request" file and then wait for the "backup.db" file to appear. --- server.js | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/server.js b/server.js index af2fc65..859a409 100644 --- a/server.js +++ b/server.js @@ -2447,3 +2447,28 @@ app.get('/user-stats/:who_name', function (req, res) { return res.status(404).send("Invalid user name.") } }) + +function backup_run() { + let start = Date.now() + console.log("BACKUP STARTED") + db.backup("backup.tmp") + .then(() => { + fs.renameSync("backup.tmp", "backup.db") + console.log("BACKUP FINISHED", Date.now() - start + "ms") + }) + .catch((err) => { + console.log("BACKUP FAILED", err) + }) +} + +function backup_heartbeat() { + try { + fs.accessSync("backup.request", fs.constants.R_OK) + fs.unlinkSync("backup.request") + backup_run() + } catch (err) { + // no file exists! + } +} + +setInterval(backup_heartbeat, 60 * 1000) -- cgit v1.2.3