Friday, August 2, 2013

MySQL backup with Version control SCript

You do need to initialize a Git repository at /path/to/dbbackups first. Then add a cron tab that calls the backup script.


#!/bin/bash  
#SETTINGS
MYSQLUSER=<insert mysql user>
MYSQLPASSWORD=<insert mysql user password>
DBNAMES=("database_1" "database_2" "database_3")
BACKUPDIR=/path/to/dbbackups

#DUMP THE DATABASES
for name in ${DBNAMES[@]}
do
/usr/bin/mysqldump --skip-opt -u$MYSQLUSER -p$MYSQLPASSWORD --quote-names --complete-insert --create-options --quick --lock-tables=false --skip-add-locks $name -r $BACKUPDIR/$name.sql

cd $BACKUPDIR
/usr/local/bin/git add $name.sql
/usr/local/bin/git commit -m "mysql database backup $name `date`"
echo "--- mysql database backup $name `date` completed"

done
/usr/local/bin/git push
echo "--- mysql database backups pushed to remote repository"

No comments:

Post a Comment