Backing up your DB on a Capistrano deployment

Yeah, it’s simple but useful. Since I’m using sqlite at the moment, it’s just a copy but you could run any command in a similar way. I’m also tagging the backup name with a date. Here’s a snip from my Capistrano deploy file….

# ...snip...
namespace :deploy do
  task :restart do
    t=Time.now
    run "cp /path/to/db.sqlite3 /path/to/backup/#{t.year}-#{t.month}-#{t.day}_#{t.hour}:#{t.min}:#{t.sec}_db.sqlite3"
    run "touch #{current_path}/tmp/restart.txt" 
  end
end

Capistrano deploy to multiple locations

Pretty simple, but wanted to log here so I can look this up in the future…

I wanted to deploy to both a stage and a live site. Here’s how that went down:

First I installed the capistrano-ext gem. Second, I edited my Capfile to look like this:

require 'capistrano/ext/multistage'
set :default_stage, "beta"
set :stages, %w(beta live)

Third, I created a deploy folder and added my two recipes there. beta.rb looks like this:

load 'deploy' if respond_to?(:namespace) # cap2 differentiator

default_run_options[:pty] = true

# be sure to change these
set :user, 'username'
set :domain, 'beta.example.org'
set :application, 'fundastache'

# the rest should be good
set :repository,  "#{user}@#{domain}:git/#{application}.git" 
#set :repository,  "/home/#{user}/git/#{application}.git" 
set :deploy_to, "/home/#{user}/#{domain}" 
set :deploy_via, :remote_cache
set :scm, 'git'
set :branch, 'master'
set :git_shallow_clone, 1
set :scm_verbose, true
set :use_sudo, false

server domain, :app, :web

namespace :deploy do
  task :restart do
    run "touch #{current_path}/tmp/restart.txt" 
  end
end

The live recipe looks almost identical.

Now when I

cap live deploy

the site is deployed to the live environment and when I

cap beta deploy

or simply

cap deploy

the site is deployed to the beta site.