Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save mithereal/13d94f5a00f4741b6342 to your computer and use it in GitHub Desktop.
Save mithereal/13d94f5a00f4741b6342 to your computer and use it in GitHub Desktop.
Git pre-commit hook for MySQL database backup ( remote mysqldump + Git push )
#!/bin/bash -e
# -e means exit if any command fails
DBHOST=address.to.your.server
DBUSER=username
DBPASS=password # do this in a more secure fashion
DBNAME=DBNAME
GITREPO=/where/is/your/repo
DUMP=$GITREPO/where/you/store/dumps
NEW=$DUMP/schema.sql
OLD=$NEW.old
DIR=$(pwd)
cd $GITREPO
if [ -f "$NEW" ]
then
mv $NEW $OLD
mysqldump -h $DBHOST -u $DBUSER -p$DBPASS $DBNAME --skip-dump-date --single-transaction --no-data > $NEW
if cmp -s $OLD $NEW; then
echo Databases are the Same
else
echo changes detected in database schema
git add $NEW
echo "schema committed"
fi
else
mysqldump -h $DBHOST -u $DBUSER -p$DBPASS $DBNAME --skip-dump-date --single-transaction --no-data > $NEW
git add $NEW
echo "schema committed"
fi
cd $DIR
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment