Skip to content

Instantly share code, notes, and snippets.

@jdx
Last active December 17, 2015 09:29
Show Gist options
  • Save jdx/5587392 to your computer and use it in GitHub Desktop.
Save jdx/5587392 to your computer and use it in GitHub Desktop.
heroku db sync script
#!/usr/bin/env ruby
require 'optparse'
def sync(delete: false)
if !delete and File.exists? 'db.dump'
puts 'Existing database found in db.dump'
else
puts 'Downloading database...'
run "heroku pgbackups:capture -a pb-production --expire"
run "curl -o db.dump `heroku pgbackups:url -a pb-production`"
end
puts 'Dropping tables in local db...'
run 'psql production_beast_development -c "drop schema public cascade; create schema public;"'
puts 'Copying into local db...'
run "pg_restore --no-acl --no-owner -h localhost -U postgres -d production_beast_development db.dump"
end
def restore(app_name: 'pb-staging', delete: false)
run "heroku pgbackups:capture -a pb-production --expire" if delete
run "heroku pgbackups:restore DATABASE -a #{app_name} `heroku pgbackups:url -a pb-production`"
end
def run(cmd)
puts cmd
system cmd
end
options = {}
OptionParser.new do |opts|
opts.banner = "Usage: script/db (sync|restore)"
opts.on '-d', '--delete', 'Deletes existing backup' do |delete|
options[:delete] = delete
end
end.parse!
unless ARGV.any?
puts "Usage: script/db (sync|restore)"
exit 1
end
case ARGV[0]
when 'sync'
sync(delete: options[:delete])
when 'restore'
restore(delete: options[:delete])
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment