Forked from kathgironpe/Capistrano task to load production data into development database
Created
March 15, 2012 11:43
-
-
Save grammaticof/2043815 to your computer and use it in GitHub Desktop.
Capistrano: task to load production data into development database
This file contains hidden or 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
desc "Load production data into development database" | |
task :import_remote_db do | |
filename = "dump.#{Time.now.strftime '%Y-%m-%d_%H:%M:%S'}.sql" | |
dbuser = "yourusername" | |
dbhost = "yourhostname" | |
dbpassword = "yourpassword" | |
application_db = "yourdatabasename" | |
local_db_host = "localhost" | |
local_db_user = "local_user_name" | |
local_db_password = "local_password" | |
local_db = "localdatabasename" | |
on_rollback do | |
delete "/tmp/#{filename}" | |
delete "/tmp/#{filename}.gz" | |
end | |
cmd = "mysqldump --opt --compress -u #{dbuser} --password=#{dbpassword} --host=#{dbhost} #{application_db} > /tmp/#{filename}" | |
puts "Dumping remote database" | |
run(cmd) do |channel, stream, data| | |
puts data | |
end | |
# compress the file on the server | |
puts "Compressing remote data" | |
run "gzip -9 /tmp/#{filename}" | |
puts "Fetching remote data" | |
get "/tmp/#{filename}.gz", "dump.sql.gz" | |
# build the import command | |
# no --password= needed if password is nil. | |
if local_db_password.nil? | |
cmd = "mysql -u #{local_db_user} #{local_db} < dump.sql" | |
else | |
cmd = "mysql -u #{local_db_user} --password=#{local_db_password} #{local_db} < dump.sql" | |
end | |
# unzip the file. Can't use exec() for some reason so backticks will do | |
puts "Uncompressing dump" | |
`gzip -d dump.sql.gz` | |
puts "Executing : #{cmd}" | |
`#{cmd}` | |
puts "Cleaning up" | |
`rm -f dump.sql` | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment