Web集群之全网备份脚本
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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}
以上是关于Web集群之全网备份脚本的主要内容,如果未能解决你的问题,请参考以下文章
网站集群架构实战(LVS负载均衡Nginx代理缓存Nginx动静分离Rsync+Inotify全网备份Zabbix自动注册全网监控)--技术流ken