Guest User

Untitled

a guest
Apr 19th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.34 KB | None | 0 0
  1. desc "Load production data into development database"
  2. task :import_remote_db do
  3.  
  4. filename = "dump.#{Time.now.strftime '%Y-%m-%d_%H:%M:%S'}.sql"
  5. dbuser = "yourusername"
  6. dbhost = "yourhostname"
  7. dbpassword = "yourpassword"
  8. application_db = "yourdatabasename"
  9. local_db_host = "localhost"
  10. local_db_user = "local_user_name"
  11. local_db_password = "local_password"
  12. local_db = "localdatabasename"
  13.  
  14.  
  15. on_rollback do
  16. delete "/tmp/#{filename}"
  17. delete "/tmp/#{filename}.gz"
  18. end
  19.  
  20. cmd = "mysqldump --opt --compress -u #{dbuser} --password=#{dbpassword} --host=#{dbhost} #{application_db} > /tmp/#{filename}"
  21. puts "Dumping remote database"
  22. run(cmd) do |channel, stream, data|
  23. puts data
  24. end
  25.  
  26. # compress the file on the server
  27. puts "Compressing remote data"
  28. run "gzip -9 /tmp/#{filename}"
  29. puts "Fetching remote data"
  30. get "/tmp/#{filename}.gz", "dump.sql.gz"
  31.  
  32. # build the import command
  33. # no --password= needed if password is nil.
  34. if local_db_password.nil?
  35. cmd = "mysql -u #{local_db_user} #{local_db} < dump.sql"
  36. else
  37. cmd = "mysql -u #{local_db_user} --password=#{local_db_password} #{local_db} < dump.sql"
  38. end
  39.  
  40. # unzip the file. Can't use exec() for some reason so backticks will do
  41. puts "Uncompressing dump"
  42. `gzip -d dump.sql.gz`
  43. puts "Executing : #{cmd}"
  44. `#{cmd}`
  45. puts "Cleaning up"
  46. `rm -f dump.sql`
  47.  
  48. end
Add Comment
Please, Sign In to add comment