diff options
author | James Edwards-Jones <jedwardsjones@gitlab.com> | 2017-09-19 16:23:26 +0100 |
---|---|---|
committer | James Edwards-Jones <jedwardsjones@gitlab.com> | 2017-09-19 16:23:26 +0100 |
commit | ac890d208737e26df420aaeba6dfa0923d37a589 (patch) | |
tree | 026c62a1b4f9ed14abaec3ea023b517c37fb3ca5 | |
parent | 120fd9af5cfa06e6e13f8b7ae7d009336867f7d4 (diff) | |
download | gitlab-ce-jej/fix-backup-manager-remove-old.tar.gz |
Extracted Backup Manager regext to new classjej/fix-backup-manager-remove-old
-rw-r--r-- | lib/backup/manager.rb | 71 |
1 files changed, 58 insertions, 13 deletions
diff --git a/lib/backup/manager.rb b/lib/backup/manager.rb index b9a573d3542..fe2a62e7d22 100644 --- a/lib/backup/manager.rb +++ b/lib/backup/manager.rb @@ -65,6 +65,60 @@ module Backup end end + class BackupFile + def initialize(path) + @path = path + end + + def valid? + passes_regex? + end + + def delete_if_expired + return if never_expires? + return unless valid? + return unless expired? + + FileUtils.rm(@path) + + true + end + + private + + def passes_regex? + regex_matches + end + + def expired? + Time.at(timestamp) < expire_before + end + + def expire_before + Time.now - keep_time + end + + def keep_time + Gitlab.config.backup.keep_time.to_i + end + + def never_expires? + keep_time <= 0 + end + + def timestamp + regex_matches[1].to_i + end + + def regex_matches + # For backward compatibility, there are 3 names the backups can have: + # - 1495527122_gitlab_backup.tar + # - 1495527068_2017_05_23_gitlab_backup.tar + # - 1495527097_2017_05_23_9.3.0-pre_gitlab_backup.tar + @regex_matches ||= @path.match(/(\d+)(?:_\d{4}_\d{2}_\d{2}(_\d+\.\d+\.\d+.*)?)?_gitlab_backup\.tar$/) + end + end + def remove_old # delete backups $progress.print "Deleting old backups ... " @@ -75,21 +129,12 @@ module Backup Dir.chdir(backup_path) do backup_file_list.each do |file| - # For backward compatibility, there are 3 names the backups can have: - # - 1495527122_gitlab_backup.tar - # - 1495527068_2017_05_23_gitlab_backup.tar - # - 1495527097_2017_05_23_9.3.0-pre_gitlab_backup.tar - next unless file =~ /(\d+)(?:_\d{4}_\d{2}_\d{2}(_\d+\.\d+\.\d+.*)?)?_gitlab_backup\.tar$/ - - timestamp = $1.to_i - - if Time.at(timestamp) < (Time.now - keep_time) - begin - FileUtils.rm(file) + begin + if BackupFile.new(file).delete_if_expired removed += 1 - rescue => e - $progress.puts "Deleting #{file} failed: #{e.message}".color(:red) end + rescue => e + $progress.puts "Deleting #{file} failed: #{e.message}".color(:red) end end end |