diff options
author | Tor Andersson <tor@ccxvii.net> | 2023-05-05 12:18:00 +0200 |
---|---|---|
committer | Tor Andersson <tor@ccxvii.net> | 2023-05-05 13:49:00 +0200 |
commit | e4e8744269b4c322323751da66eff9220ddd92e1 (patch) | |
tree | 805bc98c6615585d3939eecb7eae688ccdf3695e /server.js | |
parent | c4962601eb88ca3a51c0f7ba31cc2bd251ee9038 (diff) | |
download | server-e4e8744269b4c322323751da66eff9220ddd92e1.tar.gz |
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.
Diffstat (limited to 'server.js')
-rw-r--r-- | server.js | 25 |
1 files changed, 25 insertions, 0 deletions
@@ -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) |