-
-
Save nuclearsandwich/904891 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=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 |
$DBPATH
is defined as a variables but thenschema
is explicity defined.- I changed
if [ -t $DBPASS ]
forif [ -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!
Seems a little mad to dump the DB on EVERY commit (unless of course all you do all the time is fiddling with the DB, but I doubt that would ever be the case in most scenarios). How could one add this as some sort of parameter to the command ... to make it a little more flexible? Imagine developing a Wordpress site, where the majority of the time you'd commit code changes, and on some you actually change values (settings and the like) in the DB. Now, that could be useful.
And by the way, I can't get it to actually add the file on the last command. It still gives me this:
Untracked files:
/path/to/the/database.sql
... am I missing something?
Could it have to do with async? As a large DB would take some time to actually be written to disk (as file), and then the "git add …" command is long past ... just a thought.
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