/
auto_snapshot.rb
executable file
·59 lines (52 loc) · 1.81 KB
/
auto_snapshot.rb
1
2
3
4
5
6
7
8
9
10
11
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/usr/bin/env ruby
require 'cucloud'
require 'optparse'
DEFAULT_DAYS = '5'.freeze
options = {
add_tags: [],
num_days: DEFAULT_DAYS,
preserve_tags: []
}
OptionParser.new do |opts|
opts.on('--apply-tag Key=Foo,Value=Bar',
String,
'Appy tag to snapshots created.') do |item|
match = /^Key=([^,]+),Value=(.+)$/.match(item)
unless match && match[1] && match[2] && !match[3]
raise "Argument to --add-tag should be the string 'Key=<key>,Value=<value>'"
end
options[:add_tags] << { key: match[1], value: match[2] }
end
opts.on('--num-days N',
Integer,
"Snapshot volumes without snapshot in past N days (default #{DEFAULT_DAYS}).") do |item|
options[:num_days] = item
end
opts.on('--preserve-tags x,y,z',
Array,
'Array of tag keys to preserve from volume.') do |item|
item.each do |listitm|
options[:preserve_tags] << listitm
end
end
opts.on_tail('-h',
'--help',
'Command help') do
puts opts
exit
end
end.parse!
# Restore prior version behavior where you could simply provide the number of days
# as the sole/first argument.
options[:num_days] = ARGV[0] if ARGV.length == 1
ec2_utils = Cucloud::Ec2Utils.new
snapshots_created = ec2_utils.backup_volumes_unless_recent_backup(options[:num_days].to_i,
options[:preserve_tags],
options[:add_tags])
snapshots_created.each do |snapshot_created|
print "#{snapshot_created[:snapshot_id]} was created for volume #{snapshot_created[:volume]}"
unless snapshot_created[:instance_name].nil? || snapshot_created[:instance_name].empty?
print " attached to instance #{snapshot_created[:instance_name]}"
end
puts
end