rds-rotate-db-snapshots 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +15 -0
- data/.document +5 -0
- data/Gemfile +12 -0
- data/Gemfile.lock +83 -0
- data/LICENSE.txt +20 -0
- data/README.rdoc +54 -0
- data/Rakefile +47 -0
- data/VERSION +1 -0
- data/bin/rds-rotate-db-snapshots +269 -0
- data/lib/.empty +1 -0
- data/rds-rotate-db-snapshots.gemspec +65 -0
- data/test/helper.rb +18 -0
- data/test/test_rds-rotate-db-snapshots.rb +7 -0
- metadata +129 -0
checksums.yaml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
---
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
ZTNjZTBjNWQ3ZjA3ZTRiYmU2NzM2ZWNkYTBiN2QyZGZmYjFmNGYyMw==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
OWQzZTBhMmRiYmE4NmNjNjkzMDJjNzkzZTRmYWFmNTc2NDY0NjQ3NQ==
|
7
|
+
SHA512:
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
MDQ5ZTA0ZTFkOWM5OWYyYTMwMDY5MDM2OGY3ODRkOWNmMDc4ODI2NzgzYWQy
|
10
|
+
ZDI0MWY5ZGZiOTE1ZWUwZjdmZjE2MjA1MGM1NTg4ZmIyZDk5ZTlhZDA5NmU4
|
11
|
+
N2RkYThmNzVmYWJhNzg3YWQ3MzkyYTVlOTAyZTIxMDllNjA5Yzg=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
ODQ4YjgxNzNlZjEzYWRkMDM4ZjZmM2Y3MTQ3NjliMGFjMDRjZmJiMmJhN2Q0
|
14
|
+
OTY3NWZjYTg4NTkzYjMwODhkYTVmZmMxNzVjNzJlNjA1MzBmOTJjYTE3YmY0
|
15
|
+
Zjc2ZjIxOGQ1Zjg3OWFhY2IyMDY0MWNkY2UyM2IyNDMzYjc4ODk=
|
data/.document
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
source "http://rubygems.org"
|
2
|
+
|
3
|
+
gem 'aws-sdk', '>= 1.51.0'
|
4
|
+
|
5
|
+
# Add dependencies to develop your gem here.
|
6
|
+
# Include everything needed to run rake, tests, features, etc.
|
7
|
+
group :development do
|
8
|
+
gem 'shoulda'
|
9
|
+
gem 'bundler'
|
10
|
+
gem 'jeweler'
|
11
|
+
gem 'simplecov'
|
12
|
+
end
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
activesupport (4.1.4)
|
5
|
+
i18n (~> 0.6, >= 0.6.9)
|
6
|
+
json (~> 1.7, >= 1.7.7)
|
7
|
+
minitest (~> 5.1)
|
8
|
+
thread_safe (~> 0.1)
|
9
|
+
tzinfo (~> 1.1)
|
10
|
+
addressable (2.3.6)
|
11
|
+
aws-sdk (1.51.0)
|
12
|
+
json (~> 1.4)
|
13
|
+
nokogiri (>= 1.4.4)
|
14
|
+
builder (3.2.2)
|
15
|
+
descendants_tracker (0.0.4)
|
16
|
+
thread_safe (~> 0.3, >= 0.3.1)
|
17
|
+
docile (1.1.5)
|
18
|
+
faraday (0.9.0)
|
19
|
+
multipart-post (>= 1.2, < 3)
|
20
|
+
git (1.2.8)
|
21
|
+
github_api (0.12.0)
|
22
|
+
addressable (~> 2.3)
|
23
|
+
descendants_tracker (~> 0.0.4)
|
24
|
+
faraday (~> 0.8, < 0.10)
|
25
|
+
hashie (>= 3.2)
|
26
|
+
multi_json (>= 1.7.5, < 2.0)
|
27
|
+
nokogiri (~> 1.6.3)
|
28
|
+
oauth2
|
29
|
+
hashie (3.2.0)
|
30
|
+
highline (1.6.21)
|
31
|
+
i18n (0.6.11)
|
32
|
+
jeweler (2.0.1)
|
33
|
+
builder
|
34
|
+
bundler (>= 1.0)
|
35
|
+
git (>= 1.2.5)
|
36
|
+
github_api
|
37
|
+
highline (>= 1.6.15)
|
38
|
+
nokogiri (>= 1.5.10)
|
39
|
+
rake
|
40
|
+
rdoc
|
41
|
+
json (1.8.1)
|
42
|
+
jwt (1.0.0)
|
43
|
+
mini_portile (0.6.0)
|
44
|
+
minitest (5.4.0)
|
45
|
+
multi_json (1.10.1)
|
46
|
+
multi_xml (0.5.5)
|
47
|
+
multipart-post (2.0.0)
|
48
|
+
nokogiri (1.6.3.1)
|
49
|
+
mini_portile (= 0.6.0)
|
50
|
+
oauth2 (1.0.0)
|
51
|
+
faraday (>= 0.8, < 0.10)
|
52
|
+
jwt (~> 1.0)
|
53
|
+
multi_json (~> 1.3)
|
54
|
+
multi_xml (~> 0.5)
|
55
|
+
rack (~> 1.2)
|
56
|
+
rack (1.5.2)
|
57
|
+
rake (10.3.2)
|
58
|
+
rdoc (4.1.1)
|
59
|
+
json (~> 1.4)
|
60
|
+
shoulda (3.5.0)
|
61
|
+
shoulda-context (~> 1.0, >= 1.0.1)
|
62
|
+
shoulda-matchers (>= 1.4.1, < 3.0)
|
63
|
+
shoulda-context (1.2.1)
|
64
|
+
shoulda-matchers (2.6.2)
|
65
|
+
activesupport (>= 3.0.0)
|
66
|
+
simplecov (0.9.0)
|
67
|
+
docile (~> 1.1.0)
|
68
|
+
multi_json
|
69
|
+
simplecov-html (~> 0.8.0)
|
70
|
+
simplecov-html (0.8.0)
|
71
|
+
thread_safe (0.3.4)
|
72
|
+
tzinfo (1.2.2)
|
73
|
+
thread_safe (~> 0.1)
|
74
|
+
|
75
|
+
PLATFORMS
|
76
|
+
ruby
|
77
|
+
|
78
|
+
DEPENDENCIES
|
79
|
+
aws-sdk (>= 1.51.0)
|
80
|
+
bundler
|
81
|
+
jeweler
|
82
|
+
shoulda
|
83
|
+
simplecov
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2014 Siarhei Kavaliou
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
= rds-rotate-db-snapshots
|
2
|
+
|
3
|
+
Provides a simple way to rotate RDS DB snapshots.
|
4
|
+
|
5
|
+
== Usage
|
6
|
+
|
7
|
+
[Usage]
|
8
|
+
rds-rotate-db-snapshots [options] <db_indentifier>
|
9
|
+
|
10
|
+
Add this script to CRON (let's say it will run this script every X hours) and it will do the job well
|
11
|
+
#/usr/bin/bash
|
12
|
+
AWS_ACCESS_KEY='xxxxxxxxxxxxxxxxxxxx'
|
13
|
+
AWS_SECRET_ACCESS_KEY='yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy'
|
14
|
+
AWS_REGION='eu-west-1'
|
15
|
+
DESCRIPTION_PREFIX='automatic-backup-'
|
16
|
+
RDS_ROTATOR=/here/is/the/path/to/rds-rotate-db-snapshots
|
17
|
+
DB_NAME='db_name_here'
|
18
|
+
|
19
|
+
$RDS_ROTATOR --aws-region $AWS_REGION --aws-access-key $AWS_ACCESS_KEY --aws-secret-access-key $AWS_SECRET_ACCESS_KEY --pattern $DESCRIPTION_PREFIX --keep-hourly 24 --keep-daily 7 --keep-weekly 4 --keep-monthly 1 --keep-yearly 0 --create-snapshot $DESCRIPTION_PREFIX$DB_NAME $DB_NAME
|
20
|
+
|
21
|
+
== Options
|
22
|
+
|
23
|
+
[--aws-access-key ACCESS_KEY] "AWS Access Key"
|
24
|
+
[--aws-secret-access-key SECRET_KEY] "AWS Secret Access Key"
|
25
|
+
[--aws-region REGION] "AWS Region"
|
26
|
+
[--pattern STRING] "Snapshots without this string in the description will be ignored"
|
27
|
+
[--by-tags TAG=VALUE,TAG=VALUE] "Instead of rotating specific snapshots, rotate over all the snapshots having the intersection of all given TAG=VALUE pairs."
|
28
|
+
[--backoff-limit INTEGER] "Backoff and retry when hitting RDS Error exceptions no more than this many times. Default is 15"
|
29
|
+
[--create-snapshot STRING] "Use this option if you want to create a snapshot"
|
30
|
+
[--keep-hourly INTEGER] "Number of hourly snapshots to keep"
|
31
|
+
[--keep-daily INTEGER] "Number of daily snapshots to keep"
|
32
|
+
[--keep-weekly INTEGER] "Number of weekly snapshots to keep"
|
33
|
+
[--keep-last] "Keep the most recent snapshot, regardless of time-based policy"
|
34
|
+
[--dry-run] "Shows what would happen without doing anything"
|
35
|
+
|
36
|
+
== Tips
|
37
|
+
|
38
|
+
If you are not sure what happen - add option "dry-run".
|
39
|
+
|
40
|
+
In that case the script will not destroy/create anything in RDS, it will just show the messages.
|
41
|
+
|
42
|
+
== Contributing to rds-rotate-db-snapshots
|
43
|
+
|
44
|
+
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
|
45
|
+
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
|
46
|
+
* Fork the project
|
47
|
+
* Start a feature/bugfix branch
|
48
|
+
* Commit and push until you are happy with your contribution
|
49
|
+
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
50
|
+
* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
|
51
|
+
|
52
|
+
== Copyright
|
53
|
+
|
54
|
+
Copyright (c) 2014 Siarhei Kavaliou. See LICENSE.txt for further details.
|
data/Rakefile
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler'
|
3
|
+
begin
|
4
|
+
Bundler.setup(:default, :development)
|
5
|
+
rescue Bundler::BundlerError => e
|
6
|
+
$stderr.puts e.message
|
7
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
8
|
+
exit e.status_code
|
9
|
+
end
|
10
|
+
require 'rake'
|
11
|
+
|
12
|
+
require 'jeweler'
|
13
|
+
Jeweler::Tasks.new do |gem|
|
14
|
+
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
15
|
+
gem.name = "rds-rotate-db-snapshots"
|
16
|
+
gem.homepage = "http://github.com/serg-kovalev/rds-rotate-db-snapshots"
|
17
|
+
gem.license = "MIT"
|
18
|
+
gem.summary = %Q{Amazon RDS DB snapshot rotator}
|
19
|
+
gem.description = %Q{Provides a simple way to rotate RDS DB snapshots with configurable retention periods.}
|
20
|
+
gem.email = "kovserg@gmail.com"
|
21
|
+
gem.authors = ["Siarhei Kavaliou"]
|
22
|
+
end
|
23
|
+
Jeweler::RubygemsDotOrgTasks.new
|
24
|
+
|
25
|
+
require 'rake/testtask'
|
26
|
+
Rake::TestTask.new(:test) do |test|
|
27
|
+
test.pattern = 'test/**/test_*.rb'
|
28
|
+
test.verbose = true
|
29
|
+
end
|
30
|
+
|
31
|
+
require 'simplecov'
|
32
|
+
# Rcov::RcovTask.new do |test|
|
33
|
+
# test.libs << 'test'
|
34
|
+
# test.pattern = 'test/**/test_*.rb'
|
35
|
+
# test.verbose = true
|
36
|
+
# end
|
37
|
+
|
38
|
+
task :default => :test
|
39
|
+
|
40
|
+
require 'rdoc/task'
|
41
|
+
Rake::RDocTask.new do |rdoc|
|
42
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
43
|
+
|
44
|
+
rdoc.rdoc_dir = 'rdoc'
|
45
|
+
rdoc.title = "rds-rotate-db-snapshots #{version}"
|
46
|
+
rdoc.rdoc_files.include('README*')
|
47
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.2.1
|
@@ -0,0 +1,269 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'aws-sdk'
|
5
|
+
require 'optparse'
|
6
|
+
|
7
|
+
$opts = {
|
8
|
+
:aws_access_key => ENV["AWS_ACCESS_KEY_ID"],
|
9
|
+
:aws_secret_access_key => ENV["AWS_SECRET_ACCESS_KEY"],
|
10
|
+
:aws_region => ENV["AWS_REGION"],
|
11
|
+
:pattern => nil,
|
12
|
+
:by_tags => nil,
|
13
|
+
:dry_run => false,
|
14
|
+
:backoff_limit => 15,
|
15
|
+
:create_snapshot => nil
|
16
|
+
}
|
17
|
+
|
18
|
+
$time_periods = {
|
19
|
+
:hourly => { :seconds => 60 * 60, :format => '%Y-%m-%d-%H', :keep => 0, :keeping => {} },
|
20
|
+
:daily => { :seconds => 24 * 60 * 60, :format => '%Y-%m-%d', :keep => 0, :keeping => {} },
|
21
|
+
:weekly => { :seconds => 7 * 24 * 60 * 60, :format => '%Y-%W', :keep => 0, :keeping => {} },
|
22
|
+
:monthly => { :seconds => 30 * 24 * 60 * 60, :format => '%Y-%m', :keep => 0, :keeping => {} },
|
23
|
+
:yearly => { :seconds => 12 * 30 * 24 * 60 * 60, :format => '%Y', :keep => 0, :keeping => {} },
|
24
|
+
}
|
25
|
+
def backoff()
|
26
|
+
$backoffed = $backoffed + 1
|
27
|
+
|
28
|
+
if $opts[:backoff_limit] > 0 && $opts[:backoff_limit] < $backoffed
|
29
|
+
puts "Too many backoff attempts. Sorry it didn't work out."
|
30
|
+
exit 2
|
31
|
+
end
|
32
|
+
|
33
|
+
naptime = rand(60) * $backoffed
|
34
|
+
puts "Backing off for #{naptime} seconds..."
|
35
|
+
sleep naptime
|
36
|
+
end
|
37
|
+
|
38
|
+
def rotate_em(snapshots)
|
39
|
+
# poor man's way to get a deep copy of our time_periods definition hash
|
40
|
+
periods = Marshal.load(Marshal.dump($time_periods))
|
41
|
+
|
42
|
+
snapshots.each do |snapshot|
|
43
|
+
time = snapshot[:snapshot_create_time]
|
44
|
+
db_id = snapshot[:db_instance_identifier]
|
45
|
+
snapshot_id = snapshot[:db_snapshot_identifier]
|
46
|
+
description = snapshot_id
|
47
|
+
keep_reason = nil
|
48
|
+
|
49
|
+
if $opts[:pattern] && description !~ /#{$opts[:pattern]}/
|
50
|
+
puts " #{time.strftime '%Y-%m-%d %H:%M:%S'} #{snapshot_id} Skipping snapshot with description #{description}"
|
51
|
+
next
|
52
|
+
end
|
53
|
+
|
54
|
+
periods.keys.sort { |a, b| periods[a][:seconds] <=> periods[b][:seconds] }.each do |period|
|
55
|
+
period_info = periods[period]
|
56
|
+
keep = period_info[:keep]
|
57
|
+
keeping = period_info[:keeping]
|
58
|
+
|
59
|
+
time_string = time.strftime period_info[:format]
|
60
|
+
if Time.now - time < keep * period_info[:seconds]
|
61
|
+
if !keeping.key?(time_string) && keeping.length < keep
|
62
|
+
keep_reason = period
|
63
|
+
keeping[time_string] = snapshot
|
64
|
+
end
|
65
|
+
break
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
if keep_reason.nil? && snapshot == snapshots.last && $opts[:keep_last]
|
70
|
+
keep_reason = 'last snapshot'
|
71
|
+
end
|
72
|
+
|
73
|
+
if !keep_reason.nil?
|
74
|
+
puts " #{time.strftime '%Y-%m-%d %H:%M:%S'} #{snapshot_id} Keeping for #{keep_reason}"
|
75
|
+
else
|
76
|
+
puts " #{time.strftime '%Y-%m-%d %H:%M:%S'} #{snapshot_id} Deleting"
|
77
|
+
begin
|
78
|
+
$rds.delete_db_snapshot(db_snapshot_identifier: snapshot_id) unless $opts[:dry_run]
|
79
|
+
rescue AWS::RDS::Errors => e
|
80
|
+
backoff()
|
81
|
+
retry
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
def create_snapshot(name, db_indentifier_ids)
|
88
|
+
if !!name
|
89
|
+
name = name.gsub(/[^a-zA-Z0-9\-]/, '')
|
90
|
+
if name.size > 0
|
91
|
+
name = "#{name}-#{Time.now.strftime('%Y%m%d%H%M%S')}"
|
92
|
+
db_indentifier_ids.each do |db_id|
|
93
|
+
begin
|
94
|
+
$rds.create_db_snapshot(db_snapshot_identifier: name, db_instance_identifier: db_id) unless $opts[:dry_run]
|
95
|
+
puts " #{Time.now.strftime '%Y-%m-%d %H:%M:%S'} Creation snapshot #{name} is pending (db: #{db_id})"
|
96
|
+
rescue AWS::RDS::Errors::InvalidDBInstanceState => e
|
97
|
+
backoff()
|
98
|
+
retry
|
99
|
+
end
|
100
|
+
end
|
101
|
+
else
|
102
|
+
puts "invalid snapshot name format - #{name}"
|
103
|
+
exit 1
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
def split_tag(hash,v)
|
109
|
+
v.split(',').each do |pair|
|
110
|
+
tag, value = pair.split('=',2)
|
111
|
+
if value.nil?
|
112
|
+
puts "invalid tag=value format"
|
113
|
+
exit 1
|
114
|
+
end
|
115
|
+
hash[tag] = value
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
OptionParser.new do |o|
|
120
|
+
script_name = File.basename($0)
|
121
|
+
o.banner = "Usage: #{script_name} [options] <db_indentifier>\nUsage: #{script_name} --by-tags <tag=value,...> [other options]"
|
122
|
+
o.separator ""
|
123
|
+
|
124
|
+
o.on("--aws-access-key ACCESS_KEY", "AWS Access Key") do |v|
|
125
|
+
$opts[:aws_access_key] = v
|
126
|
+
end
|
127
|
+
|
128
|
+
o.on("--aws-secret-access-key SECRET_KEY", "AWS Secret Access Key") do |v|
|
129
|
+
$opts[:aws_secret_access_key] = v
|
130
|
+
end
|
131
|
+
|
132
|
+
o.on("--aws-region REGION", "AWS Region") do |v|
|
133
|
+
$opts[:aws_region] = v
|
134
|
+
end
|
135
|
+
|
136
|
+
o.on("--pattern STRING", "Snapshots without this string in the description will be ignored") do |v|
|
137
|
+
$opts[:pattern] = v
|
138
|
+
end
|
139
|
+
|
140
|
+
o.on("--by-tags TAG=VALUE,TAG=VALUE", "Instead of rotating specific snapshots, rotate over all the snapshots having the intersection of all given TAG=VALUE pairs.") do |v|
|
141
|
+
$opts[:by_tags] = {}
|
142
|
+
puts 'Hey! It\'s not implemented in RDS yet. Who knows, maybe they will add Tagging in RDS later.'
|
143
|
+
exit 0
|
144
|
+
split_tag($opts[:by_tags],v)
|
145
|
+
end
|
146
|
+
|
147
|
+
o.on("--backoff-limit LIMIT", "Backoff and retry when hitting RDS Error exceptions no more than this many times. Default is 15") do |v|
|
148
|
+
$opts[:backoff_limit] = v
|
149
|
+
end
|
150
|
+
|
151
|
+
o.on("--create-snapshot STRING", "Use this option if you want to create a snapshot") do |v|
|
152
|
+
$opts[:create_snapshot] = v
|
153
|
+
end
|
154
|
+
|
155
|
+
$time_periods.keys.sort { |a, b| $time_periods[a][:seconds] <=> $time_periods[b][:seconds] }.each do |period|
|
156
|
+
o.on("--keep-#{period} NUMBER", Integer, "Number of #{period} snapshots to keep") do |v|
|
157
|
+
$time_periods[period][:keep] = v
|
158
|
+
end
|
159
|
+
end
|
160
|
+
|
161
|
+
o.on("--keep-last", "Keep the most recent snapshot, regardless of time-based policy") do |v|
|
162
|
+
$opts[:keep_last] = true
|
163
|
+
end
|
164
|
+
|
165
|
+
o.on("--dry-run", "Shows what would happen without doing anything") do |v|
|
166
|
+
$opts[:dry_run] = true
|
167
|
+
end
|
168
|
+
end.parse!
|
169
|
+
|
170
|
+
if $opts[:aws_access_key].nil? || $opts[:aws_secret_access_key].nil?
|
171
|
+
puts "You must specify your Amazon credentials via --aws-access-key and --aws-secret_access-key"
|
172
|
+
exit 1
|
173
|
+
end
|
174
|
+
|
175
|
+
if ARGV.empty? and $opts[:by_tags].nil?
|
176
|
+
puts "You must provide at least one DB Indentifier when not rotating by tags"
|
177
|
+
exit 1
|
178
|
+
end
|
179
|
+
|
180
|
+
if $opts[:by_tags].nil?
|
181
|
+
db_indentifier_ids = ARGV
|
182
|
+
|
183
|
+
db_indentifier_ids.each do |db_id|
|
184
|
+
if db_id.nil? or db_id.gsub(/\s/, '').size < 1
|
185
|
+
# sanity check
|
186
|
+
puts "Invalid db indentifier: #{db_id}"
|
187
|
+
exit 1
|
188
|
+
end
|
189
|
+
end
|
190
|
+
else
|
191
|
+
if !ARGV.empty?
|
192
|
+
puts "Ignoring supplied db_indentifier_ids because we're rotating by tags."
|
193
|
+
end
|
194
|
+
if $opts[:by_tags].length == 0
|
195
|
+
puts "Rotating by tags but no tags specified? Refusing to rotate all snapshots!"
|
196
|
+
exit 1
|
197
|
+
end
|
198
|
+
end
|
199
|
+
|
200
|
+
if $opts[:backoff_limit] < 0
|
201
|
+
puts "A negative backoff limit doesn't make much sense."
|
202
|
+
exit 1
|
203
|
+
end
|
204
|
+
|
205
|
+
$backoffed = 0
|
206
|
+
begin
|
207
|
+
AWS.config(access_key_id: $opts[:aws_access_key], secret_access_key: $opts[:aws_secret_access_key], region: $opts[:aws_region])
|
208
|
+
$rds = AWS.rds.client
|
209
|
+
rescue AWS::RDS::Errors => e
|
210
|
+
backoff()
|
211
|
+
retry
|
212
|
+
end
|
213
|
+
|
214
|
+
if $opts[:create_snapshot]
|
215
|
+
create_snapshot($opts[:create_snapshot], db_indentifier_ids)
|
216
|
+
end
|
217
|
+
|
218
|
+
all_snapshots = []
|
219
|
+
if $opts[:by_tags]
|
220
|
+
$opts[:by_tags].each do |tag, value|
|
221
|
+
begin
|
222
|
+
these_snapshots = $rds.describe_tags(snapshot_type: 'manual', filters: {'resource-type'=>"snapshot", 'key'=>tag, 'value'=>value}).
|
223
|
+
delete_if{ |e| e.status != 'available' }
|
224
|
+
rescue AWS::RDS::Errors => e
|
225
|
+
backoff()
|
226
|
+
retry
|
227
|
+
end
|
228
|
+
if these_snapshots.length == 0
|
229
|
+
puts "(tag,value)=(#{tag},#{value}) found no snapshots; nothing to rotate!"
|
230
|
+
exit 0
|
231
|
+
end
|
232
|
+
if all_snapshots.length == 0
|
233
|
+
remaining_snapshots = these_snapshots
|
234
|
+
else
|
235
|
+
remaining_snapshots = all_snapshots & these_snapshots
|
236
|
+
end
|
237
|
+
if remaining_snapshots.length == 0
|
238
|
+
puts "No remaining snapshots after applying (tag,value)=(#{tag},#{value}) filter; nothing to rotate!"
|
239
|
+
exit 0
|
240
|
+
end
|
241
|
+
all_snapshots = remaining_snapshots
|
242
|
+
end
|
243
|
+
|
244
|
+
begin
|
245
|
+
rotate_these = $rds.describe_db_snapshots(db_instance_identifier: all_snapshots.map(&:db_instance_identifier).uniq).
|
246
|
+
db_snapshots.delete_if{ |e| !all_snapshots.include?(e.db_snapshot_identifier) }.
|
247
|
+
sort {|a,b| a[:snapshot_create_time] <=> b[:snapshot_create_time] }
|
248
|
+
rescue AWS::RDS::Errors => e
|
249
|
+
backoff()
|
250
|
+
retry
|
251
|
+
end
|
252
|
+
|
253
|
+
rotate_em(rotate_these)
|
254
|
+
else
|
255
|
+
begin
|
256
|
+
all_snapshots = $rds.describe_db_snapshots(snapshot_type: 'manual').db_snapshots.
|
257
|
+
delete_if{ |e| e.status != 'available' }
|
258
|
+
rescue AWS::RDS::Errors => e
|
259
|
+
backoff()
|
260
|
+
retry
|
261
|
+
end
|
262
|
+
|
263
|
+
db_indentifier_ids.each do |db_id|
|
264
|
+
rotate_em(
|
265
|
+
all_snapshots.select {|ss| ss[:db_instance_identifier] == db_id }.
|
266
|
+
sort {|a,b| a[:snapshot_create_time] <=> b[:snapshot_create_time] }
|
267
|
+
)
|
268
|
+
end
|
269
|
+
end
|
data/lib/.empty
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
Jeweler doesn't like me not having a lib/ directory.
|
@@ -0,0 +1,65 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
# stub: rds-rotate-db-snapshots 0.2.1 ruby lib
|
6
|
+
|
7
|
+
Gem::Specification.new do |s|
|
8
|
+
s.name = "rds-rotate-db-snapshots"
|
9
|
+
s.version = "0.2.1"
|
10
|
+
|
11
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
12
|
+
s.require_paths = ["lib"]
|
13
|
+
s.authors = ["Siarhei Kavaliou"]
|
14
|
+
s.date = "2014-08-15"
|
15
|
+
s.description = "Provides a simple way to rotate RDS DB snapshots with configurable retention periods."
|
16
|
+
s.email = "kovserg@gmail.com"
|
17
|
+
s.executables = ["rds-rotate-db-snapshots"]
|
18
|
+
s.extra_rdoc_files = [
|
19
|
+
"LICENSE.txt",
|
20
|
+
"README.rdoc"
|
21
|
+
]
|
22
|
+
s.files = [
|
23
|
+
".document",
|
24
|
+
"Gemfile",
|
25
|
+
"Gemfile.lock",
|
26
|
+
"LICENSE.txt",
|
27
|
+
"README.rdoc",
|
28
|
+
"Rakefile",
|
29
|
+
"VERSION",
|
30
|
+
"bin/rds-rotate-db-snapshots",
|
31
|
+
"lib/.empty",
|
32
|
+
"rds-rotate-db-snapshots.gemspec",
|
33
|
+
"test/helper.rb",
|
34
|
+
"test/test_rds-rotate-db-snapshots.rb"
|
35
|
+
]
|
36
|
+
s.homepage = "http://github.com/serg-kovalev/rds-rotate-db-snapshots"
|
37
|
+
s.licenses = ["MIT"]
|
38
|
+
s.rubygems_version = "2.2.2"
|
39
|
+
s.summary = "Amazon RDS DB snapshot rotator"
|
40
|
+
|
41
|
+
if s.respond_to? :specification_version then
|
42
|
+
s.specification_version = 4
|
43
|
+
|
44
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
45
|
+
s.add_runtime_dependency(%q<aws-sdk>, [">= 1.51.0"])
|
46
|
+
s.add_development_dependency(%q<shoulda>, [">= 0"])
|
47
|
+
s.add_development_dependency(%q<bundler>, [">= 0"])
|
48
|
+
s.add_development_dependency(%q<jeweler>, [">= 0"])
|
49
|
+
s.add_development_dependency(%q<simplecov>, [">= 0"])
|
50
|
+
else
|
51
|
+
s.add_dependency(%q<aws-sdk>, [">= 1.51.0"])
|
52
|
+
s.add_dependency(%q<shoulda>, [">= 0"])
|
53
|
+
s.add_dependency(%q<bundler>, [">= 0"])
|
54
|
+
s.add_dependency(%q<jeweler>, [">= 0"])
|
55
|
+
s.add_dependency(%q<simplecov>, [">= 0"])
|
56
|
+
end
|
57
|
+
else
|
58
|
+
s.add_dependency(%q<aws-sdk>, [">= 1.51.0"])
|
59
|
+
s.add_dependency(%q<shoulda>, [">= 0"])
|
60
|
+
s.add_dependency(%q<bundler>, [">= 0"])
|
61
|
+
s.add_dependency(%q<jeweler>, [">= 0"])
|
62
|
+
s.add_dependency(%q<simplecov>, [">= 0"])
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
data/test/helper.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler'
|
3
|
+
begin
|
4
|
+
Bundler.setup(:default, :development)
|
5
|
+
rescue Bundler::BundlerError => e
|
6
|
+
$stderr.puts e.message
|
7
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
8
|
+
exit e.status_code
|
9
|
+
end
|
10
|
+
require 'test/unit'
|
11
|
+
require 'shoulda'
|
12
|
+
|
13
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
14
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
15
|
+
require 'rds-rotate-db-snapshots'
|
16
|
+
|
17
|
+
class Test::Unit::TestCase
|
18
|
+
end
|
metadata
ADDED
@@ -0,0 +1,129 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rds-rotate-db-snapshots
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Siarhei Kavaliou
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-08-15 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: aws-sdk
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ! '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 1.51.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ! '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 1.51.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: shoulda
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ! '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ! '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ! '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: jeweler
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ! '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: simplecov
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ! '>='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ! '>='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
description: Provides a simple way to rotate RDS DB snapshots with configurable retention
|
84
|
+
periods.
|
85
|
+
email: kovserg@gmail.com
|
86
|
+
executables:
|
87
|
+
- rds-rotate-db-snapshots
|
88
|
+
extensions: []
|
89
|
+
extra_rdoc_files:
|
90
|
+
- LICENSE.txt
|
91
|
+
- README.rdoc
|
92
|
+
files:
|
93
|
+
- .document
|
94
|
+
- Gemfile
|
95
|
+
- Gemfile.lock
|
96
|
+
- LICENSE.txt
|
97
|
+
- README.rdoc
|
98
|
+
- Rakefile
|
99
|
+
- VERSION
|
100
|
+
- bin/rds-rotate-db-snapshots
|
101
|
+
- lib/.empty
|
102
|
+
- rds-rotate-db-snapshots.gemspec
|
103
|
+
- test/helper.rb
|
104
|
+
- test/test_rds-rotate-db-snapshots.rb
|
105
|
+
homepage: http://github.com/serg-kovalev/rds-rotate-db-snapshots
|
106
|
+
licenses:
|
107
|
+
- MIT
|
108
|
+
metadata: {}
|
109
|
+
post_install_message:
|
110
|
+
rdoc_options: []
|
111
|
+
require_paths:
|
112
|
+
- lib
|
113
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ! '>='
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
119
|
+
requirements:
|
120
|
+
- - ! '>='
|
121
|
+
- !ruby/object:Gem::Version
|
122
|
+
version: '0'
|
123
|
+
requirements: []
|
124
|
+
rubyforge_project:
|
125
|
+
rubygems_version: 2.2.2
|
126
|
+
signing_key:
|
127
|
+
specification_version: 4
|
128
|
+
summary: Amazon RDS DB snapshot rotator
|
129
|
+
test_files: []
|