|
|
|
@ -199,15 +199,7 @@ class Database {
|
|
|
|
|
} else {
|
|
|
|
|
log.info("db", "Database patch is needed");
|
|
|
|
|
|
|
|
|
|
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 catch anything here
|
|
|
|
|
try {
|
|
|
|
|
for (let i = version + 1; i <= this.latestVersion; i++) {
|
|
|
|
|
const sqlFile = `./db/patch${i}.sql`;
|
|
|
|
@ -223,7 +215,6 @@ class Database {
|
|
|
|
|
log.error("db", "Start Uptime-Kuma failed due to issue patching the database");
|
|
|
|
|
log.error("db", "Please submit a bug report if you still encounter the problem after restart: https://github.com/louislam/uptime-kuma/issues");
|
|
|
|
|
|
|
|
|
|
this.restore();
|
|
|
|
|
process.exit(1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -265,8 +256,6 @@ class Database {
|
|
|
|
|
log.error("db", "Start Uptime-Kuma failed due to issue patching the database");
|
|
|
|
|
log.error("db", "Please submit the bug report if you still encounter the problem after restart: https://github.com/louislam/uptime-kuma/issues");
|
|
|
|
|
|
|
|
|
|
this.restore();
|
|
|
|
|
|
|
|
|
|
process.exit(1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -451,100 +440,6 @@ class Database {
|
|
|
|
|
process.removeListener("unhandledRejection", listener);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* One backup one time in this process.
|
|
|
|
|
* Reset this.backupPath if you want to backup again
|
|
|
|
|
* @param {string} version Version code of backup
|
|
|
|
|
*/
|
|
|
|
|
static backup(version) {
|
|
|
|
|
if (! this.backupPath) {
|
|
|
|
|
log.info("db", "Backing up the database");
|
|
|
|
|
this.backupPath = this.dataDir + "kuma.db.bak" + version;
|
|
|
|
|
fs.copyFileSync(Database.path, this.backupPath);
|
|
|
|
|
|
|
|
|
|
const shmPath = Database.path + "-shm";
|
|
|
|
|
if (fs.existsSync(shmPath)) {
|
|
|
|
|
this.backupShmPath = shmPath + ".bak" + version;
|
|
|
|
|
fs.copyFileSync(shmPath, this.backupShmPath);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const walPath = Database.path + "-wal";
|
|
|
|
|
if (fs.existsSync(walPath)) {
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Restore from most recent backup */
|
|
|
|
|
static restore() {
|
|
|
|
|
if (this.backupPath) {
|
|
|
|
|
log.error("db", "Patching the database failed!!! Restoring the backup");
|
|
|
|
|
|
|
|
|
|
const shmPath = Database.path + "-shm";
|
|
|
|
|
const walPath = Database.path + "-wal";
|
|
|
|
|
|
|
|
|
|
// Make sure we have a backup to restore before deleting old db
|
|
|
|
|
if (
|
|
|
|
|
!fs.existsSync(this.backupPath)
|
|
|
|
|
&& !fs.existsSync(shmPath)
|
|
|
|
|
&& !fs.existsSync(walPath)
|
|
|
|
|
) {
|
|
|
|
|
log.error("db", "Backup file not found! Leaving database in failed state.");
|
|
|
|
|
process.exit(1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Delete patch failed db
|
|
|
|
|
try {
|
|
|
|
|
if (fs.existsSync(Database.path)) {
|
|
|
|
|
fs.unlinkSync(Database.path);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (fs.existsSync(shmPath)) {
|
|
|
|
|
fs.unlinkSync(shmPath);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (fs.existsSync(walPath)) {
|
|
|
|
|
fs.unlinkSync(walPath);
|
|
|
|
|
}
|
|
|
|
|
} catch (e) {
|
|
|
|
|
log.error("db", "Restore failed; you may need to restore the backup manually");
|
|
|
|
|
process.exit(1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Restore backup
|
|
|
|
|
fs.copyFileSync(this.backupPath, Database.path);
|
|
|
|
|
|
|
|
|
|
if (this.backupShmPath) {
|
|
|
|
|
fs.copyFileSync(this.backupShmPath, shmPath);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (this.backupWalPath) {
|
|
|
|
|
fs.copyFileSync(this.backupWalPath, walPath);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
log.info("db", "Nothing to restore");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Get the size of the database */
|
|
|
|
|
static getSize() {
|
|
|
|
|
log.debug("db", "Database.getSize()");
|
|
|
|
|