From c677f2e70627098f01bc8672c5f802e6bf00301b Mon Sep 17 00:00:00 2001 From: Eduardo Dantas Date: Fri, 13 Dec 2024 14:55:50 -0300 Subject: [PATCH] fix: database backup on shutdown --- src/canary_server.cpp | 3 +- src/database/database.cpp | 44 +++++++++++++--------------- src/game/scheduling/save_manager.cpp | 2 -- 3 files changed, 21 insertions(+), 28 deletions(-) diff --git a/src/canary_server.cpp b/src/canary_server.cpp index 9f9ffc29359..492028093ed 100644 --- a/src/canary_server.cpp +++ b/src/canary_server.cpp @@ -316,8 +316,6 @@ void CanaryServer::initializeDatabase() { )); } - g_database().createDatabaseBackup(false); - DatabaseManager::updateDatabase(); if (g_configManager().getBoolean(OPTIMIZE_DATABASE) @@ -392,6 +390,7 @@ void CanaryServer::modulesLoadHelper(bool loaded, std::string moduleName) { } void CanaryServer::shutdown() { + g_database().createDatabaseBackup(true); g_dispatcher().shutdown(); g_metrics().shutdown(); inject().shutdown(); diff --git a/src/database/database.cpp b/src/database/database.cpp index cb9ec58df6b..a6e732694b1 100644 --- a/src/database/database.cpp +++ b/src/database/database.cpp @@ -108,35 +108,31 @@ void Database::createDatabaseBackup(bool compress) const { // Compress the backup file if requested std::string compressedFileName; - if (compress) { - compressedFileName = backupFileName + ".gz"; - gzFile gzFile = gzopen(compressedFileName.c_str(), "wb9"); - if (!gzFile) { - g_logger().error("Failed to open gzip file for compression."); - return; - } - - std::ifstream backupFile(backupFileName, std::ios::binary); - if (!backupFile.is_open()) { - g_logger().error("Failed to open backup file for compression: {}", backupFileName); - gzclose(gzFile); - return; - } - - std::string buffer(8192, '\0'); - while (backupFile.read(&buffer[0], buffer.size()) || backupFile.gcount() > 0) { - gzwrite(gzFile, buffer.data(), backupFile.gcount()); - } + compressedFileName = backupFileName + ".gz"; + gzFile gzFile = gzopen(compressedFileName.c_str(), "wb9"); + if (!gzFile) { + g_logger().error("Failed to open gzip file for compression."); + return; + } - backupFile.close(); + std::ifstream backupFile(backupFileName, std::ios::binary); + if (!backupFile.is_open()) { + g_logger().error("Failed to open backup file for compression: {}", backupFileName); gzclose(gzFile); - std::filesystem::remove(backupFileName); + return; + } - g_logger().info("Database backup successfully compressed to: {}", compressedFileName); - } else { - g_logger().info("Database backup successfully created at: {}", backupFileName); + std::string buffer(8192, '\0'); + while (backupFile.read(&buffer[0], buffer.size()) || backupFile.gcount() > 0) { + gzwrite(gzFile, buffer.data(), backupFile.gcount()); } + backupFile.close(); + gzclose(gzFile); + std::filesystem::remove(backupFileName); + + g_logger().info("Database backup successfully compressed to: {}", compressedFileName); + // Delete backups older than 7 days auto nowTime = std::chrono::system_clock::now(); auto sevenDaysAgo = nowTime - std::chrono::hours(7 * 24); // 7 days in hours diff --git a/src/game/scheduling/save_manager.cpp b/src/game/scheduling/save_manager.cpp index 0479e3f779e..45cd9392194 100644 --- a/src/game/scheduling/save_manager.cpp +++ b/src/game/scheduling/save_manager.cpp @@ -26,8 +26,6 @@ SaveManager &SaveManager::getInstance() { } void SaveManager::saveAll() { - g_database().createDatabaseBackup(true); - Benchmark bm_saveAll; logger.info("Saving server..."); const auto players = game.getPlayers();