Skip to content

Instantly share code, notes, and snippets.

@knoxjeffrey
Created October 23, 2018 23:18
Show Gist options
  • Save knoxjeffrey/3ec03e33cde35035849cf10188587dc5 to your computer and use it in GitHub Desktop.
Save knoxjeffrey/3ec03e33cde35035849cf10188587dc5 to your computer and use it in GitHub Desktop.
Pull specified db to development, move to production
# rake db:staging_to_production
# eg. rake db:staging_to_production
namespace :db do
desc 'Pull specified db to development, move to production'
task :staging_to_production do
dumpfile = "#{Rails.root}/tmp/latest.dump"
ssh = Rails.application.credentials[:staging][:ssh]
ssh_port = Rails.application.credentials[:staging][:ssh_port]
db_staging_user = Rails.application.credentials[:staging][:db_user]
db_staging_password = Rails.application.credentials[:staging][:db_password]
db_staging_name = Rails.application.credentials[:staging][:db_name]
db_staging_host = Rails.application.credentials[:staging][:db_host]
db_production_user = Rails.application.credentials[:production][:db_user]
db_production_password = Rails.application.credentials[:production][:db_password]
db_production_name = Rails.application.credentials[:production][:db_name]
db_production_host = Rails.application.credentials[:production][:db_host]
pg_command = Rails.application.credentials[:pg_command]
puts 'PG_DUMP on staging database...'
system "ssh #{ssh} -p #{ssh_port} 'PGPASSWORD=\"#{db_staging_password}\" #{pg_command}/pg_dump -U #{db_staging_user} #{db_staging_name} -h #{db_staging_host} -F t' > #{dumpfile}"
puts 'Pulled!'
puts 'SCP database from local to server'
system "scp -P #{ssh_port} #{dumpfile} #{ssh}:"
puts 'Transferred!'
puts 'PG_RESTORE on production database...'
system "ssh #{ssh} -p #{ssh_port} 'PGPASSWORD=\"#{db_production_password}\" #{pg_command}/pg_restore --verbose --clean --no-acl --no-owner -h #{db_production_host} -U #{db_production_user} -d #{db_production_name} latest.dump'"
puts 'Done!'
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment