Capistrano Task For Importing Remotely Hosted Assets to Local Directory

This is related to my post about using Capistrano to retrieve the a database from a remote host and import to local database.

When I was a hard worker (and sometimes, I still am), I used to just use FTP to download files. The problem with FTP is that it is slow (think it’s slower than SCP and Rsync). And if you don’t have a good memory (I don’t by the way), it’s not best to use FTP. You’d think.. “So what file did I just download? Should I download the whole directory?”

Anyway your config/deploy.rb file should look like this if you use Passenger. The deploy file and the Capfile is generated using the “capify .” command. There’s a period after.


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

default_run_options[:pty] = true
ssh_options[:forward_agent] = true

# be sure to change these
set :user, 'user'
set :domain, 'domain'
set :deploy_to, "~/public_html/#{domain}"



# the rest should be good
set :repository,  "somewhere" 
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
role :db, domain, :primary => true



task :production_config, :roles => [:app, :db] do
  run "ln -nfs #{shared_path}/config/database.yml #{release_path}/config/database.yml"
end


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


after "deploy:update_code", :production_config
after "deploy:symlink", "deploy:cleanup"


For those who use Paperclip gem, the default directory for assets on public/system. So if you want to copy files uploaded on the server, Capistrano would be useful. In fact, it is useful for handling a lot of other Rails-related tasks and server administration tasks.

# Capfile 

desc "Mirrors the remote shared public directory with your local copy, doesn't download symlinks"
task :import_remote_assets do
   run_locally("rsync --recursive --times --rsh=ssh --compress --human-readable --progress #{user}@#{domain}:~/public_html/#{domain}/shared/system public/")
end


Usage:

cap import_remote_assets