ebs_snapper 0.0.8 → 0.0.9

Sign up to get free protection for your applications and to get access to all the features.
@@ -4,5 +4,6 @@ aws:
4
4
  secret_access_key : 'ACCESS KEY'
5
5
  volume_tag : 'Snapper' # tag to use in AWS for the EBS backup flags
6
6
  retain : '10.days'
7
+ max_retries : 3
8
+ pause_time: 1
7
9
  log_to: '/var/log/ebs_snapper.log'
8
- pause_time: 1
@@ -21,7 +21,10 @@ class EbsSnapper::CLI
21
21
 
22
22
  def self.run
23
23
  opts = parse(ARGV)
24
- ebs = EbsSnapper::Ebs.new(opts[:aws])
24
+ if opts[:dry_run]
25
+ @logger.info "Dry run mode enabled"
26
+ end
27
+ ebs = EbsSnapper::Ebs.new(opts[:aws], opts[:dry_run])
25
28
  ebs.snapshot_and_purge
26
29
  rescue => e
27
30
  @logger.error "Exception: #{e}\n" + e.backtrace().join("\n")
@@ -49,12 +52,12 @@ class EbsSnapper::CLI
49
52
  def self.parse(args)
50
53
  options = {}
51
54
  options[:aws] = {}
55
+ options[:dry_run] = false
52
56
  options[:ultradns] = {}
53
57
  options[:log_to] = nil
54
58
  options[:verbose] = false
55
59
  options[:out] = ''
56
60
  options[:config] = nil
57
- options[:pause_time] = 1
58
61
 
59
62
  opts = OptionParser.new do |opts|
60
63
  opts.banner = "Usage: ebs_snapper [options]"
@@ -73,6 +76,10 @@ class EbsSnapper::CLI
73
76
  options[:verbose] = v
74
77
  end
75
78
 
79
+ opts.on("-d", "--dry-run", "Dry run, will not create or delete snapshots") do
80
+ options[:dry_run] = true
81
+ end
82
+
76
83
  opts.on_tail("-h", "--help", "Show this message") do
77
84
  puts opts
78
85
  exit
@@ -19,16 +19,26 @@ class EbsSnapper::Ebs
19
19
  DEFAULT_TAG_NAME = 'Snapper'
20
20
  DEFAULT_PAUSE_TIME = 0
21
21
 
22
- def initialize(opts = {})
22
+ def initialize(opts = {}, dry_run = false)
23
+ @dry_run = dry_run
24
+ @logger = opts[:logger] || Logger.new(STDOUT)
25
+ max_retries = opts[:max_retries] || AWS.config.max_retries
23
26
  if !opts[:secret_access_key].nil? && !opts[:access_key_id].nil?
24
27
  AWS.config(:access_key_id => opts[:access_key_id],
25
- :secret_access_key => opts[:secret_access_key])
28
+ :secret_access_key => opts[:secret_access_key],
29
+ :logger => @logger,
30
+ :max_retries => max_retries)
31
+ else
32
+ AWS.config(:logger => @logger,
33
+ :max_retries => max_retries)
26
34
  end
27
35
 
28
- @logger = opts[:logger] || Logger.new(STDOUT)
29
36
  @retain = opts[:retain]
30
37
  @tag_name = opts[:volume_tag] || DEFAULT_TAG_NAME # default
31
38
  PausingEnumerable.pause_time = opts[:pause_time] || DEFAULT_PAUSE_TIME
39
+ @logger.info "Initializing"
40
+ @logger.info {"Dry run mode: #{@dry_run}"}
41
+ @logger.info {"AWS SDK max retries: #{AWS.config.max_retries}"}
32
42
  end
33
43
 
34
44
  def snapshot_and_purge
@@ -62,13 +72,18 @@ class EbsSnapper::Ebs
62
72
  end
63
73
 
64
74
  def snapshot_volume(region, vol_id)
65
- timestamp = Time.now.utc
66
- @logger.info {"Snapshotting #{vol_id} at: #{timestamp}"}
67
75
  vol = region.volumes[vol_id]
68
76
  if vol != nil
69
- snapshot = vol.create_snapshot("Snapper Backup #{timestamp}")
70
- # tag the snapshot with the timestamp so we can look it up later for cleanup
71
- snapshot.tag(@tag_name, :value => "#{timestamp.to_i}")
77
+ if dry_run?
78
+ @logger.info {"Dry run - would have called vol.create_snapshot for volume #{vol_id}"}
79
+ @logger.info {"Dry run - would have called snapshot.tag for new snapshot of volume #{vol_id}"}
80
+ else
81
+ timestamp = Time.now.utc
82
+ @logger.info {"Snapshotting #{vol_id} at: #{timestamp}"}
83
+ snapshot = vol.create_snapshot("Snapper Backup #{timestamp}")
84
+ # tag the snapshot with the timestamp so we can look it up later for cleanup
85
+ snapshot.tag(@tag_name, :value => "#{timestamp.to_i}")
86
+ end
72
87
  else
73
88
  @logger.error "Error: Volume #{vol_id} in Region: #{region} not found"
74
89
  end
@@ -80,9 +95,13 @@ class EbsSnapper::Ebs
80
95
  unless snapshot.status == :pending
81
96
  ts = snapshot.tags[@tag_name]
82
97
  if ttl.purge?(ts)
83
- @logger.info {"Purging #{vol_id} snapshot: #{snapshot.id}"}
84
98
  begin
85
- snapshot.delete
99
+ if dry_run?
100
+ @logger.info {"Dry run - would have called snapshot.delete for snapshot #{snapshot.id} of volume #{vol_id}"}
101
+ else
102
+ @logger.info {"Purging #{vol_id} snapshot: #{snapshot.id}"}
103
+ snapshot.delete
104
+ end
86
105
  rescue => e
87
106
  @logger.error "Exception: #{e}\n" + e.backtrace().join("\n")
88
107
  end
@@ -90,6 +109,10 @@ class EbsSnapper::Ebs
90
109
  end
91
110
  end
92
111
  end
112
+
113
+ def dry_run?
114
+ @dry_run == true
115
+ end
93
116
 
94
117
  module PausingEnumerable
95
118
 
@@ -120,6 +143,7 @@ class EbsSnapper::Ebs
120
143
  enumerable
121
144
  end
122
145
  end
146
+
123
147
 
124
148
  def each_region
125
149
  ec2.regions.each do |region|
@@ -13,5 +13,5 @@
13
13
  # limitations under the License.
14
14
 
15
15
  module EbsSnapper
16
- VERSION = "0.0.8"
16
+ VERSION = "0.0.9"
17
17
  end
data/spec/lib/ebs_spec.rb CHANGED
@@ -19,6 +19,37 @@ require 'ostruct'
19
19
  describe EbsSnapper::Ebs do
20
20
  DEFAULT_TAG_NAME = EbsSnapper::Ebs::DEFAULT_TAG_NAME
21
21
 
22
+
23
+ it "should run purge_old_snapshots in dry mode" do
24
+ ebs = EbsSnapper::Ebs.new({}, true)
25
+ ttl = EbsSnapper::Ebs::TTL.new("1.day") # 1 day
26
+ region = OpenStruct.new
27
+
28
+ snapshot_old = OpenStruct.new
29
+ snapshot_old.status = :complete
30
+ snapshot_old.tags = {DEFAULT_TAG_NAME => Time.now.utc.to_i - (86400 * 2)}
31
+ snapshot_old.should_not_receive(:delete)
32
+
33
+ region.snapshots = [snapshot_old]
34
+ region.snapshots.stub(:filter).and_return(region.snapshots)
35
+
36
+ ebs.purge_old_snapshots(ttl, region, 2)
37
+ end
38
+
39
+ it "should run snapshot_volume in dry mode" do
40
+ ebs = EbsSnapper::Ebs.new({}, true)
41
+
42
+ region = OpenStruct.new
43
+ region.tags = []
44
+ region.id = '999'
45
+
46
+ volume = OpenStruct.new
47
+ volume.should_not_receive(:create_snapshot)
48
+ region.volumes = {3 => volume}
49
+
50
+ ebs.snapshot_volume(region, 3).should == true
51
+ end
52
+
22
53
  it "should find tagged volumes in a region" do
23
54
  ebs = EbsSnapper::Ebs.new
24
55
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ebs_snapper
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.8
4
+ version: 0.0.9
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-01-09 00:00:00.000000000 Z
12
+ date: 2014-01-14 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: aws-sdk
@@ -111,7 +111,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
111
111
  version: '0'
112
112
  segments:
113
113
  - 0
114
- hash: 529299969700256006
114
+ hash: 1635891677389619037
115
115
  required_rubygems_version: !ruby/object:Gem::Requirement
116
116
  none: false
117
117
  requirements:
@@ -120,7 +120,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
120
120
  version: '0'
121
121
  segments:
122
122
  - 0
123
- hash: 529299969700256006
123
+ hash: 1635891677389619037
124
124
  requirements: []
125
125
  rubyforge_project:
126
126
  rubygems_version: 1.8.24