Created
June 17, 2010 21:08
-
-
Save datapimp/442783 to your computer and use it in GitHub Desktop.
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
require 'aws/s3' | |
set :s3_key, "<PUT YOUR KEY HERE>" | |
set :s3_secret, "< PUT YOUR KEY HERE >" | |
set :s3_buckets, { | |
"production" => "<PUT YOUR BUCKET NAME HERE>", | |
"development" => "<PUT YOUR BUCKET NAME HERE>" | |
} | |
set :s3_ignore, ["vendor","backups","coverage"] | |
namespace :s3_backup do | |
task :all do | |
db | |
app | |
end | |
task :connect do | |
AWS::S3::Base.establish_connection!( | |
:access_key_id => fetch(:s3_key), | |
:secret_access_key => fetch(:s3_secret) | |
) | |
end | |
desc "Setup your S3 Buckets and Backup Folders" | |
task :setup, :roles => :db do | |
run "mkdir -p #{shared_path}/backups/app" | |
system "mkdir -p backups/app" | |
run "mkdir -p #{shared_path}/backups/db" | |
system "mkdir -p backups/db" | |
buckets = fetch(:s3_buckets) | |
buckets.keys.each do |environment| | |
AWS::S3::Bucket.create( buckets[environment] ) | |
end | |
end | |
desc "Backup your application and copy to S3" | |
task :app, :roles => :app do | |
rails_env = fetch(:rails_env,"production") | |
filename = "#{application}-#{ rails_env }.app-backup.#{Time.now.to_i}.bz2" | |
dir = "#{shared_path}/backups/app" | |
file = "#{dir}/#{filename}" | |
targets = Dir.glob("*") | |
fetch(:s3_ignore).each {|dir| targets.delete(dir) } | |
run "cd #{ current_path }; tar -c #{ targets.join(" ") } |bzip2 -c > #{ shared_path }/backups/app/#{ filename }" | |
get file, "backups/app/#{ filename }" | |
puts " * Uploading to S3" | |
AWS::S3::S3Object.store( | |
"#{ filename }", | |
File.open("backups/app/#{ filename }"), | |
fetch(:s3_buckets)[rails_env] | |
) | |
end | |
desc "Backup your database and copy the file to S3" | |
task :db, :roles => :db do | |
rails_env = fetch(:rails_env,"production") | |
filename = "#{application}-#{ rails_env }.db-backup.#{Time.now.to_i}.sql.bz2" | |
dir = "#{shared_path}/backups/db" | |
file = "#{dir}/#{filename}" | |
on_rollback { delete file } | |
db = YAML::load(ERB.new(IO.read(File.join('config', 'database.yml'))).result)[rails_env] | |
dump_cmd = "mysqldump -u #{db['username']} --password=#{db['password']} #{db['database']} | bzip2 -c > #{file}" | |
run(dump_cmd) do |ch, stream, data| | |
puts data | |
end | |
system "mkdir -p backups/db" | |
get file, "backups/db/#{ filename }" | |
puts " * Uploading to S3" | |
AWS::S3::S3Object.store( | |
"#{ filename }", | |
File.open("backups/db/#{ filename }"), | |
fetch(:s3_buckets)[rails_env] | |
) | |
end | |
before "s3_backup:setup", "s3_backup:connect" | |
before "s3_backup:db", "s3_backup:connect" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment