-
-
Save davidphasson/104135 to your computer and use it in GitHub Desktop.
Amazon S3 Backup Example
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
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 " + full_filename | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment