Created
October 10, 2012 15:00
-
-
Save michaeldauria/3866168 to your computer and use it in GitHub Desktop.
This file contains 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
require 'fog' | |
require 'net/ssh' | |
pretend = false | |
backup_volumes = { | |
'hostname' => [ | |
{ | |
:device => '/dev/xvdf', | |
:mount => '/srv/mongo' | |
} | |
] | |
} | |
now = Time.now | |
now_date = now.strftime('%Y-%m-%d') | |
now_time = now.strftime('%H:%M:%S') | |
now_datetime = [ now_date, now_time ].join(' ') | |
puts '. Starting Backup Process %s' % now_datetime | |
AWS = Fog::Compute.new({ | |
:provider => 'AWS', | |
:aws_access_key_id => '', | |
:aws_secret_access_key => '' | |
}) | |
ssh_user = 'ec2_user' | |
ssh_options = { | |
:auth_methods => [ 'publickey' ], | |
:forward_agent => false | |
} | |
puts '. Grabbing Server Info' | |
servers = {} | |
AWS.servers.each do |server| | |
servers[server.tags['Name']] = server | |
end | |
backup_volumes.each_pair do |srv, volumes| | |
volumes.each do |volume| | |
start = Time.now | |
server = servers[srv] | |
device = volume[:device] | |
mount = volume[:mount] | |
snapshots = [] | |
puts '.. Connecting to %s as %s' % [ srv, ssh_user ] | |
Net::SSH.start(srv, ssh_user, ssh_options) do |ssh| | |
puts '.. MongoDB Flushing to Disk' | |
ssh.exec!("mongo --eval 'db.runCommand({fsync:1,lock:1});' admin") unless pretend | |
puts '.. Syncing disks' | |
ssh.exec!('sync') unless pretend | |
server.volumes.all('tag-value' => '%s %s' % [ srv, mount ]).each do |vol| | |
puts '... %s [ %s => %s ]' % [ srv, mount, vol.id ] | |
snapshots << vol.snapshots.create unless pretend | |
end | |
puts '.. MongoDB Unlocking Tables' | |
ssh.exec!("mongo --eval 'db.$cmd.sys.unlock.findOne();' admin") unless pretend | |
end | |
puts '.. Tagging Snapshots' | |
snapshots.each do |snapshot| | |
snapshot.reload | |
{ | |
'Name' => '%s %s' % [ srv, mount ], | |
'backup_date' => now_date, | |
'backup_time' => now_time | |
}.each_pair do |k,v| | |
AWS.tags.create( | |
:key => k, | |
:value => v, | |
:resource_id => snapshot.id, | |
:resource_type => 'snapshot' | |
) | |
end | |
end | |
puts '.. Done (%.2f)' % (Time.now - start) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment