Tuesday, June 17, 2008

How to backup MySQL databases

1. create /backup directory
2. create startbackup.sh script file:

rm -f /backup/mysql*
### System Setup ###
BACKUP=/backup
NOW=$(date +"%d-%m-%Y")

### MySQL Setup ###
MUSER="mysqluser"
MPASS="password"
MHOST="localhost"
MYSQL="$(which mysql)"
MYSQLDUMP="$(which mysqldump)"
GZIP="$(which gzip)"


### Start MySQL Backup ###
# Get all databases name
DBS="$($MYSQL -u $MUSER -h $MHOST -p$MPASS -Bse 'show databases')"
for db in $DBS
do
FILE=$BACKUP/mysql-$db.$NOW-$(date +"%T").gz
$MYSQLDUMP -u $MUSER -h $MHOST -p$MPASS $db | $GZIP -9 > $FILE
done