class Synco::Command::Prune

Public Instance Methods

call() click to toggle source
# File lib/synco/command/prune.rb, line 141
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(options[: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() click to toggle source
# File lib/synco/command/prune.rb, line 100
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?() click to toggle source
# File lib/synco/command/prune.rb, line 117
def dry?
        @options[:dry]
end
perform_rotation(keep, erase) click to toggle source
# File lib/synco/command/prune.rb, line 129
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() click to toggle source
# File lib/synco/command/prune.rb, line 87
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) click to toggle source