Skip to content

Instantly share code, notes, and snippets.

@newbamboo
Created January 19, 2009 17:29
Show Gist options
  • Select an option

  • Save newbamboo/49073 to your computer and use it in GitHub Desktop.

Select an option

Save newbamboo/49073 to your computer and use it in GitHub Desktop.
S3 database backup adapted for Merb
# S3 Backup Task for MySQL
# Assumes InnoDB tables
# Database User needs the "reload" permission on the database (for --flush-logs in mysqldump)
#
# Stores files in Amazon S3 using the excellent AWS Gem: http://amazon.rubyforge.org/
# For information about Amazon S3: http://aws.amazon.com/s3
#
# Installation
# 1) Install AWS Gem
# 2) Enter your S3 Bucket, access_key_id and secret_access_key in this file
# 3) Run (rake db:backup)
#
# Inspired by code from:
# http://blog.craigambrose.com/articles/2007/03/01/a-rake-task-for-database-backups
# http://www.rubyinside.com/advent2006/15-s3rake.html
namespace :db do
require "aws/s3"
desc "Backup database to Amazon S3"
task :backup => :merb_env do
BUCKET = "bucket"
begin
AWS::S3::Base.establish_connection!(
:access_key_id => "",
:secret_access_key => ""
)
db_config = ActiveRecord::Base.configurations[Merb.environment.to_sym]
backup_path = "schema"
datestamp = Time.now.strftime("%Y%m%d%H%M%S")
file_name = "#{db_config[:database]}_#{Merb.environment}_#{datestamp}.sql.gz"
backup_file = File.join(backup_path, file_name)
sh "mysqldump -u #{db_config[:username]} --single-transaction --flush-logs --add-drop-table --add-locks --create-options --disable-keys --extended-insert --quick #{db_config[:database]} | gzip -c > #{backup_file}"
puts "Created backup: #{file_name}"
puts "Storing file in S3: #{BUCKET}"
AWS::S3::S3Object.store(file_name, open(backup_file), BUCKET)
FileUtils.rm backup_file
puts "Backup Complete"
rescue AWS::S3::ResponseError => error
puts error.response.code.to_s + ": " + error.message
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment