Created
July 11, 2016 19:55
-
-
Save mzaragoza/fbd3978b72d66955bf926b9c229d96e0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#lib/tasks/db.rake | |
namespace :db do | |
desc "Import most recent database dump" | |
task :import_from_prod => :environment do | |
puts 'heroku run pg:backups capture --app sushi-prod' | |
restore_backup 'sushi-prod' | |
end | |
def path_to_heroku | |
['/usr/local/heroku/bin/heroku', '/usr/local/bin/heroku'].detect {|path| File.exists?(path)} | |
end | |
def heroku(command, site) | |
`GEM_HOME='' BUNDLE_GEMFILE='' GEM_PATH='' RUBYOPT='' #{path_to_heroku} #{command} -a #{site}` | |
end | |
def restore_backup(site = 'sushi-prod') | |
dump_file = "#{Rails.root}/tmp/postgres.dump" | |
unless File.exists?(dump_file) | |
pgbackups_url = heroku('pg:backups public-url -q', site).chomp | |
puts "curl -o #{dump_file} #{pgbackups_url}" | |
system "curl -o #{dump_file} '#{pgbackups_url}'" | |
end | |
database_config = YAML.load(File.open("#{Rails.root}/config/database.yml")).with_indifferent_access | |
dev_db = database_config[Rails.env] | |
system "pg_restore -d #{dev_db[:database]} -c #{dump_file}".gsub(/\s+/,' ') | |
puts | |
puts "'rm #{dump_file}' to redownload postgres dump." | |
puts "Done!" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment