Class: Synco::Command::Prune
- Inherits:
-
Samovar::Command
- Object
- Samovar::Command
- Synco::Command::Prune
- Defined in:
- lib/synco/command/prune.rb
Instance Method Summary collapse
- #call ⇒ Object
- #current_backups ⇒ Object
- #dry? ⇒ Boolean
- #perform_rotation(keep, erase) ⇒ Object
- #policy ⇒ Object
- #print_rotation(keep, erase) ⇒ Object
Instance Method Details
#call ⇒ Object
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/synco/command/prune.rb', line 126 def call backups = current_backups retain, erase = policy.filter(backups, keep: @options[:keep].to_sym, &:time) # We need to retain the latest backup regardless of policy if latest = @options[:latest] and File.exist?(latest) latest_path = File.readlink([:latest]) latest_rotation = erase.find{|rotation| rotation.path == latest_path} if latest_rotation puts "Retaining latest backup #{latest_rotation}" erase.delete(latest_rotation) retain << latest_rotation end end if dry? print_rotation(retain, erase) elsif erase.any? perform_rotation(retain, erase) end end |
#current_backups ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/synco/command/prune.rb', line 85 def current_backups backups = [] Dir["*"].each do |path| next if path == @options[:latest] date_string = File.basename(path) begin backups << Rotation.new(path, DateTime.strptime(date_string, @options[:format])) rescue ArgumentError $stderr.puts "Skipping #{path}, error parsing #{date_string}: #{$!}" end end return backups end |
#dry? ⇒ Boolean
102 103 104 |
# File 'lib/synco/command/prune.rb', line 102 def dry? @options[:dry] end |
#perform_rotation(keep, erase) ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/synco/command/prune.rb', line 114 def perform_rotation(keep, erase) puts "*** Rotating backups ***" erase.sort.each do |backup| puts "Erasing #{backup.path}..." $stdout.flush # Ensure that we can remove the backup system("chmod", "-R", "ug+rwX", backup.path) system("rm", "-rf", backup.path) end end |
#policy ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/synco/command/prune.rb', line 72 def policy policy = Periodical::Filter::Policy.new policy << Periodical::Filter::Hourly.new(@options[:hourly]) policy << Periodical::Filter::Daily.new(@options[:daily]) policy << Periodical::Filter::Weekly.new(@options[:weekly]) policy << Periodical::Filter::Monthly.new(@options[:monthly]) policy << Periodical::Filter::Quarterly.new(@options[:quarterly]) policy << Periodical::Filter::Yearly.new(@options[:yearly]) return policy end |
#print_rotation(keep, erase) ⇒ Object
106 107 108 109 110 111 112 |
# File 'lib/synco/command/prune.rb', line 106 def print_rotation(keep, erase) puts "*** Rotating backups (DRY!) ***" puts "\tKeeping:" keep.sort.each { |backup| puts "\t\t#{backup.path}" } puts "\tErasing:" erase.sort.each { |backup| puts "\t\t#{backup.path}" } end |