Skip to content

Instantly share code, notes, and snippets.

@jcsky
Created May 26, 2016 02:43
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 jcsky/6fdc538642fc41bdfa95b4759eeec0e5 to your computer and use it in GitHub Desktop.
Save jcsky/6fdc538642fc41bdfa95b4759eeec0e5 to your computer and use it in GitHub Desktop.
dump staging/production db and restore to local db, just use rake:db_pull
namespace :db do
desc 'Pull staging db to development'
task :pull => [:dump, :restore]
task :dump do
dumpfile = "#{Rails.root}/tmp/latest.dump"
puts 'PG_DUMP on staging database...'
staging = Rails.application.config.database_configuration['staging']
system "ssh user@server 'PGPASSWORD=\"#{staging['password']}\" pg_dump -U postgres #{staging['database']} -h #{staging['host']} -F t' > #{dumpfile}"
puts 'Done!'
end
task :restore do
dev = Rails.application.config.database_configuration['development']
dumpfile = "#{Rails.root}/tmp/latest.dump"
puts 'PG_RESTORE on development database...'
system "pg_restore --verbose --clean --no-acl --no-owner -h 127.0.0.1 -U #{dev['username']} -d #{dev['database']} #{dumpfile}"
puts 'Done!'
end
end
# source https://martinschurig.com/posts/2015/02/pulling-production-database-to-local-machine-rails-task/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment