Guest User

Untitled

a guest
May 1st, 2018
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.60 KB | None | 0 0
  1. # Directly copied from eycap-0.5.2 (thanks!)
  2. #
  3. # With these tasks you can:
  4. # - dump your production database and save it in shared_path/db_backups
  5. # - dump your production into your local database (clone_to_local)
  6. #
  7. # Right now is not tested without all the gem. Give me a moment to check it :-)
  8. Capistrano::Configuration.instance(:must_exist).load do
  9.  
  10. namespace :db do
  11. task :backup_name, :roles => :db, :only => { :primary => true } do
  12. now = Time.now
  13. run "mkdir -p #{shared_path}/db_backups"
  14. backup_time = [now.year,now.month,now.day,now.hour,now.min,now.sec].join('-')
  15. set :backup_file, "#{shared_path}/db_backups/#{environment_database}-snapshot-#{backup_time}.sql"
  16. end
  17.  
  18. desc "Backup your MySQL or PostgreSQL database to shared_path+/db_backups"
  19. task dump, :roles => :db, :only => {:primary => true} do
  20. backup_name
  21. run("cat #{shared_path}/config/database.yml") { |channel, stream, data| @environment_info = YAML.load(data)[rails_env] }
  22. dbuser = @environment_info['username']
  23. dbpass = @environment_info['password']
  24. environment_database = @environment_info['database']
  25. dbhost = @environment_info['host']
  26. if @environment_info['adapter'] == 'mysql'
  27. #dbhost = environment_dbhost.sub('-master', '') + '-replica' if dbhost != 'localhost' # added for Solo offering, which uses localhost
  28. run "mysqldump --add-drop-table -u #{dbuser} -h #{dbhost} -p #{environment_database} | bzip2 -c > #{backup_file}.bz2" do |ch, stream, out |
  29. ch.send_data "#{dbpass}\n" if out=~ /^Enter password:/
  30. end
  31. else
  32. run "pg_dump -W -c -U #{dbuser} -h #{dbhost} #{environment_database} | bzip2 -c > #{backup_file}.bz2" do |ch, stream, out |
  33. ch.send_data "#{dbpass}\n" if out=~ /^Password:/
  34. end
  35. end
  36. end
  37.  
  38. desc "Sync your production database to your local workstation"
  39. task :clone_to_local, :roles => :db, :only => {:primary => true} do
  40. backup_name
  41. dump
  42. get "#{backup_file}.bz2", "/tmp/#{application}.sql.bz2"
  43. development_info = YAML.load_file("config/database.yml")['development']
  44. if development_info['adapter'] == 'mysql'
  45. run_str = "bzcat /tmp/#{application}.sql.bz2 | mysql -u #{development_info['username']} --password='#{development_info['password']}' -h #{development_info['host']} #{development_info['database']}"
  46. else
  47. run_str = "PGPASSWORD=#{development_info['password']} bzcat /tmp/#{application}.sql.bz2 | psql -U #{development_info['username']} -h #{development_info['host']} #{development_info['database']}"
  48. end
  49. %x!#{run_str}!
  50. end
  51.  
  52. end
  53. end
Add Comment
Please, Sign In to add comment