Created
July 19, 2017 17:21
-
-
Save paulanunda/f8876a11bfbdc8f92ea8d0f383b9ef76 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
# source: https://www.lewagon.com/blog/how-to-migrate-your-heroku-postgres-database-to-amazon-rds | |
# lib/tasks/db.rake | |
require "uri" | |
namespace :db do | |
desc "Dump AWS production DB and restore it locally." | |
task import_from_aws: [ :environment, :create ] do | |
c = Rails.configuration.database_configuration[Rails.env] | |
Bundler.with_clean_env do | |
puts "[1/4] Fetching DB password from Heroku" | |
db = URI(`heroku config:get DATABASE_URL`) | |
file = "tmp/rds.dump" | |
puts "[2/4] Dumping DB" | |
`PGPASSWORD=#{db.password} pg_dump -h #{db.host} -U #{db.user} -d #{db.path[1..-1]} -F c -b -v -f #{file}` | |
puts "[3/4] Restoring dump on local database" | |
`pg_restore --clean --verbose --no-acl --no-owner -h #{c["host"] || 'localhost'} -d #{c["database"]} #{file}` | |
puts "[4/4] Removing local backup" | |
`rm #{file}` | |
puts "Done." | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment