Wednesday, January 21, 2009

MySQL backup script



#!/bin/bash
# A simple shell script to backup all MySQL Server Database
# Each dump will be line as follows:
# Directory: /var/lib/mysql/backup/mm-dd-yyyy
# File: mysql-DBNAME.04-25-2008.gz
# -------------------------------------------------------------------------
NOW=$(date +"%m-%d-%Y") # mm-dd-yyyy format
FILE="db-full-$NOW.tar.gz"
BAK="/var/lib/mysql/backups/"

### Server Setup ###
#* MySQL login user name *#
MUSER="user"

#* MySQL login PASSWORD name *#
MPASS="pass"

#* MySQL binaries *#
MYSQL="$(which mysql)"
MYSQLDUMP="$(which mysqldump)"
GZIP="$(which gzip)"

# get all application related databases
DBS="$($MYSQL -u $MUSER -p$MPASS -Bse 'show databases' | grep -v information_schema)"

# create backups securely
umask 006

# start to dump database one by one
for db in $DBS
do
FILE=$BAK/mysql-$db.$NOW.gz
# gzip compression for each backup file
$MYSQLDUMP -u $MUSER -p$MPASS $db | $GZIP -9 > $FILE
done

No comments: