Skip to content

Instantly share code, notes, and snippets.

@paulanunda
Created July 19, 2017 17:21
Show Gist options
  • Save paulanunda/f8876a11bfbdc8f92ea8d0f383b9ef76 to your computer and use it in GitHub Desktop.
Save paulanunda/f8876a11bfbdc8f92ea8d0f383b9ef76 to your computer and use it in GitHub Desktop.
# 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