Skip to content

Instantly share code, notes, and snippets.

@nielsnuebel
Last active September 9, 2019 10:36
Show Gist options
  • Save nielsnuebel/ee450df9144ed23fe671887d6806c5df to your computer and use it in GitHub Desktop.
Save nielsnuebel/ee450df9144ed23fe671887d6806c5df to your computer and use it in GitHub Desktop.
Git Hook: create a MySQL Backup from an Joomla Database before commit.
#!/bin/bash -e
# TOWER APP
PATH="$PATH:/usr/local/bin"
# PATH
SCRIPTPATH="$( cd "$(dirname "$0")" ; pwd -P )"
GITREPO=$SCRIPTPATH/../../
JCONF=$GITREPO/configuration.php
MYSQL=$(which mysql)
DIR=$(pwd)
# DB Backup Files
DUMP=$GITREPO/dumps
NEW=$DUMP/schema.sql
# db credentials
DBHOST=$(grep '$host =' "$JCONF" | cut -d \' -f 2)
DBUSER=$(grep '$user =' "$JCONF" | cut -d \' -f 2)
DBPASS=$(grep '$password =' "$JCONF" | cut -d \' -f 2)
DBNAME=$(grep '$db =' "$JCONF" | cut -d \' -f 2)
cd $GITREPO
$MYSQL -h $DBHOST -u $DBUSER -p$DBPASS $DBNAME < $NEW
echo "Update MySQL Database" >&2
cd $DIR
exit 0
#!/bin/bash -e
# TOWER APP
PATH="$PATH:/usr/local/bin"
# PATH
SCRIPTPATH="$( cd "$(dirname "$0")" ; pwd -P )"
GITREPO=$SCRIPTPATH/../..
JCONF=$GITREPO/configuration.php
DUMP=$GITREPO/dumps
MYSQLDUMP="$(which mysqldump)"
DIR=$(pwd)
# DB Backup Files
NEW=$DUMP/schema.sql
OLD=$NEW.old
# db credentials
DBHOST=$(grep '$host =' "$JCONF" | cut -d \' -f 2)
DBUSER=$(grep '$user =' "$JCONF" | cut -d \' -f 2)
DBPASS=$(grep '$password =' "$JCONF" | cut -d \' -f 2)
DBNAME=$(grep '$db =' "$JCONF" | cut -d \' -f 2)
if [ ! -d DUMP ]; then
mkdir -p ${DUMP}
touch ${OLD}
touch ${NEW}
fi
cd $GITREPO
mv $NEW $OLD
$MYSQLDUMP -h $DBHOST -u $DBUSER -p$DBPASS $DBNAME --skip-dump-date --single-transaction > $NEW
# NOTE : ignore .old
if cmp -s $OLD $NEW; then
echo "Same" >&2
else
echo "Differ" >&2
git add $NEW
git commit $NEW -m "$DBNAME DB update"
echo "schema+data committed" >&2
fi
cd $DIR
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment