Guest User

Untitled

a guest
Mar 1st, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.71 KB | None | 0 0
  1. # Task has the following dependencies
  2. # net/ssh gem [gem install net-ssh]
  3. # net/sftp gem [gem install net-sftp]
  4. # rsync command line utility
  5. # yamb_db rails plugin [ruby script/plugin install git://github.com/adamwiggins/yaml_db.git]
  6.  
  7. desc "This task will make your development environment download and import a fresh copy of your staging database and also sync any folders such as image uploads so you can quickly replicate your staging/production server content locally"
  8. task :sync_with_staging => :environment do
  9. @remote_host = 'yourremoteserver.com'
  10. @remote_user = 'yoursshuser'
  11. @remote_password = "yoursshpassword"
  12. @remote_environment = "staging"
  13. @remote_path_to_rails = "/home/rails/myproject/current"
  14. @directories_to_sync = {"/home/rails/myproject/shared/system" => "#{RAILS_ROOT}/public"}
  15.  
  16. require 'net/ssh'
  17. require 'net/sftp'
  18.  
  19. #Delete our old local data.yml file
  20. File.delete("#{RAILS_ROOT}/db/data.yml") if File::exists?("#{RAILS_ROOT}/db/data.yml")
  21.  
  22. #Create a new data.yml file on the staging server
  23. Net::SSH.start(@remote_host, @remote_user, :password => @remote_password) do |ssh|
  24. output = ssh.exec!("hostname")
  25. ssh.exec "rake -f #{@remote_path_to_rails}/Rakefile RAILS_ENV=#{@remote_environment} db:data:dump"
  26.  
  27. #Now download the data.yml file over the ssh connection
  28. ssh.sftp.connect do |sftp|
  29. sftp.download!("#{@remote_path_to_rails}/db/data.yml", "#{RAILS_ROOT}/db/data.yml")
  30. end
  31. end
  32.  
  33. #Import the new data.yml file
  34. Rake::Task["db:data:load"].invoke
  35.  
  36. #Sync the uploads
  37. @directories_to_sync.each do |remote_path, local_path|
  38. sh("rsync -vra #{@remote_user}@#{@remote_host}:#{remote_path} #{local_path}")
  39. end
  40. end
Add Comment
Please, Sign In to add comment