Skip to content

Instantly share code, notes, and snippets.

@trobrock
Created November 17, 2011 23:18
Show Gist options
  • Save trobrock/1374900 to your computer and use it in GitHub Desktop.
Save trobrock/1374900 to your computer and use it in GitHub Desktop.
Clean up EC2 snapshots
namespace :snapshots do
desc "cull all snapshots except for 1st of each month and last 60 days."
task :cull do
ec2 = Configuration.instance.connection
snaps = ec2.describe_snapshots
snaps = snaps.select {|s| DateTime.parse(s[:aws_started_at]) < 60.days.ago }
snaps.inject({}) { |newhash, h| newhash[Time.parse(h[:aws_started_at])] = h; newhash }
saved = newhash.keys.group_by { |date| date.month }.map { |k, v| v.first }
saved.each do |key|
snaps.delete key
end
snaps.each do |snap|
begin
ec2.delete_snapshot(snap[:aws_id])
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment