How to dump the database whenever you deploy with Capistrano
We often want to do a database dump before deployment of new releases. This backs you up in case migrations touch your data in a unfavorable way or a new feature is buggy.
I already introduced you to our dumple script in order to dump from within rails projects.
We often use it for this purpose. To do so, you need dumple on the remote machine. Additionally add two tasks to the database namespace and two callbacks in config/deploy.rb:
namespace :db do
...
desc "Do a dump of the DB on the remote machine using dumple"
task :dump do
rails_env = fetch(:rails_env, 'production')
run "cd #{current_path}; dumple --fail-gently #{rails_env}"
end
desc "Show usage of ~/dumps/ on remote host"
task :show_dump_usage do
run "dumple -i "
end
end
...
before "deploy", "db:dump"
after "deploy", "db:show_dump_usage"
Next time you cap deploy you will see some output before and after the deployment.
The first (db:dump) indicates whether the dump was successful and prints its size. The second one (db:show_dump_usage) shows the overall consumption of the dumps-directory.
If you still wonder about the --fail-gently parameter: database.yml does not exists when you run your first deployment of a project. If you raise within a Ruby script the exit code is 1 and Capistrano stops. The gently parameter makes dumple use exit, which results in a exit code of 0 and Capistrano continues!
You can follow any response to this post through the Atom feed.


