Skip to content

Instantly share code, notes, and snippets.

@dreki
Created August 8, 2012 20:21
Show Gist options
  • Save dreki/3298279 to your computer and use it in GitHub Desktop.
Save dreki/3298279 to your computer and use it in GitHub Desktop.
Easily flash your local Rails database with data from your Heroku environment
# Flash your local Rails database with data from your Heroku environment.
# Much faster than heroku db:pull.
#
# Notes:
# - Requires that you use postgres locally
# - Requires that pg_restore is available
# - Requires initialized Heroku setup for the given project
# - Requires that the pgbackups addon be installed for the given Heroku app
require "open-uri"
# Update the local development database with the data currently in production on Heroku's servers.
task :flash_db do
# Create new backup.
print "Creating a new production backup (may take a while) ... "
%x{bundle exec heroku pgbackups:capture}
puts "done"
# Get new/latest backup name.
backup_name = %x{bundle exec heroku pgbackups}.split("\n").last.match(/(^.*?)\s/)[1]
# Get uri for backup.
uri = %x{bundle exec heroku pgbackups:url #{backup_name}}
# Download backup.
print "Downloading latest production backup (#{backup_name}) ... "
open(backup_name, "wb") do |file|
file << open(uri).read
end
puts "done"
print "Flashing local development database with backup data (may take a while) ... "
db_name = Rails.configuration.database_configuration[Rails.env]["database"]
%x{pg_restore --verbose --clean --no-acl --no-owner -d #{db_name} #{backup_name}}
puts "done"
# Clean up backup we created.
print "Removing backup download file ... "
%x{rm #{backup_name}}
puts "done"
print "Removing backup #{backup_name} from server ... "
%x{bundle exec heroku pgbackups:destroy #{backup_name}}
puts "done"
puts "All done!"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment