Skip to content

Instantly share code, notes, and snippets.

@rpowis
Last active December 15, 2015 03:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save rpowis/5198224 to your computer and use it in GitHub Desktop.
Save rpowis/5198224 to your computer and use it in GitHub Desktop.
Export DB to temp file on git commit. If the export fails delete the temp file, otherwise overwrite the tracked file.
#!/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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment