Skip to content

Instantly share code, notes, and snippets.

@davidmfoley
Created May 2, 2012 16:09
Show Gist options
  • Save davidmfoley/2577832 to your computer and use it in GitHub Desktop.
Save davidmfoley/2577832 to your computer and use it in GitHub Desktop.
Heroku/Rails: restore local development db from heroku
#! /bin/bash
# First you need to have the "pgbackups" addon in your heroku app, for example:
# $ heroku addons:add pgbackups:auto-week
# your database name here
DB_NAME='foobar_development'
# grab the data directory for postgres from the running process
DATA_DIR=`ps ax |grep [/]post | sed "s/^.*\-D //" | sed "s/ .*$//"`
# note: this log location may be different for you...
# not sure how to get this from the command line
LOG_FILE='/usr/local/var/postgres/server.log'
cd "`dirname "$0"`"
cd ..
echo 'capturing backup on heroku'
heroku pgbackups:capture --expire
echo 'downloading backup'
curl -o tmp/latest.dump `heroku pgbackups:url`
echo 'restarting PG locally'
echo $DATA_DIR
pg_ctl -D $DATA_DIR -l $LOG_FILE restart -w
# this assumes that your local unix user has a matching posrtgres superuser
# to add a postgres user with the same name as your login:
# $ createuser `whoami`
# then follow the prompts
echo 'dropping, recreating database'
dropdb $DB_NAME
createdb $DB_NAME
echo 'restoring data from backup'
pg_restore -O -U postgres -d $DB_NAME -h localhost tmp/latest.dump
echo 'migrating'
rake db:migrate db:test:prepare
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment