Created
August 2, 2016 17:04
-
-
Save rikvanderkemp/de27df9c9758248b5bea42229d5f3ac0 to your computer and use it in GitHub Desktop.
Capistrano - Load a Mysql dump from a host to local machine
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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