Skip to content

Instantly share code, notes, and snippets.

@deanrather
Last active October 5, 2015 16:48
Show Gist options
  • Save deanrather/2839639 to your computer and use it in GitHub Desktop.
Save deanrather/2839639 to your computer and use it in GitHub Desktop.
Automated MySQL Backup & Revision with Git
#!/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
@deanrather
Copy link
Author

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.

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