Created
March 3, 2014 09:49
-
-
Save mkurzeja/9321662 to your computer and use it in GitHub Desktop.
Capifony restore dump on remote server
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
namespace :database do | |
namespace :restore do | |
desc "Uploads latest db dump to remote and populates data into db" | |
task :remote, :roles => :db, :only => { :primary => true } do | |
filename = "#{application}.remote_dump.latest.sql.gz" | |
file = "#{backup_path}/#{filename}" | |
sqlfile = "#{application}_dump.sql" | |
config = "" | |
upload(file, "#{remote_tmp_dir}/#{filename}", :via => :scp) | |
run "#{try_sudo} gunzip -c #{remote_tmp_dir}/#{filename} > #{remote_tmp_dir}/#{sqlfile}" | |
data = capture("#{try_sudo} cat #{current_path}/#{app_config_path}/#{app_config_file}") | |
config = load_database_config data, symfony_env_prod | |
case config['database_driver'] | |
when "pdo_mysql", "mysql" | |
data = capture("#{try_sudo} mysql -u#{config['database_user']} --host='#{config['database_host']}' --password='#{config['database_password']}' #{config['database_name']} < #{remote_tmp_dir}/#{sqlfile}") | |
puts data | |
when "pdo_pgsql", "pgsql" | |
data = capture("#{try_sudo} PGPASSWORD=\"#{config['database_password']}\" psql -U #{config['database_user']} #{config['database_name']} -h#{config['database_host']} < #{remote_tmp_dir}/#{sqlfile}") | |
puts data | |
end | |
run "#{try_sudo} rm -f #{remote_tmp_dir}/#{filename}" | |
run "#{try_sudo} rm -f #{remote_tmp_dir}/#{sqlfile}" | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment