Skip to content

Instantly share code, notes, and snippets.

@rikvanderkemp
Created August 2, 2016 17:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rikvanderkemp/de27df9c9758248b5bea42229d5f3ac0 to your computer and use it in GitHub Desktop.
Save rikvanderkemp/de27df9c9758248b5bea42229d5f3ac0 to your computer and use it in GitHub Desktop.
Capistrano - Load a Mysql dump from a host to local machine
import 'config/local.rb'
namespace :content do
desc "Fetch database from target environment"
task :pull do
on roles(:web) do |host|
file_name = "content-#{fetch(:stage)}-" + Time.now.to_i.to_s
execute "mysqldump --opt -Q #{fetch(:database_name)} > db.sql"
execute "tar czf #{file_name}.tar db.sql --remove-files"
run_locally do
execute "scp #{host.user}@#{fetch(:host_name)}:~/#{file_name}.tar ."
ask :load, "Do you want to load the retrieved dump into your local database? Y/n", "y"
if (fetch(:load) == "y")
execute "tar xvf #{file_name}.tar"
execute "mysql #{fetch(:local_database_name)} < db.sql"
execute "rm db.sql"
end
end
execute "rm #{file_name}.tar"
end
end
end
# Contains local settings, used for local tasks
set :local_database_name, 'DBNAME'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment