Skip to content

Instantly share code, notes, and snippets.

@Rich86man
Forked from abstractcoder/import.rake
Last active August 29, 2015 14:19
Show Gist options
  • Save Rich86man/0aa8540e9b1d4fa58337 to your computer and use it in GitHub Desktop.
Save Rich86man/0aa8540e9b1d4fa58337 to your computer and use it in GitHub Desktop.
Import heroku database
namespace :db do
desc "Backs up heroku database and restores it locally"
task clong: :environment do
# Load the current environments database config
c = Rails.configuration.database_configuration[Rails.env]
# This gets around an issue with the Heroku Toolbelt
# https://github.com/sstephenson/rbenv/issues/400#issuecomment-18742700
# https://github.com/sstephenson/rbenv/issues/400#issuecomment-18744931
Bundler.with_clean_env do
# Capture new backup, delete oldest manual backup if limit reached
system("heroku pg:backups capture")
# Download the backup to a file called latest.dump, consider adding this file to .gitignore
system("curl -o latest.dump \`heroku pg:backups public-url\`")
# Restore the backup to the current environment's database
system("pg_restore --verbose --clean --no-acl --no-owner -h localhost -U #{c["username"]} -d #{c["database"]} latest.dump")
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment