Skip to content

Instantly share code, notes, and snippets.

@bensie
Created April 21, 2009 23:21
Show Gist options
  • Save bensie/99465 to your computer and use it in GitHub Desktop.
Save bensie/99465 to your computer and use it in GitHub Desktop.
MySQL Database Backup => Amazon S3
mysql_username = "backup"
mysql_password = ""
databases = [ 'db1', 'sb2' ]
temp_directory = "/mnt/storage/backups"
s3_access_key_id = 'xxxx'
s3_secret_access_key = 'xxxx'
s3_bucket_name = 'database-bucket'
require 'rubygems'
require 'aws/s3'
databases.each do |db|
now = Time.now.strftime("%Y-%m-%d-%H-%M-%S")
filename = "#{db}-#{now}.sql"
puts "Dumping #{db} to SQL file"
`mysqldump --user=#{mysql_username} --password=#{mysql_password} #{db} > #{temp_directory}/#{db}-#{now}.sql`
puts "...done\n\n"
puts "Gzipping #{db}\n"
`gzip #{temp_directory}/#{db}-#{now}.sql`
puts "...done\n\n"
filename = filename + ".gz"
puts "Sending #{filename} to Amazon S3\n"
AWS::S3::Base.establish_connection!(
:access_key_id => s3_access_key_id,
:secret_access_key => s3_secret_access_key
)
AWS::S3::S3Object.store(
filename,
File.open(temp_directory + "/" + filename),
s3_bucket_name,
:content_type => 'application/x-gzip',
:access => :private
)
if AWS::S3::Service.response.success?
# Remove the file on the local filesystem
FileUtils.rm temp_directory + "/" + filename
puts "...done\n\n"
else
puts "...there was a problem uploading " + filename + "\n\n"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment