Skip to content

Instantly share code, notes, and snippets.

@zsim0n
Created March 15, 2014 20:05
Show Gist options
  • Save zsim0n/9573126 to your computer and use it in GitHub Desktop.
Save zsim0n/9573126 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Pre-commit hook to make a mysql dump right before committing and add it to the commit.
#
## Change the following values to suit your local setup.
# The name of a database user with read access to the database.
DBUSER=root
# The password associated with the above user. Leave commented if none.
#DBPASS=password
# The database associated with this repository.
DBNAME=site1
# The path relative to the repository root in which to store the sql dump.
DBPATH=.db
# If something fails, exit with status other than 0
set -e
has_clean_work_tree () {
git rev-parse --quiet --verify HEAD &> /dev/null || return 0
git update-index -q --ignore-submodules --refresh
if ! git diff-files --quiet --ignore-submodules
then
return 0
fi
if ! git diff-index --cached --quiet --ignore-submodules HEAD --
then
return 0
fi
return 1
}
has_clean_work_tree
if [ "$?" == 0 ]; then
[[ -d $DBPATH ]] || mkdir $DBPATH
if [ -n $DBPASS ]; then
mysqldump -u $DBUSER -p$DBPASS $DBNAME > $DBPATH/$DBNAME.sql
else
mysqldump -u $DBUSER $DBNAME > $DBPATH/$DBNAME.sql
fi
git add $DBPATH/$DBNAME.sql
fi
exit 0
#!/bin/bash
source .git/hooks/pre-commit-mysql-dump
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment