Skip to content

Instantly share code, notes, and snippets.

@rynbyjn
Created July 16, 2013 17:57
Show Gist options
  • Save rynbyjn/6011039 to your computer and use it in GitHub Desktop.
Save rynbyjn/6011039 to your computer and use it in GitHub Desktop.
Use heroku pgbackups to restore local database with staging/production data.
APP_NAME = 'INSERT_DEVIL_WORSHIPPING_APP_NAME_HERE'
namespace :heroku do
namespace :db do
namespace :pull do
desc "Pulls staging db to local."
task :staging => :environment do
pull_db_locally("#{APP_NAME}-staging", "#{APP_NAME}_development")
end
desc "Pulls production db to local."
task :production => :environment do
pull_db_locally("#{APP_NAME}-production", "#{APP_NAME}_development")
end
end
end
end
def pull_db_locally(app_name, db_name)
# capture db
puts "Capturing backup of '#{app_name}' from heroku."
`heroku pgbackups:capture -e -a "#{app_name}"`
# grab the url
url = `heroku pgbackups:url -a "#{app_name}"`
# curl the db file locally
puts "Copy db '#{url}' to 'db.dump'."
`curl -o db.dump "#{url}"`
# restore localhost from dump file
puts "Restoring local db '#{db_name}' from 'db.dump'."
`pg_restore -v -c -O -h localhost -d "#{db_name}" db.dump`
# remove dump file
puts "Removing temporary 'db.dump' file."
`rm db.dump`
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment