Skip to content

Instantly share code, notes, and snippets.

@dmreiland
Created March 9, 2012 22:19
Show Gist options
  • Save dmreiland/2009020 to your computer and use it in GitHub Desktop.
Save dmreiland/2009020 to your computer and use it in GitHub Desktop.
create, purge EBS snapshots
require "rubygems"
require "yaml"
require "syslog"
require "aws-sdk"
class Snapshot
def initialize(config_file)
begin
@config = YAML.load_file(config_file)
@ec2 = AWS::EC2.new(:access_key_id => @config['access_key_id'], :secret_access_key => @config['secret_access_key'])
rescue Exception => e
Syslog.open('snapshot_maintenance') { |log| log.crit("Failed to load configuration file: #{e.message}") }
exit
end
end
def create
@config['volumes'].each { |volume| @ec2.snapshots.create(:volume_id => volume)}
end
def purge
@config['volumes'].each { |volume|
@ec2.snapshots.filter('volume-id', volume).each { |snapshot|
((Date.today - Date.parse(snapshot.start_time.to_s)).to_i) < @config['max_age'] or snapshot.delete
}
}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment