Skip to content

Instantly share code, notes, and snippets.

@Amitesh
Forked from aarongustafson/backup-to-git.sh
Created December 9, 2011 12:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save Amitesh/1451422 to your computer and use it in GitHub Desktop.
Save Amitesh/1451422 to your computer and use it in GitHub Desktop.
DB Backup to Git
#!/bin/bash
##
# MySQL DB dump to Git commit
#
# Dumps the specified mysql database to the given location and commits it and
# the previous database to the Git repository.
#
# It is assumed you have already setup the Git respository to only be the
# a checkout of the database backup location
#
# To do that (in the repository):
# $ git config core.sparsecheckout true
# $ echo sql-backup/ > .git/info/sparse-checkout
# $ git read-tree -m -u HEAD
#
# Author: Aaron Gustafson, Easy-Designs LLC
# Copyright: Copyright (c) 2011 Easy-Designs LLC
# Since: Version 0.1
##
# path to Git repository
REPO_PATH="/path/to/your/repo/sql-backup"
REPO_BRANCH="master"
# database settings
DB_NAME="You DB Name Here"
DB_USER="Your DB Username Here"
DB_PASS="Your DB Password Here"
FILENAME=${DB_NAME}.sql
# svn up the content
cd $REPO_PATH
git pull --quiet
# dump the database using the mysql administrator - so we can see all dbs
mysqldump -u$DB_USER -p$DB_PASS --opt --routines --skip-extended-insert --compact --force "${DB_NAME}" > "${FILENAME}"
# add everything we have - will throw a warning the dbname.sql already is added but its fine
git add .
# commit
git commit --quiet -m "SQL Database Dump"
# push
git push --quiet origin $REPO_BRANCH
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment