From 1af6d33fcd9621f258eb72938e86e438ab4dadd5 Mon Sep 17 00:00:00 2001 From: Louis Lam Date: Tue, 28 Jun 2022 22:11:59 +0800 Subject: [PATCH] Make sure the backup database process is actually created backup files. Improve https://github.com/louislam/uptime-kuma/issues/1412#issuecomment-1166576395 --- server/database.js | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/server/database.js b/server/database.js index c5650808..060ac886 100644 --- a/server/database.js +++ b/server/database.js @@ -177,7 +177,13 @@ class Database { } else { log.info("db", "Database patch is needed"); - this.backup(version); + try { + this.backup(version); + } catch (e) { + log.error("db", e); + log.error("db", "Unable to create a backup before patching the database. Please make sure you have enough space and permission."); + process.exit(1); + } // Try catch anything here, if gone wrong, restore the backup try { @@ -445,6 +451,23 @@ class Database { this.backupWalPath = walPath + ".bak" + version; fs.copyFileSync(walPath, this.backupWalPath); } + + // Double confirm if all files actually backup + if (!fs.existsSync(this.backupPath)) { + throw new Error("Backup failed! " + this.backupPath); + } + + if (fs.existsSync(shmPath)) { + if (!fs.existsSync(this.backupShmPath)) { + throw new Error("Backup failed! " + this.backupShmPath); + } + } + + if (fs.existsSync(walPath)) { + if (!fs.existsSync(this.backupWalPath)) { + throw new Error("Backup failed! " + this.backupWalPath); + } + } } }