Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
executable file 43 lines (34 sloc) 1001 Bytes
#!/bin/bash
set -e
if [ $# -lt 1 ]; then
echo "usage: $(basename $0) <git_remote_name> <heroku_app_name>" >&2
echo "example: heroku-db-sync production murming-water-1234" >&2
exit 1
fi
readonly REMOTE=$1
readonly HEROKU_APP=$2
function development_db_name() {
cat config/database.yml | \
grep development --after-context=3 | \
grep database | \
awk '{ print $2 }'
}
# want the rest of the script to fail if checkout fails
function checkout_remote_repo_sha() {
git ls-remote "$REMOTE" | \
grep HEAD | \
git checkout $(awk '{ print $1 }')
}
function restore_remote_db() {
# need a flag to not backup
heroku pg:backups capture -r "$HEROKU_APP"
curl -o latest.dump $(heroku pg:backups public-url -r $HEROKU_APP)
pg_restore --verbose --clean --no-acl --no-owner -h localhost -d $(development_db_name) latest.dump
}
function main() {
checkout_remote_repo_sha
# ignore pg restore errors with the '|| true'
restore_remote_db || true
rm latest.dump
}
main