Rails Changeset 6009 includes my patch that adds a “get” helper, to pull a file from a remote server to the localhost. More info about get and example to create backup of production server database is available in previuous posts.
Based on and using capistrano get method posted few days before here comes capistrano recipe to backup database on remote server.
desc <<DESC
Backup remote database from primary server.
DESC
task :remote_backup, :roles => :db, :only => { :primary => true } do
filename = "dump.#{Time.now.strftime '%Y%m%dT%:%H%M%S'}.sql"
on_rollback { delete "/tmp/#{filename}" }
run "mysqldump -uusername -ppassword database_name > \n
/tmp/#{filename}" do |channel, stream, data|
puts data
end
get "/tmp/#{filename}", "c:/backup/#{filename}"
delete "/tmp/#{filename}"
end
Still looking if there is better way to do this (and I bet there it is :), but until I realize, add to top of deploy.rb:
# Get file remote_path from FIRST server targetted by
# the current task and transfer it to local machine as path, SFTP required
def actor.get(remote_path, path, options = {})
execute_on_servers(options) do |servers|
self.sessions[servers.first].sftp.connect do |tsftp|
logger.info "Get #{remote_path} to #{path}"
tsftp.get_file remote_path, path
end
end
end
Sample recipe to get production log from server:
task :download_log, :roles => :web, :only => { :primary => true } do
get "#{deploy_to}/current/log/production.log",
"log/production.log.web" //download server log
end