From 53563b9720fee107676395b4669cda7f23a3b81a Mon Sep 17 00:00:00 2001 From: Chris Miles Date: Sat, 17 Jun 2023 18:16:29 -0500 Subject: [PATCH] [Backups] Move world database:dump to use MySQL credentials file (#3410) --- common/database/database_dump_service.cpp | 57 ++++++++++++++++++----- common/database/database_dump_service.h | 2 + 2 files changed, 48 insertions(+), 11 deletions(-) diff --git a/common/database/database_dump_service.cpp b/common/database/database_dump_service.cpp index 4d22f2fc8..4d81ed663 100644 --- a/common/database/database_dump_service.cpp +++ b/common/database/database_dump_service.cpp @@ -93,6 +93,8 @@ std::string DatabaseDumpService::GetMySQLVersion() return Strings::Trim(version_output); } +const std::string CREDENTIALS_FILE = "login.my.cnf"; + /** * @return */ @@ -101,21 +103,15 @@ std::string DatabaseDumpService::GetBaseMySQLDumpCommand() auto config = EQEmuConfig::get(); if (IsDumpContentTables() && !config->ContentDbHost.empty()) { return fmt::format( - "mysqldump -u {} -p{} -h {} --port={} {}", - config->ContentDbUsername, - config->ContentDbPassword, - config->ContentDbHost, - config->ContentDbPort, + "mysqldump --defaults-extra-file={} {}", + CREDENTIALS_FILE, config->ContentDbName ); }; return fmt::format( - "mysqldump -u {} -p{} -h {} --port={} {}", - config->DatabaseUsername, - config->DatabasePassword, - config->DatabaseHost, - config->DatabasePort, + "mysqldump --defaults-extra-file={} {}", + CREDENTIALS_FILE, config->DatabaseDB ); } @@ -321,6 +317,7 @@ void DatabaseDumpService::DatabaseDump() pipe_file ); + BuildCredentialsFile(); std::string execution_result = Process::execute(execute_command); if (!execution_result.empty() && IsDumpOutputToConsole()) { std::cout << execution_result; @@ -352,7 +349,6 @@ void DatabaseDumpService::DatabaseDump() } LogInfo("Database dump created at [{}.sql]", GetDumpFileNameWithPath()); - if (IsDumpWithCompression() && !IsDumpOutputToConsole()) { if (HasCompressionBinary()) { LogInfo("Compression requested. Compressing dump [{}.sql]", GetDumpFileNameWithPath()); @@ -389,6 +385,8 @@ void DatabaseDumpService::DatabaseDump() } } + RemoveCredentialsFile(); + // LogDebug("[{}] dump-to-console", IsDumpOutputToConsole()); // LogDebug("[{}] dump-path", GetSetDumpPath()); // LogDebug("[{}] compression", (IsDumpWithCompression() ? "true" : "false")); @@ -568,4 +566,41 @@ void DatabaseDumpService::RemoveSqlBackup() if (File::Exists(file)) { std::filesystem::remove(file); } + + RemoveCredentialsFile(); +} + +void DatabaseDumpService::BuildCredentialsFile() +{ + auto config = EQEmuConfig::get(); + std::ofstream out(CREDENTIALS_FILE); + if (out.is_open()) { + if (IsDumpContentTables() && !config->ContentDbHost.empty()) { + out << "[mysqldump]" << std::endl; + out << "user=" << config->ContentDbUsername << std::endl; + out << "password=" << config->ContentDbPassword << std::endl; + out << "host=" << config->ContentDbHost << std::endl; + out << "port=" << config->ContentDbPort << std::endl; + out << "default-character-set=utf8" << std::endl; + } + else { + out << "[mysqldump]" << std::endl; + out << "user=" << config->DatabaseUsername << std::endl; + out << "password=" << config->DatabasePassword << std::endl; + out << "host=" << config->DatabaseHost << std::endl; + out << "port=" << config->DatabasePort << std::endl; + out << "default-character-set=utf8" << std::endl; + } + out.close(); + } + else { + LogError("Failed to open credentials file for writing"); + } +} + +void DatabaseDumpService::RemoveCredentialsFile() +{ + if (File::Exists(CREDENTIALS_FILE)) { + std::filesystem::remove(CREDENTIALS_FILE); + } } diff --git a/common/database/database_dump_service.h b/common/database/database_dump_service.h index c146ece03..e60c04d95 100644 --- a/common/database/database_dump_service.h +++ b/common/database/database_dump_service.h @@ -93,6 +93,8 @@ private: std::string GetSetDumpPath(); std::string GetQueryServTables(); void RemoveSqlBackup(); + void BuildCredentialsFile(); + void RemoveCredentialsFile(); };