Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
@aarongustafson

This comment has been minimized.

Copy link
Owner Author

@aarongustafson aarongustafson commented Aug 20, 2011

Extended inserts increase the change size, so killing them should keep your repository smaller.

@sambauers

This comment has been minimized.

Copy link

@sambauers sambauers commented Jan 17, 2014

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