Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A pre-commit hook for git which dumps and adds a mysql database to the repository just before commit.
#!/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=seekrit
# The database associated with this repository.
DBNAME=dplay
# The path relative to the repository root in which to store the sql dump.
DBPATH=schema
[[ -d schema ]] || mkdir schema
if [ -t $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
exit 0
@MaartenW

This comment has been minimized.

Copy link

commented May 27, 2013

In my case, working on a local Mac OS X, I needed to add the path to the executables. Resulting in:
/usr/local/mysql/bin/mysqldump ....
And in:
/usr/local/git/bin/git add ...

I also needed to change the comparison, the -t parameter was not recognized. -n did the trick though.
if [ -n $DBPASS]; then

@teledirigido

This comment has been minimized.

Copy link

commented Jul 31, 2014

  1. $DBPATH is defined as a variables but then schema is explicity defined.
  2. I changed if [ -t $DBPASS ] for if [ -n $DBPASS ] and now works very good.

If you have git and mysqldump on your path variables you don't need to add any absolute url.

Thanks for sharing this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.