Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Request: Automatic Cleanup of Old Backups #224

Open
loftwah opened this issue Oct 25, 2024 · 0 comments
Open

Feature Request: Automatic Cleanup of Old Backups #224

loftwah opened this issue Oct 25, 2024 · 0 comments

Comments

@loftwah
Copy link
Owner

loftwah commented Oct 25, 2024

Currently, the BackupDatabaseJob successfully creates and uploads backups of the database but does not automatically delete old backups. Over time, this could lead to storage accumulation, especially if backups are retained indefinitely.

This feature request proposes the addition of an automatic cleanup mechanism that deletes old backups after a configurable period, ensuring efficient disk usage and preventing backup directory bloat.

Proposed Solution

  • Add a method within BackupDatabaseJob to delete backups older than a configurable number of days (e.g., 7 days).

  • The method should:

    1. Identify and delete .tar.gz files in the db/backups directory.
    2. Calculate the file age using the file's mtime (modification time).
    3. If the file is older than the configured maximum age (e.g., 7 days), delete the file.

Example logic:

ruby
Copy code
def cleanup_old_backups
  backup_directory = "db/backups"
  max_age_days = 7  # Number of days to keep backups

  Dir.glob("#{backup_directory}/*.tar.gz").each do |file|
    file_age_in_days = (Time.now - File.mtime(file)) / (60 * 60 * 24)
    if file_age_in_days > max_age_days
      File.delete(file)
      Rails.logger.info "Deleted old backup file: #{file}"
    end
  end
end

Additional Considerations

  • Add configuration options to easily adjust the backup retention policy (e.g., via environment variables or settings).
  • Ensure proper logging of cleanup actions for traceability.
  • Optional: Provide an additional method to retain only the latest N backups, rather than using time-based deletion.

Related Files

  • app/jobs/backup_database_job.rb

Priority

Low/Medium (adjust based on your workflow)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant