Backup your production database with capistrano
Say you have a remote server and you want to back up your remote DB to your home machine but you're behind a firewall. Here's my quick solution/hack of the default capistrano backup task. Remotely, it will dump your database to /tmp and bzip2 it. Locally, it will scp the file to your RAILS_ROOT/backups. You need to have all your ssh keys set up for this to work. It probably will fail if you're using a gateway.

role :db,  "caboo.se", :primary => true
set :user, "bananas"

desc "Backup the database"
task :backup, :roles => :db, :only => { :primary => true } do
  filename = "/tmp/#{application}.dump.#{Time.now.to_f}.sql.bz2"

  on_rollback { delete filename }
  run "mysqldump -u root -p mephisto_production | bzip2 -c > #{filename}" do |ch, stream, out|
    ch.send_data "assword\n" if out =~ /^Enter password:/
    # set this to your db password.. yuk!
  end
  `rsync #{user}@#{roles[:db][0].host}:#{filename} #{File.dirname(__FILE__)}/../backups/`
  delete filename
end

Invoke with rake remote:exec ACTION=backup Suggested improvements: - is it necessary to shell out to scp? (update: now uses rsync) - is there a better way of doing the db password? - any way to use the 'backup gem', a favorite of rails professionals everywhere?
Language Ruby / Tagged with rails, capistrano, backup