Skip to content

Commit

Permalink
feat: create database backup
Browse files Browse the repository at this point in the history
  • Loading branch information
dudantas committed Nov 12, 2024
1 parent 3bef034 commit b453b80
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -395,5 +395,8 @@ canary.old
# VCPKG
vcpkg_installed

# DB Backups
database_backup

# CLION
cmake-build-*
1 change: 1 addition & 0 deletions config.lua.dist
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,7 @@ mysqlHost = "127.0.0.1"
mysqlUser = "root"
mysqlPass = "root"
mysqlDatabase = "otservbr-global"
mysqlDatabaseBackup = false
mysqlPort = 3306
mysqlSock = ""
passwordType = "sha1"
Expand Down
2 changes: 2 additions & 0 deletions src/canary_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,8 @@ void CanaryServer::initializeDatabase() {
));
}

g_database().createDatabaseBackup();

DatabaseManager::updateDatabase();

if (g_configManager().getBoolean(OPTIMIZE_DATABASE)
Expand Down
1 change: 1 addition & 0 deletions src/config/config_enums.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ enum ConfigKey_t : uint16_t {
MONTH_KILLS_TO_RED,
MULTIPLIER_ATTACKONFIST,
MYSQL_DB,
MYSQL_DB_BACKUP,
MYSQL_HOST,
MYSQL_PASS,
MYSQL_SOCK,
Expand Down
1 change: 1 addition & 0 deletions src/config/configmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ bool ConfigManager::load() {
loadStringConfig(L, MAP_DOWNLOAD_URL, "mapDownloadUrl", "");
loadStringConfig(L, MAP_NAME, "mapName", "canary");
loadStringConfig(L, MYSQL_DB, "mysqlDatabase", "canary");
loadBoolConfig(L, MYSQL_DB_BACKUP, "mysqlDatabaseBackup", false);
loadStringConfig(L, MYSQL_HOST, "mysqlHost", "127.0.0.1");
loadStringConfig(L, MYSQL_PASS, "mysqlPass", "");
loadStringConfig(L, MYSQL_SOCK, "mysqlSock", "");
Expand Down
48 changes: 48 additions & 0 deletions src/database/database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "config/configmanager.hpp"
#include "lib/di/container.hpp"
#include "lib/metrics/metrics.hpp"
#include "utils/tools.hpp"

Database::~Database() {
if (handle != nullptr) {
Expand Down Expand Up @@ -60,6 +61,53 @@ bool Database::connect(const std::string* host, const std::string* user, const s
return true;
}

void Database::createDatabaseBackup() const {
if (!g_configManager().getBoolean(MYSQL_DB_BACKUP)) {
return;
}

std::time_t now = getTimeNow();
std::string formattedTime = fmt::format("{:%Y-%m-%d_%H-%M-%S}", fmt::localtime(now));

if (formattedTime.empty()) {
g_logger().error("Failed to format time for database backup.");
return;
}

std::string backupDir = "database_backup/";
std::filesystem::create_directories(backupDir);
std::string backupFileName = fmt::format("{}/backup_{}.sql", backupDir, formattedTime);

std::string tempConfigFile = "database_backup.cnf";
std::ofstream configFile(tempConfigFile);
if (configFile.is_open()) {
configFile << "[client]\n";
configFile << "user=" << g_configManager().getString(MYSQL_USER) << "\n";
configFile << "password=" << g_configManager().getString(MYSQL_PASS) << "\n";
configFile << "host=" << g_configManager().getString(MYSQL_HOST) << "\n";
configFile << "port=" << g_configManager().getNumber(SQL_PORT) << "\n";
configFile.close();
} else {
g_logger().error("Failed to create temporary MySQL configuration file.");
return;
}

std::string command = fmt::format(
"mysqldump --defaults-extra-file={} {} > {}",
tempConfigFile, g_configManager().getString(MYSQL_DB), backupFileName
);

int result = std::system(command.c_str());

std::filesystem::remove(tempConfigFile);

if (result != 0) {
g_logger().error("Failed to create database backup using mysqldump.");
} else {
g_logger().info("Database backup successfully created at: {}", backupFileName);
}
}

bool Database::beginTransaction() {
if (!executeQuery("BEGIN")) {
return false;
Expand Down
1 change: 1 addition & 0 deletions src/database/database.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class Database {
bool connect();

bool connect(const std::string* host, const std::string* user, const std::string* password, const std::string* database, uint32_t port, const std::string* sock);
void createDatabaseBackup() const;

bool retryQuery(std::string_view query, int retries);
bool executeQuery(std::string_view query);
Expand Down

0 comments on commit b453b80

Please sign in to comment.