Created
November 17, 2011 23:18
-
-
Save trobrock/1374900 to your computer and use it in GitHub Desktop.
Clean up EC2 snapshots
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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