Skip to content

Instantly share code, notes, and snippets.

@WesleyGoncalves
Last active January 27, 2021 16:07
Show Gist options
  • Save WesleyGoncalves/fc9657bf77bf8162ba84a4d47dfed0a6 to your computer and use it in GitHub Desktop.
Save WesleyGoncalves/fc9657bf77bf8162ba84a4d47dfed0a6 to your computer and use it in GitHub Desktop.
This is a Git pre-commit script to automatically add a MySQL database backup in the commit
#!/bin/sh
echo "#############################################################################"
echo "# Git Pre-Commit hook #"
echo "# This is a pre-commit Git hook to update a WordPress deployment #"
echo "# Created by Wesley Gonçalves <dev@wesleygoncalves.com> #"
echo "# https://gist.github.com/WesleyGoncalves/fc9657bf77bf8162ba84a4d47dfed0a6 #"
echo "# version: 1.0.0 #"
echo "#############################################################################"
ROOT=.
echo "#############################################################################"
echo "### Export the database and include the SQL file in the commit ####"
echo "#############################################################################"
MYSQLDUMP=mysqldump
DB_TEMP_FILENAME=db.sql.temp
DB_FILENAME=db.sql
DB_LOGIN_PATH=<login-path-name> # set with this command: mysql_config_editor set --login-path=<login-path-name> --host=<localhost> --user=<username> --password
DB_USER=<db-user>
DB_NAME=<db-name>
echo "Type the DB Password below:";
# Save backup into temp file
if $MYSQLDUMP --login-path=$DB_LOGIN_PATH -u $DB_USER $DB_NAME > $ROOT/$DB_TEMP_FILENAME
then
echo "> Replacing DB files..."
rm -rf $ROOT/$DB_FILENAME
mv $ROOT/$DB_TEMP_FILENAME $ROOT/$DB_FILENAME
git add $ROOT/$DB_FILENAME
else
echo "> ERROR Error backing up"
echo "> You should run this script from a machine that has access to the website database"
echo "> If you want to prevent the database backup run `git commit --no-verify` or `git commit -n`"
rm -rf $ROOT/$DB_TEMP_FILENAME
exit 1
fi
exit 0
@WesleyGoncalves
Copy link
Author

WesleyGoncalves commented Jan 27, 2021

  1. Download this file to the Git Hooks folder using this command curl -o ./.git/hooks/pre-commit https://gist.githubusercontent.com/WesleyGoncalves/fc9657bf77bf8162ba84a4d47dfed0a6/raw/git-pre-commit
  2. Give execute permissions to the file: chmod +x ./.git/hooks/pre-commit
  3. To avoid typing the database password in every commit or storing the raw password in this file which both can cause critical security issues, this script uses MySQL login path. It is easily set up with this command: mysql_config_editor set --login-path=<login-path-name> --host=<localhost> --user=<username> --password
    1. Alternatively, you can remove --login-path=$DB_LOGIN_PATH from every mysqldump command in this script - you'll be requested the db password in every commit
  4. Edit the DB credentials in the pre-commit file (nano .git/hooks/pre-commit): DB_LOGIN_PATH, DB_NAME, DB_USER.
  5. Done 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment