Skip to content

Commit

Permalink
fix: database backup on linux
Browse files Browse the repository at this point in the history
  • Loading branch information
dudantas authored Nov 27, 2024
1 parent 0fd7bf7 commit 08fc766
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions src/database/database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,23 +137,24 @@ void Database::createDatabaseBackup(bool compress) const {
g_logger().info("Database backup successfully created at: {}", backupFileName);
}

// Delete old backups
auto twentyFourHoursAgo = std::chrono::system_clock::now() - std::chrono::hours(24);
auto sevenDaysAgo = std::chrono::system_clock::now() - std::chrono::hours(24 * 7);
for (const auto &entry : std::filesystem::directory_iterator("database_backup")) {
// 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
for (const auto& entry : std::filesystem::directory_iterator("database_backup")) {
if (entry.is_directory()) {
try {
auto dirTime = std::filesystem::last_write_time(entry);
if (dirTime.time_since_epoch() < sevenDaysAgo.time_since_epoch()) {
// Instead of deleting the entire directory, delete only specific files
for (const auto &file : std::filesystem::directory_iterator(entry)) {
if (file.path().extension() == ".gz" || file.path().extension() == ".sql") {
for (const auto& file : std::filesystem::directory_iterator(entry)) {
if (file.path().extension() == ".gz") {
auto fileTime = std::filesystem::last_write_time(file);
auto fileTimeSystemClock = std::chrono::clock_cast<std::chrono::system_clock>(fileTime);

Check failure on line 149 in src/database/database.cpp

View workflow job for this annotation

GitHub Actions / ubuntu-22.04-linux-release

‘clock_cast’ is not a member of ‘std::chrono’

Check failure on line 149 in src/database/database.cpp

View workflow job for this annotation

GitHub Actions / ubuntu-22.04-linux-release

expected primary-expression before ‘>’ token

Check failure on line 149 in src/database/database.cpp

View workflow job for this annotation

GitHub Actions / ubuntu-22.04-linux-debug

‘clock_cast’ is not a member of ‘std::chrono’

Check failure on line 149 in src/database/database.cpp

View workflow job for this annotation

GitHub Actions / ubuntu-22.04-linux-debug

expected primary-expression before ‘>’ token

if (fileTimeSystemClock < sevenDaysAgo) {
std::filesystem::remove(file);
g_logger().info("Deleted old backup file: {}", file.path().string());
}
}
}
} catch (const std::filesystem::filesystem_error &e) {
} catch (const std::filesystem::filesystem_error& e) {
g_logger().error("Failed to check or delete files in backup directory: {}. Error: {}", entry.path().string(), e.what());
}
}
Expand Down

0 comments on commit 08fc766

Please sign in to comment.