Skip to content

Instantly share code, notes, and snippets.

@LL782
Forked from rpowis/pre-commit-db-export
Created January 27, 2014 18:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LL782/8654285 to your computer and use it in GitHub Desktop.
Save LL782/8654285 to your computer and use it in GitHub Desktop.
#!/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