#!/bin/sh # # A hook script to export your database on every commit. # # The export is saved to a temp file. If the export # fails, the file is trashed. Otherwise it overwrites the tracked file. # # Called by "git commit" with no arguments. The hook should # exit with non-zero status after issuing an appropriate message if # it wants to stop the commit. # # To enable this hook: # $ cd /path/to/repo # git clone https://gist.github.com/rpowis/5198224.git .git/hooks/pre-commit # chmod +x .git/hooks/pre-commit # Users Initials USR="" # Database Details DB_DIR="db" DB_NAME="" DB_HOST="" DB_USER="" DB_PASS="" # Create directory if it doesn't exist if [ ! -d "$DB_DIR" ]; then mkdir -p ${DB_DIR} fi # File names TMP="${DB_DIR}/${DB_NAME}_TMP.sql" if [ -z "$USR" ]; then FILE="${DB_DIR}/${DB_NAME}.sql" else FILE="${DB_DIR}/${DB_NAME}_${USR}.sql" fi # Export Database mysqldump --add-drop-table --skip-extended-insert --skip-comments -h ${DB_HOST} -u ${DB_USER} -p${DB_PASS} ${DB_NAME} > ${TMP} if [[ "$?" -eq 0 ]]; then mv $TMP $FILE echo 'DATABASE EXPORT COMPLETE!' else rm $TMP echo 'DATABASE EXPORT FAILED!' fi