etskelly/holiday_machine — Issue #38: Cheap database backups on deploy
Author: damian
Created: 2011-11-23 12:00:06+00:00
State: open

Slightly modified from my original capistrano script to dump out a copy of the current database and copy it to the local machine that's performing the deploy. The step to remove the database dump is optional if your happy keeping a directory of dumps.

Will need tweaking to set up the proper paths and to be set up on a before deploy hook.

```ruby
namespace :db do
desc 'Backup database'
task :backup => :environment do
# Loads in database config and finds the correct environment
config = YAML::load(File.open('config/database.yml'))[Rails.env]

# Creates a filename which is unique to the backup
filename = "backup_#{Time.now.to_i}"

# Run a mysqldump using our credentials from database config
`mysqldump -u#{config['username']} -p#{config['password']} #{config['database']} > tmp/#{filename}.sql`

# Copy the backup from the remote box to our local machine
get "tmp/#{filename}", "~/Desktop/#{filename}"

# Delete the backup on the remote box
delete "tmp/#{filename}"
end
end
```