Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save bperel/8060316 to your computer and use it in GitHub Desktop.
Save bperel/8060316 to your computer and use it in GitHub Desktop.
Support table filtering and custom MySQL path, removes the "Dump completed on..." line
#!/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.
#Uncomment if mysqldump is not in your path
#MYSQLPATH=c:/wamp/bin/mysql/mysql5.5.24/bin
#Comment if mysqldump is in your path
MYSQLPATH=""
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
# The name of the export file
DBEXPORTNAME=db_structure.sql
# The regex filtering the tables whose structure is fetched, leave empty to fetch all the tables
# Example : DBTABLES="^(user_|admin_|accounts)"
DBTABLES=""
[[ -d $DBPATH ]] || mkdir $DBPATH
if [ -t $DBPASS ]; then
$MYSQLPATH/mysqldump -d -u $DBUSER -p$DBPASS $DBNAME $($MYSQLPATH/mysql -u $DBUSER -p$DBPASS $DBNAME -Bse "SHOW TABLES WHERE Tables_in_$DBNAME REGEXP '$DBTABLES'") > $DBPATH/$DBEXPORTNAME.sql
else
$MYSQLPATH/mysqldump -d -u $DBUSER $DBNAME $($MYSQLPATH/mysql -u $DBUSER -p$DBPASS $DBNAME -Bse "SHOW TABLES WHERE Tables_in_$DBNAME REGEXP '$DBTABLES'") > $DBPATH/$DBEXPORTNAME.sql
fi
# Remove the "Dump completed on..." line, which changes at each mysqldump
sed -i 's/^-- Dump completed.*//' $DBPATH/$DBEXPORTNAME.sql
git add $DBPATH/$DBEXPORTNAME.sql
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment