Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save kathgironpe/229147 to your computer and use it in GitHub Desktop.
Save kathgironpe/229147 to your computer and use it in GitHub Desktop.
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