Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@skout23
Last active October 6, 2015 13:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save skout23/3001264 to your computer and use it in GitHub Desktop.
Save skout23/3001264 to your computer and use it in GitHub Desktop.
AWS ec2 EBS snapshot management
#!/usr/bin/env ruby
# cleaned up a bit, makes use of memoize to better handle queries to the EC2 backend, less calls == faster.
require 'rubygems'
require 'aws-sdk'
regions = [
"ec2.us-east-1.amazonaws.com",
"ec2.us-west-1.amazonaws.com",
]
DEBUG = false
targetDate = Time.now - (60 * 60 * 24 * 14)
ACCESS_KEY_ID="SCRUBBED"
SECRET_ACCESS_KEY="SCRUBBED"
regions.each do |region|
puts region if DEBUG
AWS.config({
:access_key_id => ACCESS_KEY_ID,
:secret_access_key => SECRET_ACCESS_KEY,
:ec2_endpoint => "#{region}",
:max_retries => 2,
})
ec2 = AWS::EC2.new
AWS.memoize do
delshots = ec2.snapshots.with_owner(:self)
puts delshots.count if DEBUG
delcounter = delshots.count
delshots.each do |snap|
delcounter-=1
puts delcounter if DEBUG
next if not snap.start_time < targetDate
next if snap.description =~ /created/i
begin
puts "#{snap.id}\t#{snap.start_time}\t#{snap.description}" if DEBUG
snap.delete
rescue => e
puts "opps"
next
end
end
end
sleep 60
# create snapshots
AWS.memoize do
ec2.volumes.each do |vol|
next if not vol.size > 9
$attempt = 1
begin
puts "#{vol.id}\t#{vol.tags['Name']}\t#{vol.size}\t#{vol.status}" if DEBUG
vol.create_snapshot("Snapshot of #{vol.tags['Name']}")
rescue => e
if $attempt == 1
$attempt +=1
retry
else
puts e.backtrace if DEBUG
next
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment