Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Push / Pull Wordpress Databases to / from remote and dev environments
namespace :db do
desc "Pull (dump) the remote database and replace the local database with it"
task :pull do
on roles(:app) do
within release_path do
with path: "#{fetch(:path)}:$PATH" do
execute :mysqldump, "-u #{fetch(:wpdb)[fetch(:stage)][:user]} -p\"#{fetch(:wpdb)[fetch(:stage)][:password]}\" -h #{fetch(:wpdb)[fetch(:stage)][:host]} #{fetch(:wpdb)[fetch(:stage)][:name]} > #{fetch(:tmp_dir)}/database.sql"
download! "#{fetch(:tmp_dir)}/database.sql", "database.sql"
execute :rm, "#{fetch(:tmp_dir)}/database.sql"
end
end
run_locally do
sql = File.read("database.sql")
File.open("database.sql", "w") {|file| file.puts sql.gsub("#{fetch(:wpdb)[fetch(:stage)][:domain]}", "#{fetch(:wpdb)[:development][:domain]}")}
execute "mysql -u #{fetch(:wpdb)[:development][:user]} -p#{fetch(:wpdb)[:development][:password]} -h #{fetch(:wpdb)[:development][:host]} #{fetch(:wpdb)[:development][:name]} < database.sql"
execute :rm, "database.sql"
end
end
end
desc "Push (dump) the local database and replace the remote database with it"
task :push do
on roles(:app) do
run_locally do
execute "mysqldump -u #{fetch(:wpdb)[:development][:user]} -p#{fetch(:wpdb)[:development][:password]} -h #{fetch(:wpdb)[:development][:host]} #{fetch(:wpdb)[:development][:name]} > database.sql"
sql = File.read("database.sql")
File.open("database.sql", "w") {|file| file.puts sql.gsub("#{fetch(:wpdb)[:development][:domain]}", "#{fetch(:wpdb)[fetch(:stage)][:domain]}")}
end
upload! "database.sql", "#{fetch(:tmp_dir)}/database.sql"
run_locally do
execute :rm, "database.sql"
end
within release_path do
with path: "#{fetch(:path)}:$PATH" do
execute :mysql, "-u #{fetch(:wpdb)[fetch(:stage)][:user]} -p\"#{fetch(:wpdb)[fetch(:stage)][:password]}\" -h #{fetch(:wpdb)[fetch(:stage)][:host]} #{fetch(:wpdb)[fetch(:stage)][:name]} < #{fetch(:tmp_dir)}/database.sql"
execute :rm, "#{fetch(:tmp_dir)}/database.sql"
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.