Skip to content

Instantly share code, notes, and snippets.

@dliggat
Created January 8, 2015 22:54
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 dliggat/5c15059a4d5218e4cb6b to your computer and use it in GitHub Desktop.
Save dliggat/5c15059a4d5218e4cb6b to your computer and use it in GitHub Desktop.
Dump a postgresql production database excluding migrations; and restore locally
namespace :production do
task sync: :environment do |t, args|
user_at_host = 'ubuntu@aws-host.com'
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