• Download Gist
Capistrano task to load production data into development database
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
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`
 
puts "Be sure to run rake db:migrate to ensure your database schema is up to date!"
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.