Skip to content

Instantly share code, notes, and snippets.

@ryane
Created February 21, 2012 17:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryane/1877545 to your computer and use it in GitHub Desktop.
Save ryane/1877545 to your computer and use it in GitHub Desktop.
Rake task to restore the latest backup from heroku into your local development postgres database
# quick and dirty postgres database restoration from heroku
# This works in my local dev environment
# It might not work in yours without some changes
# Now just uses Heroku::Auth to get an authenticated client. No
# need to read a credentials file. You probably need a recent
# version of the heroku gem for this to work
desc "Restore from heroku"
task :restore do
require 'pgbackups/client'
# get heroku app name
app_name = ""
`git remote -v`.split("\n").each do |remote|
name, url, method = remote.split(/\s/)
if url =~ /^git@heroku\.com:([\w\d-]+)\.git$/
app_name = $1
end
end
# get PGBACKUPS_URL
heroku = Heroku::Auth.client
pgb_url = heroku.config_vars(app_name)["PGBACKUPS_URL"]
# get latest backup public url
pgb_client = PGBackups::Client.new(pgb_url)
backup_url = pgb_client.get_latest_backup["public_url"]
# download
# TODO: don't depend on curl
out_file = "/tmp/#{app_name}.dump"
system "curl '#{backup_url}' -o #{out_file}"
# TODO: support for other databases other than postgres
# TODO: use auth info from database.yml
# restore database
db_name = Rails.configuration.database_configuration["development"]["database"]
restore_cmd = "pg_restore --verbose --clean --no-acl --no-owner -d #{db_name} #{out_file}"
system restore_cmd
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment