Skip to content

Instantly share code, notes, and snippets.

@wilcollins
Last active April 28, 2023 10:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save wilcollins/dfa33ef20caf6dab5826 to your computer and use it in GitHub Desktop.
Save wilcollins/dfa33ef20caf6dab5826 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
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
else
echo Differ
git add $NEW
git commit $NEW -m "$DBNAME DB update"
echo "schema+data committed"
git push # assuming you have a remote to push to
echo "schema+data pushed"
fi
cd $DIR
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment