Created
January 8, 2015 22:54
-
-
Save dliggat/5c15059a4d5218e4cb6b to your computer and use it in GitHub Desktop.
Dump a postgresql production database excluding migrations; and restore locally
This file contains hidden or 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
namespace :production do | |
task sync: :environment do |t, args| | |
user_at_host = '[email protected]' | |
user_keypair = '-i ~/.ec2/gsg-keypair' | |
filename = Time.now.strftime "production_dump_%Y-%m-%d_%H-%M-%S.psql" | |
dump_path = "/tmp/#{filename}" | |
project = 'project' | |
# Dumping the table (excluding schema_migrations) on the server. | |
dump_command = ["pg_dump --exclude-table=schema_migrations", | |
"-a -F c -v -U postgres -h localhost #{project}_production", | |
"-f #{dump_path}"].join(' ') | |
sh "ssh #{user_keypair} #{user_at_host} '#{dump_command}'" | |
# Copying the dump file locally to #{dump_path} | |
sh "scp #{user_keypair} #{user_at_host}:#{dump_path} #{dump_path}" | |
# Dropping, creating, and migrating the database via Rake | |
Rake::Task["db:drop"].invoke | |
Rake::Task["db:create"].invoke | |
Rake::Task["db:migrate"].invoke | |
# Restoring the dump file | |
sh "pg_restore -c -C -F c -v -U #{project} -d #{project}_development #{dump_path}" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment