CAP DB DUMP
Capistrano recipes for backing your production database and transferring it to your local machine.
INSTALLATION
-
Install the gem in bundler’s Gemfile:
gem 'cap_db_dump' $ bundle install
-
Require the gem in the cap file:
require 'cap_db_dump/recipes'
Finally, add the :db_dump => true flag on the server where you’d like the mysqldump to run:
task :staging do role :web, 'example.com' role :app, 'example.com' role :db, 'example.com', :primary => true, :db_dump => true end
Make sure rails_env and database_engine variables are set
set :rails_env, "production" set :database_engine, :mysql # specify :mysql | :psql
You also might want to add these to your .gitignore:
“‘ current.sql.gz dump_.sql.gz “`
USAGE
Create a dump:
cap database:dump
Create a dump, and transfer it with scp:
cap database:dump_and_transfer
Only dump the schema for a given set of tables (don’t dump the data):
set :schema_only_tables, [:sessions, :my_large_table]
With postgres this uses pg_dump –exclude-table-data, so the tables are still created on restore, just empty.
DATABASE CONFIG WITH ERB
The database settings are read from config/database.yml in the current release. If that file uses ERB (for example, to pull credentials out of Rails.application.credentials), it can’t be read as plain YAML. In that case the settings are fetched from Rails on the server instead, with:
cd <current_path> && RAILS_ENV=<rails_env> bundle exec rails runner "..."
This boots your app on the db_dump server, so it needs Rails 6.1+ and the credentials key to be present there.
Control this with :database_config_source:
set :database_config_source, :auto # default. plain YAML, unless database.yml contains <% set :database_config_source, :yaml # always read database.yml as plain YAML (the old behavior) set :database_config_source, :rails # always ask rails runner for the resolved config
Restore script
Restore script is not included, but you may want to do something like this (postgres example):
#!/usr/bin/env bash set -x createdb my_app_development set -e gunzip -c current.sql.gz -k | pg_restore --verbose --clean --no-acl --no-owner -h localhost -d my_app_development
Contributors:
Conor Hunt (conorh) Stephen Schor (nodanaonlyzuul)
COPYRIGHT
Copyright © 2008-2022 Scott Taylor / Eastmedia <scott@eastmedia.com>
Released under the MIT + GPL licenses