Class: Synco::Command::Prune

Inherits:
Samovar::Command
  • Object
show all
Defined in:
lib/synco/command/prune.rb

Instance Method Summary collapse

Instance Method Details

#current_backupsObject



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# 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?Boolean

Returns:

  • (Boolean)


117
118
119
# File 'lib/synco/command/prune.rb', line 117

def dry?
	@options[:dry]
end

#invoke(parent) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/synco/command/prune.rb', line 141

def invoke(parent)
	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)
	else
		perform_rotation(retain, erase)
	end
end

#perform_rotation(keep, erase) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
# 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

#policyObject



87
88
89
90
91
92
93
94
95
96
97
98
# 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


121
122
123
124
125
126
127
# File 'lib/synco/command/prune.rb', line 121

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