Module: BackupCleaner
- Defined in:
- lib/backup_cleaner.rb,
lib/backup_cleaner/cli.rb
Overview
See backup_cleaner/cli for API documentation
Defined Under Namespace
Modules: Cli Classes: Entry, GroupKey
Constant Summary collapse
- DATE_PATTERN =
/^(.*)(\d{4}[-\.]\d{2}[-\.]\d{2})(.*)$/
Class Method Summary collapse
Class Method Details
.clean_backups(folder, opts = {}) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/backup_cleaner.rb', line 12 def self.clean_backups(folder, opts = {}) days = opts[:days] weeks = opts[:weeks] dry_run = opts[:dry_run] raise ArgumentError, "'days' option must have an integer value" unless days.is_a? Integer raise ArgumentError, "'weeks' option must have an integer value" unless weeks.is_a? Integer raise ArgumentError, "#{folder} should be a directory" unless File.directory? folder entries = Dir.entries(folder).select { |name| DATE_PATTERN =~ name }.collect do |name| m = DATE_PATTERN.match(name) Entry.new(name, Date.parse(m[2]), m[1].gsub(/[^A-Za-z]/, ''), m[3].gsub(/[^A-Za-z]/, '')) end weekly_keepers = Set.new(first_by_date_property(entries, :week_ordering_number)) monthly_keepers = Set.new(first_by_date_property(entries, :month_ordering_number)) entries.each do |entry| unless entry.date >= days.days.ago.to_date || (week_ordering_number(entry.date) >= week_ordering_number(weeks.weeks.ago.to_date) && weekly_keepers.member?(entry)) || monthly_keepers.member?(entry) if dry_run puts "pretending to delete (dry_run) #{entry.name}" else puts "deleting #{entry.name}" FileUtils.rm_rf(File.join(folder, entry.name)) end end end end |