Last active
August 4, 2016 20:23
-
-
Save julienma/96ec4443ba65407e66d5c74438b64122 to your computer and use it in GitHub Desktop.
Download latest Postgres backup for an Heroku app. Based on latest Heroku's `pg:backups`.
This file contains hidden or 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 | |
# | |
# Download latest PG backup for an Heroku app. | |
# See https://devcenter.heroku.com/articles/heroku-postgres-backups#downloading-your-backups | |
# | |
# Usage: | |
# Don't forget to chmod +x the script, then: | |
# | |
# $ ./download_latest_pgbackup_from_heroku.sh {HEROKU_APPNAME} | |
# | |
# Params: | |
# | |
# HEROKU_APPNAME: name of the application on Heroku, to get the PostgreSQL DB backup from. | |
# | |
echo "Run: $(date +%Y%m%d)-$(date +%H%M)" | |
HEROKU_APPNAME=$1 | |
# Exit script if no param is given | |
if [[ -z "$1" ]] | |
then | |
echo "Please pass the Heroku App name as a param to this script." | |
echo "Ex: $ ./download_latest_pgbackup_from_heroku.sh my_app" | |
exit 1 | |
fi | |
HEROKU_PATH=/usr/local/heroku/bin | |
BACKUP_FOLDER=~/_heroku_pg_backups | |
BACKUP_FILENAME=${HEROKU_APPNAME}_db_$(date +%Y%m%d)-$(date +%H%M).dump | |
# get the public-url (S3) for latest backup | |
BACKUP_URL=`$HEROKU_PATH/heroku pg:backups public-url --app $HEROKU_APPNAME | cat` | |
# if we can find a valid backup URL for this app | |
if [[ $BACKUP_URL == http* ]] | |
then | |
echo "Downloading latest backup for $HEROKU_APPNAME, from $BACKUP_URL" | |
# download it locally | |
curl --output $BACKUP_FOLDER/$BACKUP_FILENAME --location $BACKUP_URL | |
# and gzip it | |
gzip --best $BACKUP_FOLDER/$BACKUP_FILENAME | |
echo "Backup done." | |
else | |
echo "Error while getting backup URL. Make sure $HEROKU_APPNAME exists, and has at least 1 existing backup." | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment