MYSQL自动备份任务设计for Linux

本方案在LINUX下配置MYSQL自动备份任务,并生成压缩文件传输至远程服务器,最后删除历史备份文件。

在LINUX下部署shell程序:

#!/bin/bash

USER=root

PASSWORD="root"

DATABASE="oa"

BACKUP_DIR=/home/mysqlbak/

LOGFILE=/home/mysqlbak/oa_backup.log

DATE=`date '+%Y%m%d-%H%M'`

DUMPFILE=$DATE.sql

ARCHIVE=$DATE.sql.tgz

OPTIONS="-u$USER -p$PASSWORD $DATABASE"

if [ ! -d $BACKUP_DIR ] ;

then

mkdir -p "$BACKUP_DIR"

fi

echo " " >> $LOGFILE

echo " " >> $LOGFILE

echo "———————————————–" >> $LOGFILE

echo "BACKUP DATE:" $(date +"%y-%m-%d %H:%M:%S") >> $LOGFILE

echo "———————————————– " >> $LOGFILE

cd $BACKUP_DIR

mysqldump $OPTIONS > $DUMPFILE

if [[ $? == 0 ]]; then

tar czvf $ARCHIVE $DUMPFILE >> $LOGFILE 2>&1

echo “[$ARCHIVE] Backup Successful!” >> $LOGFILE

rm -f $DUMPFILE

else

echo “Database Backup Fail!” >> $LOGFILE

fi

echo “Backup Process Done

##以下是将备份文件传输至异地服务器

rsync -avz --port=8888 $ARCHIVE rsync@220.128.180.123::backup --password-file=/etc/.rsyncd.secrets

# delete old backup(删除7天前历史备份文件)

find /home/mysqlbak/ -mtime +7 -name "*.sql.tgz" -exec rm -rf {} \;