#!/bin/bash
# Pre-commit hook to make a mysql dump right before committing and add it to the commit.
# NOTE: if hook not executed try the following command:
# chmod +x .git/hooks/pre-commit
# The absolute path of mysqldump
MYSQLDUMP="<database_command>"
DBHOST="<database_host>"
DBPORT="<database_port>"
# The name of a database user with read access to the database.
DBUSER="<database_user>"
# The password associated with the above user. Leave commented if none.
DBPASS="<database_password>"
# The database associated with this repository.
DBNAME="<database_name>"
# The path relative to the repository root in which to store the sql dump.
DBPATH="dbdumps"

# The dumpfile name
DBDUMPFILENAME="dbdump_latest"

mkdir -p $DBPATH

printf "Dumping database ...\n"
if [ -n "$DBPASS" ]; then
 $MYSQLDUMP --host=$DBHOST --port=$DBPORT -u $DBUSER -p$DBPASS $DBNAME > $DBPATH/$DBDUMPFILENAME.sql
else
 $MYSQLDUMP --host=$DBHOST --port=$DBPORT -u $DBUSER $DBNAME > $DBPATH/$DBDUMPFILENAME.sql
fi
# <gitadd>
exit 0