Skip to content

Instantly share code, notes, and snippets.

@mattbailey
Created May 31, 2012 22:36
Show Gist options
  • Save mattbailey/2846848 to your computer and use it in GitHub Desktop.
Save mattbailey/2846848 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# Do not edit this script from the host, it is generated by chef
require 'rubygems'
require 'aws'
$aws_id = "REDACTED"
$aws_key = "REDACTED"
AWS.config(:access_key_id => $aws_id, :secret_access_key => $aws_key, :ec2_endpoint => "ec2.amazonaws.com")
ec2 = AWS::EC2.new
backups = YAML::load(File.open('/etc/backups.yml'))
backups.each do |host|
host["volumes"].each do |volume|
snapshot = ec2.volumes[volume["id"]].create_snapshot("automated backup")
name = "#{host["name"]}-#{host["env"]}-#{volume["device"]}"
snapshot.add_tag('Name', :value => name)
snapshot.add_tag('app', :value => host["name"])
snapshot.add_tag('env', :value => host["env"])
snapshot.add_tag('device', :value => volume["device"])
if ec2.snapshots.filter("tag:Name", name).count > host["retention"]
ec2.snapshots.filter("tag:Name", name).first.delete
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment