备份项目实例
1、项目要求
一台 备份服务器 backup 10.0.0.41
两台Web服务器 Web01 10.0.0.7
Web02 10.0.0.8
Web服务器需要备份的目录:/etc /service /var/www/html /var/spool/cron /var/log
按天打包,打包后md5sum生成一个校验值,都放在以自己ip地址命名的目录下,最后通过rsync推送到备份服务器
find /root/ -type f -name "*.tar.gz"|xargs md5sum >>check.md5
10 00 * * * /bin/bash /service/scripts/backup.sh
backup服务器:
- md5sum校验,如果校验失败,发送邮件到运维或运维组
- 清理工作:保留最近一周以及过去每周六的备份
/backup/10.0.0.7
/backup/10.0.0.8
mail -s "`date +F` 备份失败" xxxxx@qq.com < check.info
10 1 * * * /bin/bash /service/scripts/check_backup.sh
md5sum –c check.md5
2、准备环境
创建需要备份的5个目录,并使目录下有文件
快速安装配置backup服务端rsync –daemon
useradd -M -s /sbin/nologin rsync
mkdir /backup
chown -R rsync:rsync /backup
echo '#rsync server
#created by lx 14:18 2015-6-18
##rsyncd.conf start##
uid = rsync
gid = rsync
use chroot = no
max connections = 2000
timeout = 600
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
ignore errors
read only = false
list = false
hosts allow = 192.168.1.0/24
auth users = rsync_backup
secrets file = /etc/rsync.password
#####################################
[backup]
path = /backup' >>/etc/rsyncd.conf
echo "rsync_backup:oldboy" >/etc/rsync.password
chmod 600 /etc/rsync.password
rsync --daemon
netstat -lntup|grep 873
3、客户端备份脚本
cd /service/scripts
[root@web02 scripts]# cat backup.sh
#!/bin/bash
####NO.1 tar
IP=`ifconfig eth0|awk -F '[ :]+' 'NR==2{print $4}'`
mkdir -p /backup/$IP
W=`date +%w`
if [ $W -eq 6 ];then
tar zcvf /backup/$IP/6_etc_`date +%F`.tar.gz /etc
tar zcvf /backup/$IP/6_service_`date +%F`.tar.gz /service
tar zcvf /backup/$IP/6_html_`date +%F`.tar.gz /var/www/html
tar zcvf /backup/$IP/6_cron_`date +%F`.tar.gz /var/spool/cron
tar zcvf /backup/$IP/6_log_`date +%F`.tar.gz /var/log
else
tar zcvf /backup/$IP/etc_`date +%F`.tar.gz /etc
tar zcvf /backup/$IP/service_`date +%F`.tar.gz /service
tar zcvf /backup/$IP/html_`date +%F`.tar.gz /var/www/html
tar zcvf /backup/$IP/cron_`date +%F`.tar.gz /var/spool/cron
tar zcvf /backup/$IP/log_`date +%F`.tar.gz /var/log
fi
####NO.2 check
find /backup/$IP -type f -name "*.tar.gz"|xargs md5sum >>/backup/$IP/check_`date +%F`.md5
####NO.3 rsync
rsync -avz /backup/ rsync_backup@192.168.1.41::backup --password-file=/etc/rsync.password
打包的脚本代码上也可以采取循环的做法,脚本如下
#!/bin/bash
ip=`ifconfig |awk -F "[: ]+" 'NR==2{print $4}'`
a=`date +%w`
mkdir -p /backup/$ip
DATE=`date -d yesterday +%F`
####NO.1 tar&&check
for i in /etc /service /var/www/html /var/spool/cron /var/log
do
name=`echo ${i}|awk -F "/" '{print $NF}'`
if [ $a -eq 0 ];then
tar zcvf /backup/$ip/6_${name}_${DATE}.tar.gz $i
md5sum /backup/$ip/6_${name}_${DATE}.tar.gz >>/backup/${ip}/6_check_${DATE}.md5
else
tar zcvf /backup/$ip/${name}_${DATE}.tar.gz $i
md5sum /backup/$ip/${name}_${DATE}.tar.gz >>/backup/${ip}/check_${DATE}.md5
fi
done
####NO.2 rsync
rsync -avzP /backup/$ip rsync_backup@192.168.1.41::backup --password-file=/etc/rsync.password
####NO.3 clean
find /backup -type f -mtime +3|grep -v '6_'|xargs rm -fr
4、备份服务器检查脚本
[root@backup ~]# cat /service/scripts/check_backup.sh
#!/bin/bash
DATE=`date +%F`
####NO.1 check
find /backup -type f -name "check_${DATE}.md5"|xargs md5sum -c 2>/dev/null >/tmp/check.info
mail -s "$DATE 备份情况" xxxxx@qq.com </tmp/check.info
####NO.2 clean
find /backup -type f -mtime +7|grep -v '/6_'|xargs rm -fr
5、测试脚本
#!/bin/bash
for n in `seq -w 24`
do
date -s "201804$n";/bin/bash /service/scripts/backup.sh
done
ntpdate ntp1.aliyun.com
执行测试脚本后,再执行备份服务器backup的检查脚本,若在执行前删除一个备份文件压缩包,那么邮件内容中会有相应失败提示
博主原创文章,转载请务必注明出处