Advertisement
Guest User

Untitled

a guest
Dec 9th, 2016
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.49 KB | None | 0 0
  1. namespace :accio do
  2. desc "Drop/restore database from Heroku backup"
  3. task :db do
  4. Rake::Task['accio:download_backup'].invoke
  5. Rake::Task['accio:restore_backup'].invoke
  6. end
  7.  
  8. desc "Download database dump from Heroku"
  9. task :download_backup => [:logger_no_env, '~/.netrc'] do |t, args|
  10. return Rails.logger.error('curl not installed') unless \
  11. command?('curl')
  12. Rails.logger.info "Downloading Heroku database backup"
  13. Bundler.with_clean_env do
  14. url = `heroku pg:backups public-url --app itsallgravy`.chomp
  15. `curl -o #{filename} '#{url}'`
  16. end
  17. end
  18.  
  19. desc "drops db and restores from backup"
  20. task :restore_backup => [:logger] do |t, args|
  21. return Rails.logger.error('pg_restore not installed') unless \
  22. command?('pg_restore')
  23. return Rails.logger.error('no db backup found') unless \
  24. File.exists?(filename)
  25.  
  26. ActiveRecord::Base.connection
  27. db_config = ActiveRecord::Base.connection_config
  28. database = db_config[:database]
  29. username = db_config[:username]
  30. host = db_config[:host]
  31.  
  32. Rails.logger.info "Drop/creating database"
  33. Rake::Task['db:drop'].invoke
  34. Rake::Task['db:create'].invoke
  35.  
  36. Rails.logger.info "Restoring from database backup"
  37. cmd = []
  38. cmd << 'pg_restore'
  39. cmd << "-d #{database}"
  40. cmd << "-U #{username}"
  41. cmd << "-h #{host}"
  42. cmd << "#{filename}"
  43. system cmd.join(' ')
  44. end
  45. end
  46.  
  47. def command?(command)
  48. system "which #{command} > /dev/null 2>&1"
  49. end
  50.  
  51. def filename
  52. "#{Rails.root}/tmp/db/backup.dump"
  53. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement