Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
# Credit to Brandon Keepers
# http://opensoul.org/2007/2/9/automatically-backing-up-your-remote-database-on-deploy
require 'yaml'
desc "Backup the remote production database"
task :backup, :roles => :db, :only => { :primary => true } do
filename = "#{application}.dump.#{Time.now.to_i}.sql.bz2"
file = "/tmp/#{filename}"
env = ENV['RAILS_ENV'] || 'development'
on_rollback { delete file }
db = YAML::load(ERB.new(IO.read(File.join(File.dirname(__FILE__), 'database.yml'))).result)[env]
run "pg_dump -u #{db['username']} --password=#{db['password']} #{db['database']} | bzip2 -c > #{file}" do |ch, stream, data|
puts data
end
`mkdir -p #{File.dirname(__FILE__)}/../backups/`
get file, "backups/#{filename}"
delete file
end
desc "Backup the database before running migrations"
task :before_migrate do
backup
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment