Created
July 2, 2010 00:56
-
-
Save bradgessler/460768 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
#!/usr/bin/env ruby | |
require 'rubygems' | |
require 'aws/s3' # sudo gem install aws-s3 | |
require 'logger' | |
require 'optparse' | |
AWS::S3::Base.establish_connection!( | |
:access_key_id => 'TODO', | |
:secret_access_key => 'TODO' | |
) | |
options = { | |
:user => 'root', | |
:logfile => $stdout, | |
:bucket => "TODO" | |
} | |
parser = OptionParser.new do |opts| | |
opts.on('-u', '--user [OPT]', 'Output more information' ) do |user| | |
options[:user] = user | |
end | |
opts.on('-l', '--logfile [OPT]', 'Logfile path' ) do |path| | |
options[:logfile] = path | |
end | |
opts.on('-b', '--as3-bucket [OPT]', 'S3 bucket' ) do |bucket| | |
options[:bucket] = bucket | |
end | |
opts.on('-h', '--help', 'Help') do | |
puts opts | |
exit | |
end | |
end.parse! | |
database = ARGV.last | |
logger = Logger.new(options[:logfile]) | |
file = "dump_#{database}_#{Time.now.to_i}.gz" | |
path = File.join(File.dirname(__FILE__), file) | |
logger.info "Dumping #{database}..." | |
`mysqldump -u #{options[:user]} #{database} --single-transaction --quick | gzip > #{file}` | |
logger.info "Dumped #{File.size(file)} bytes to #{file}" | |
logger.info "Uploading #{file} to S3 bucket #{options[:bucket]}..." | |
AWS::S3::S3Object.store(file, File.open(file), options[:bucket], :use_ssl => true) | |
logger.info "Succesfully uploaded #{file}" | |
logger.info "Cleaning up #{path}" | |
`rm #{path}` | |
logger.info "Backup succesfull!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment