[root@linux-node2 scripts]# vi backup.sh
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
#!/bin/sh IP="`ifconfig eth0|awk -F '[ :]+' 'NR==2 {print $4}'`" Path=/backup/$IP [ ! -d $Path ] && mkdir $Path -p cd $Path #backup tar zcPf $Path/www_$(date +%F).tar.gz /var/html/www/ &&\ tar zcPf $Path/conf_$(date +%F).tar.gz /var/spool/cron/root /etc/rc.d/rc.local /etc/sysconfig/iptables /server/scripts &&\ tar zcPf $Path/logs_$(date +%F).tar.gz /app/logs/ &&\ find /backup/ -type f -name "*$(date +%F).tar.gz"|xargs md5sum >>/$Path/flag_$(date +%F) #to bakup server rsync -az /backup/ rsync_backup@10.89.7.9::backup --password-file=/etc/rsync.password #del find $Path -type f -name "*.tar.gz" -mtime +7|xargs rm -f
#################################################
[root@backup backup]# ll
total 4
drwxr-xr-x 2 rsync rsync 4096 Aug 29 13:43 10.89.7.10
[root@backup backup]# cd 10.89.7.10/
[root@backup 10.89.7.10]# ll
total 16
-rw-r--r-- 1 rsync rsync 934 Aug 29 13:43 conf_2018-08-29.tar.gz
-rw-r--r-- 1 rsync rsync 227 Aug 29 13:43 flag_2018-08-29
-rw-r--r-- 1 rsync rsync 115 Aug 29 13:43 logs_2018-08-29.tar.gz
-rw-r--r-- 1 rsync rsync 118 Aug 29 13:43 www_2018-08-29.tar.gz
[root@backup 10.89.7.10]# cat flag_2018-08-29
da2845ff1010e2ac01cf216929034558 /backup/10.89.7.10/www_2018-08-29.tar.gz
22610f1441a524aa056e824f3dbf624d /backup/10.89.7.10/conf_2018-08-29.tar.gz
7742644088b57b69b396d0025f835097 /backup/10.89.7.10/logs_2018-08-29.tar.gz
[root@backup 10.89.7.10]#
#通过md5sum检查,如果没有问题都是“OK”
[root@backup 10.89.7.10]# md5sum -c flag_2018-08-29
/backup/10.89.7.10/www_2018-08-29.tar.gz: OK
/backup/10.89.7.10/conf_2018-08-29.tar.gz: OK
/backup/10.89.7.10/logs_2018-08-29.tar.gz: OK
#模拟问题
[root@backup 10.89.7.10]# echo 111 >logs_2018-08-29.tar.gz
[root@backup 10.89.7.10]# md5sum -c flag_2018-08-29 |grep FAILED
md5sum: WARNING: 1 of 3 computed checksums did NOT match
/backup/10.89.7.10/logs_2018-08-29.tar.gz: FAILED
#检查脚本:
[root@backup scripts]# cat chkbak.sh
#! /bin/sh /bin/find /backup -type f -name "flag_$(date +%F)"|xargs md5sum -c |grep FAILED >/opt/mail_body_$(date +%F).txt mail -s "$(date +%U%T) back" 123456379@qq.com </opt/mail_body_$(date +%F).txt
#启动邮件服务
[root@backup scripts]# yum install sendmail -y
[root@backup scripts]# /etc/init.d/sendmail start
Starting sendmail: [ OK ]
Starting sm-client: [ OK ]
[root@linux-node2 scripts]#
#执行脚本,查看邮件
[root@backup scripts]# sh chkbak.sh
md5sum: WARNING: 1 of 3 computed checksums did NOT match