需求:实现指定目录或文件定时打包到本地的/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}