Skip to content

Instantly share code, notes, and snippets.

@nwjsmith
Created May 4, 2010 13:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nwjsmith/389415 to your computer and use it in GitHub Desktop.
Save nwjsmith/389415 to your computer and use it in GitHub Desktop.
# 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