Last active
August 29, 2015 14:23
-
-
Save rajiteh/a9022510e8e8c24c3d93 to your computer and use it in GitHub Desktop.
Sync two Heroku Postgres databases via 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 | |
set -e | |
######## Heroku Postgres Sync Script ####### | |
## Author: Raj <[email protected]> | |
## License: MIT | |
## | |
## Syntax: sync.sh FROM_APP TO_APP [USE OLD BACKUP] | |
## | |
## FROM_APP Source application to obtain the backup. | |
## TO_APP Destination application to restore. | |
## USE_OLD_BACKUP Presence of this argument disables capturing | |
## a new backup from the FROM_APP forcing the | |
## script to use last available backup. | |
FROM_DB=$1 | |
TO_DB=$2 | |
OLD_BACKUP=$3 | |
if [ -z "$FROM_DB" -o -z "$TO_DB" ] | |
then | |
echo "<script.sh> SOURCE_APP DESTINATION_APP [USE_OLD_BACKUP]" | |
exit | |
fi | |
echo "NOTICE: Source : $FROM_DB" | |
echo "NOTICE: Destination : $TO_DB" | |
read -p "Press [Enter] key to start or CTRL+C to bail..." | |
if [ -z "$OLD_BACKUP" ] | |
then | |
echo "--- Capturing Backup ---" | |
heroku pg:backups capture --app $FROM_DB | |
fi | |
echo -n "--- Retrieving Backup ID ---" | |
BACKUP_ID=$(heroku pg:backups --app $FROM_DB | sed -n '/=== Backups/,/=== Restores/p' | grep -E "Finished" | head -n1 | cut -d" " -f1) | |
echo " OK ${BACKUP_ID}" | |
echo -n "--- Retrieving Backup URL ---" | |
DB_URL=$(heroku pg:backups public-url $BACKUP_ID --app $FROM_DB | grep "http") | |
echo " OK" | |
echo "--- Reset destination ---" | |
heroku pg:reset DATABASE_URL --confirm $TO_DB | |
echo "--- Restoring to destination database ---" | |
heroku pg:backups restore "${DB_URL}" DATABASE_URL --confirm $TO_DB --app $TO_DB |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment