task :backup, :roles => :db, :only => { :primary => true } do
# the on_rollback handler is only executed if this task is executed within
# a transaction (see below), AND it or a subsequent task fails.
filename = "/tmp/#{application}.dump.#{Time.now.to_f}.sql.bz2"
text = capture "cat #{deploy_to}/#{shared_dir}/system/database.yml"
yaml = YAML::load(text)
on_rollback { delete filename }
run "mysqldump -u #{yaml['production']['username']} -p #{yaml['production']['database']} | bzip2 -c > #{filename}" do |ch, stream, out|
ch.send_data "#{yaml['production']['password']}\n" if out =~ /^Enter password:/
end
`mkdir -p #{File.dirname(__FILE__)}/../backups`
`rsync #{user}@#{roles[:db][0].host}:#{filename} #{File.dirname(__FILE__)}/../backups/`
delete filename
public_dir_backup
end