Skip to content

Commit

Permalink
fix: database backup on shutdown
Browse files Browse the repository at this point in the history
  • Loading branch information
dudantas committed Dec 13, 2024
1 parent 6b41bc6 commit c677f2e
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 28 deletions.
3 changes: 1 addition & 2 deletions src/canary_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -316,8 +316,6 @@ void CanaryServer::initializeDatabase() {
));
}

g_database().createDatabaseBackup(false);

DatabaseManager::updateDatabase();

if (g_configManager().getBoolean(OPTIMIZE_DATABASE)
Expand Down Expand Up @@ -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<ThreadPool>().shutdown();
Expand Down
44 changes: 20 additions & 24 deletions src/database/database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions src/game/scheduling/save_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit c677f2e

Please sign in to comment.