Last active
October 5, 2015 16:48
-
-
Save deanrather/2839639 to your computer and use it in GitHub Desktop.
Automated MySQL Backup & Revision with Git
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/bash -e | |
# This script will sqldump your database into a git repo | |
# If the sqldump is different from the existing one, it will git-push to the remote origin | |
# Replace <user>, <pass>, <db>, <git-repo> below with your database' details | |
# If you want this to automatically run every minute: | |
# | |
# crontab -e | |
# * * * * * /dir/to/script/db-backup-script.sh | |
# | |
DBHOST=localhost | |
DBUSER=<user> | |
DBPASS=<pass> | |
DBNAME=<db> | |
GITREPO=<git-repo> | |
# ------- | |
cd $GITREPO | |
echo "dumping $DBNAME" | |
mysqldump -h $DBHOST -u $DBUSER -p$DBPASS $DBNAME --lock-tables=false --skip-comments > $GITREPO/dbase.sql | |
if | |
[[ $(git status 2> /dev/null | tail -n1) != "nothing to commit (working directory clean)" ]] | |
then | |
echo "git-adding and committing" | |
git add dbase.sql | |
git commit -m "Automated $DBNAME backup" | |
echo "git pushing" | |
git push | |
else | |
echo "there were no changes to commit" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
GITREPO should be the path to a git repo specifically created for this database. That repo should have it's 'origin' set to a remote repo somewhere.