Created
January 30, 2014 11:53
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
These heroku commands are deprecated. Instead use
heroku pg:backups capture
&heroku pg:backups public-url