Last active
April 14, 2021 00:12
-
-
Save mithereal/13d94f5a00f4741b6342 to your computer and use it in GitHub Desktop.
Git pre-commit hook for MySQL database backup ( remote mysqldump + Git push )
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 -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