1. 程式人生 > >Web叢集之全網備份指令碼

Web叢集之全網備份指令碼

需求:實現指定目錄或檔案定時打包到本地的/backup目錄以本機IP命名的目錄下,並且對打包的檔案做md5雜湊,然後將雜湊的結果寫入到當前目錄下md5.log檔案中,然後將打包的檔案推送到backup服務端;還得將7天前的備份檔案給刪除,每一步的操作都寫入到日誌中

備份的目錄/檔案:var/spool/cron/root server/scripts/ etc/hosts etc/sysconfig/iptables var/log/
打包存放目錄:/backup/10.0.0.31
日誌檔案目錄:/tmp/backup.log
rsync認證賬戶:rsync_backup
rsync認證模組:backup
rsync認證密碼:123456

指令碼可以在優化幾個點:
1)日誌內容太多怎麼辦?(可以統計日誌內容的行數,超過一定值,進行清空)
2)對打包的檔案是否打包成功做判斷

指令碼如下:

#!/bin/sh
#created by jason 15:01 2018-8-24
#QQ 769866297 Mail:[email protected]
#Regularly back up files or directory scripts
Ip=$(ifconfig eth0 | awk -F "[ :]+" 'NR==2{print $4}')
Dir=/backup/$Ip
Time=$(date +%F)
Log=/tmp/backup.log
Rsync_auth=rsync_backup
Rsync_module=backup
Rsync_Ip=172.16.1.41
Rsync_passwdfile=/etc/rsync.password
[ ! -f $Log ] && touch /tmp/backup.Log

[ ! -d $Dir ] && mkdir $Dir -p

if [ -d $Dir ];then
    cd / &&\
    echo "<--------------------------${Time}----------------------------->" >> ${Log}
    tar zcf ${Dir}/backup_tar_${Time}.gz var/spool/cron/root server/scripts/ etc/hosts etc/sysconfig/iptables var/log/
    echo "backup file or directory is successfully ${Time} "  >> ${Log}
        md5sum  ${Dir}/backup_tar_${Time}.gz >>${Dir}/md5.log
        echo "backup file or directory md5 hashing complete ${Time} "  >> ${Log}
fi
[ $? -eq 0 ] && rsync -az ${Dir}/ ${Rsync_auth}@${Rsync_Ip}::${Rsync_module}/ --password-file=${Rsync_passwdfile}
    echo "backup file or directory rsync successful ${Time}" >> ${Log}
[ $? -eq 0 ] && find ${Dir} -name "*.gz" -atime +7 -exec   rm {} \;
    echo "find the directory or file that was 7 days ago and delete it successfully ${Time}" >> ${Log}
    echo "<-----------------------------END--------------------------------->" >> ${Log}
        echo " " >>${Log}