自动备份MYSQL的linux 脚本

这是我写的一个自动备份MYSQL的 脚本, 能备份所有MYSQL 中的所有数据库。

 

#!/bin/bash

umask 177

MUSER="userid"
MPASS="password"
MHOST="localhost"
MYSQL="$(which mysql)"
MYSQLDUMP="$(which mysqldump)"
PARAMS="--hex-blob --skip-extended-insert --password=$MPASS"
DATE="$(date +%F)"
BACKUPDIR="/home/${MUSER}/BACKUP/$DATE/"
RMDATE="$(date -d "1 week ago" +%F)"
RMDIR="/home/${MUSER}/BACKUP/$RMDATE"
BACKUPLOG="/home/${MUSER}/BACKUP/backup.log"

if [ ! -f $BACKUPLOG ] ; then
touch $BACKUPLOG
chmod 600 $BACKUPLOG
fi

if [ -d $RMDIR ]; then
rm -rf $RMDIR
echo "Backup DELETED from" $RMDATE >> $BACKUPLOG
fi

if [ ! -d $BACKUPDIR ]; then
mkdir -m 700 -p $BACKUPDIR
fi

echo "Backup BEGINNING" $DATE >> $BACKUPLOG

DBS="$($MYSQL -h $MHOST -u $MUSER -p$MPASS -Bse 'show databases')"
for db in $DBS
do
$MYSQLDUMP $PARAMS $db > $BACKUPDIR$db.sql && bzip2 --best $BACKUPDIR$db.sql
done

echo "Backup COMPLETED" $DATE >> $BACKUPLOG

 

另外可以结合CRONJOB定时备份

你可能感兴趣的:(linux)