Last active
January 27, 2021 16:07
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 <[email protected]> #" | |
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
curl -o ./.git/hooks/pre-commit https://gist.githubusercontent.com/WesleyGoncalves/fc9657bf77bf8162ba84a4d47dfed0a6/raw/git-pre-commit
chmod +x ./.git/hooks/pre-commit
mysql_config_editor set --login-path=<login-path-name> --host=<localhost> --user=<username> --password
--login-path=$DB_LOGIN_PATH
from everymysqldump
command in this script - you'll be requested the db password in every commitnano .git/hooks/pre-commit
):DB_LOGIN_PATH
,DB_NAME
,DB_USER
.