Created
April 14, 2012 18:18
-
-
Save yaniv-aknin/2386666 to your computer and use it in GitHub Desktop.
heroku-dupdb: small utility to clone a database between two apps, useful for copying production into staging
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
#!/usr/bin/env bash | |
cprint () { | |
printf "%b" "\e[1;37m${1}\e[0m\n" | |
} | |
usage() { | |
cat << EOF | |
usage: $0 [OPTIONS] <src> <dst> | |
--confirm <dst> dont ask for confirmation before destroying and restoring into <dst> | |
EOF | |
exit 2 | |
} | |
set -e | |
unset src dst confirm | |
while [ -n "$1" ]; do | |
if [ "$1" == "--confirm" ]; then | |
confirm="--confirm $2" | |
shift | |
elif echo $1 | egrep -q '^-'; then | |
echo "unknown option $1" | |
usage | |
elif [ -z "$src" ]; then | |
src="$1" | |
elif [ -z "$dst" ]; then | |
dst="$1" | |
else | |
echo "superflous argument $1" | |
usage | |
fi | |
shift | |
done | |
if [ -z "$src" -o -z "$dst" ]; then | |
echo "insufficient arguments" | |
usage | |
fi | |
if [ -n "$confirm" ] && echo "$confirm" | cut -f2 -d' ' | grep -vwq "$dst"; then | |
echo "--confirm name doesn't match destination name" | |
exit 1 | |
fi | |
cprint "retrieving latest backup for \e[1;32m${src}" | |
backup_name=$(heroku pgbackups -a "$src" | tail -1 | cut -f1 -d' ') | |
[ -z "$backup_name" ] && exit 1 | |
cprint "retrieving url for \e[1;32m${src}:${backup_name}" | |
backup_url="$(heroku pgbackups:url "$backup_name" -a "$src")" | |
[ -z "$backup_url" ] && exit 1 | |
cprint "destroying \e[1;31m$dst" | |
heroku pg:reset DATABASE -a "$dst" $confirm | |
cprint "restoring into \e[1;31m$dst" | |
heroku pgbackups:restore DATABASE "$backup_url" -a "$dst" $confirm |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment