Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace :accio do
- desc "Drop/restore database from Heroku backup"
- task :db do
- Rake::Task['accio:download_backup'].invoke
- Rake::Task['accio:restore_backup'].invoke
- end
- desc "Download database dump from Heroku"
- task :download_backup => [:logger_no_env, '~/.netrc'] do |t, args|
- return Rails.logger.error('curl not installed') unless \
- command?('curl')
- Rails.logger.info "Downloading Heroku database backup"
- Bundler.with_clean_env do
- url = `heroku pg:backups public-url --app itsallgravy`.chomp
- `curl -o #{filename} '#{url}'`
- end
- end
- desc "drops db and restores from backup"
- task :restore_backup => [:logger] do |t, args|
- return Rails.logger.error('pg_restore not installed') unless \
- command?('pg_restore')
- return Rails.logger.error('no db backup found') unless \
- File.exists?(filename)
- ActiveRecord::Base.connection
- db_config = ActiveRecord::Base.connection_config
- database = db_config[:database]
- username = db_config[:username]
- host = db_config[:host]
- Rails.logger.info "Drop/creating database"
- Rake::Task['db:drop'].invoke
- Rake::Task['db:create'].invoke
- Rails.logger.info "Restoring from database backup"
- cmd = []
- cmd << 'pg_restore'
- cmd << "-d #{database}"
- cmd << "-U #{username}"
- cmd << "-h #{host}"
- cmd << "#{filename}"
- system cmd.join(' ')
- end
- end
- def command?(command)
- system "which #{command} > /dev/null 2>&1"
- end
- def filename
- "#{Rails.root}/tmp/db/backup.dump"
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement