Skip to content

Instantly share code, notes, and snippets.

@itayadler
Created January 30, 2014 11:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save itayadler/8706993 to your computer and use it in GitHub Desktop.
Save itayadler/8706993 to your computer and use it in GitHub Desktop.
A bash script to import and restore data from your heroku postgres database
#!/bin/bash
#import_db.sh: a bash script that imports from your heroku app the latest postgres db schema dump and imports it to
#the specified db schema name
RED="\x1b[31m"
GREEN="\x1b[32m"
COLOR_RESET="\x1b[0m"
DUMP_FILENAME="latest.dump"
usage() {
echo "Usage: $0 postgres_db_name"
exit 1
}
echo_with_color() {
echo -e "$2$1$COLOR_RESET"
}
# call usage() function if no db name is supplied
[[ $# -eq 0 ]] && usage
HAS_HEROKU_REMOTE=$(heroku apps:info 2>&1 > /dev/null)
if [ ! -z "$HAS_HEROKU_REMOTE" ]; then
echo_with_color "You need to add the app's heroku remote to git in order for heroku to be setup" $RED
exit 0
else
echo_with_color "Beginning import..." $GREEN
echo_with_color "Fetching from heroku the latest dump and saving it into $DUMP_FILENAME" $GREEN
heroku pgbackups:capture
curl -o $DUMP_FILENAME `heroku pgbackups:url`
echo_with_color "Restoring the dump to $1" $GREEN
pg_restore --verbose --clean --no-acl --no-owner -h localhost -d $1 $DUMP_FILENAME
echo_with_color "Deleting the dump file.." $GREEN
rm $DUMP_FILENAME
echo_with_color "Import completed successfully" $GREEN
fi
@mnort9
Copy link

mnort9 commented Jun 27, 2016

These heroku commands are deprecated. Instead use heroku pg:backups capture & heroku pg:backups public-url

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment