Tech Blog :: Incremental database backups with Git


Nov 16 '10 1:27am
Tags

Incremental database backups with Git

I'm trying a new method for database (MySql) backups: incremental tracking every 10 minutes with Git. Basically I have a Git repository cloned to a directory on the server, every 10 minutes on cron the database is dumped to the directory, and all changes are committed. I added a changed-line counter to make the logs more interesting.

I'm not sure how this will work in production, or what strain mysqldump puts on the database (I should run this off a slave when it goes live, I'm thinking), but the ability to revert the database to any 10-minute increment in the past and view changes to each table line by line seems pretty cool. This is the script:

#! /bin/bash
 
cd /var/backups/db-git
DRUSH=/usr/bin/drush
$DRUSH --root=/path/to/webroot sql-dump > /var/backups/db-git/site-db.sql
syncDT=`date "+%Y-%m-%d_%H-%M"`
 
## count # of lines changed
NUMLINES=$(git diff | grep -e "^[+-]" | wc -l)
## don't count 2 header lines
NUMLINES=$(( $NUMLINES - 2 ))
 
git ci -a -m"DB backup DEV: $syncDT ($NUMLINES lines changed)"
git push origin

Any thoughts?

Post new comment

Don't bother putting in spam links. They'll be set to rel=nofollow and will be removed and reported as spam shortly after submitting.

The content of this field is kept private and will not be shown publicly.
CAPTCHA
Are you human?