Created
August 13, 2010 21:28
-
-
Save ephesus/523570 to your computer and use it in GitHub Desktop.
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/sh | |
# | |
# Dump databases when switching branches | |
# James Rubingh | |
# ephesus@gmail.com | |
# To enable: export PCD_ENABLE=1 | |
# To disable: unset PCD_ENABLE | |
# environment variables | |
# PCD_ENABLE - set to 1 to dump databases when switching branches, default is off | |
# PCDB - post checkout database cli command | |
# PCDBDUMP - post checkout database dump command | |
if [ ${PCD_ENABLE-0} != "1" ];then | |
exit 0 | |
fi | |
DBNAME=local_dev | |
DBHOST=localhost | |
DBUSER=james | |
DBPASS=james | |
if [[ ${3-0} == "1" -a $1 != $2 ]]; then | |
DATE=`date +%y%m%d%H%M` | |
BRANCH=`git symbolic-ref HEAD 2> /dev/null` | |
NEWBRANCH=${BRANCH#refs/heads/} | |
BRANCH=$(git show-ref --heads | grep $1 | awk '{print $2}') | |
OLDBRANCH=${BRANCH#refs/heads/} | |
echo "Saving ${OLDBRANCH}'s database" | |
${PCDBDUMP:-mysqldump} -h $DBHOST -u $DBUSER --password=$DBPASS $DBNAME > "$OLDBRANCH.sql" || exit 1 | |
rm -f $OLDBRANCH.sql.bz2 | |
bzip2 $OLDBRANCH.sql | |
if [ -f $NEWBRANCH.sql.bz2 ]; then | |
echo "Restoring ${NEWBRANCH}'s database" | |
echo "DROP DATABASE $DBNAME; CREATE DATABASE $DBNAME" | ${PCDB-mysql} -h $DBHOST -u $DBUSER --password=$DBPASS $DBNAME | |
bzcat $NEWBRANCH.sql.bz2 | ${PCDB:-mysql} -h $DBHOST -u $DBUSER --password=$DBPASS $DBNAME | |
fi | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment