利用rsync将多台服务器的日志定时备份到备份服务器

========================利用rsync将多台服务器的日志定时备份到备份服务器========================

------测试说明------

首先rsync环境要搭建好:
rsync_server:192.168.16.66
rsync_client:192.168.16.106

rsync_client每天0点定时将日志打包备份,并通过rsync推送到rsync_server端的/backup目录下;
rsync_client本地保留7天的备份,超过7天的删除。rsync_server端保留6个月的备份记录,超过6个月的删除;
rsync_server每天检查日志的备份情况,并通过邮件发送管理员。

------rsync_server端配置文件------

[root@rsync_server /]# cat /etc/rsyncd.conf
##rsyncd.conf start##
uid = rsync
gid = rsync
use chroot = no
max connections = 200
timeout = 300
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
hosts allow = 192.168.16.0/24
hosts deny = 0.0.0.0/32
auth users = rsync_backup        //虚拟用户,不是系统用户
secrets file = /etc/rsync.password        //密码文件
ignore errors
read only = false
list = false
[lbw]
path = /backup/
#rsync_config_______________end

------rsync_server 创建备份目录/backup并授权------

[root@rsync_server etc]# mkdir /backup
[root@rsync_server /]# chown -R rsync /backup

------rsync_server和rsync_client密码文件------

[root@rsync_server ~]# cat /etc/rsync.password 
rsync_backup:system

[root@rsync_client Wed Aug 21 13:27 ~]# cat /etc/rsync.password 
system


------rsync_client测试发送文件到rsync_server------

[root@rsync_client Wed Aug 21 13:28 etc]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

192.168.2.105   linux_64
192.168.2.106   linux_64_clone
[root@rsync_client Wed Aug 21 13:31 etc]# rsync -avz hosts [email protected]::lbw/ --password-file=/etc/rsync.password 
sending incremental file list
hosts

sent 154 bytes  received 27 bytes  362.00 bytes/sec
total size is 215  speedup is 1.19

[root@rsync_server ~]# cat /backup/hosts 
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

192.168.2.105   linux_64
192.168.2.106   linux_64_clone


------rsync_client打包备份脚本------

[root@rsync_client Wed Aug 21 14:31 scripts]# cat bak.sh 
#!/bin/bash
ip=$(ifconfig eth0 | sed -n 2p | awk -F "[ :]+" '{print $4}')
dir=/backup/$ip
dt=$(date +%F)
[ ! -d $dir ] && mkdir -p $dir
cd $dir &&\
/bin/tar zcf bak_$dt.tar.gz /etc/hosts /var/log/*
/usr/bin/rsync -az /backup/ [email protected]::lbw/ --password-file=/etc/rsync.password &&\
/bin/touch flag_${ip}_$dt &&\
/usr/bin/rsync -az flag_${ip}_$dt [email protected]::lbw/$ip --password-file=/etc/rsync.password &&\
echo "ok"
find $dir -type f -name "*.tar.gz" -mtime +7 | xargs rm -f

------测试脚本------

[root@rsync_client Wed Aug 21 14:27 scripts]# ./bak.sh 
/bin/tar: Removing leading `/' from member names
ok

[root@rsync_server backup]# ll 192.168.16.106/
total 840
-rw-r--r-- 1 rsync rsync 856108 Aug 21 14:37 bak_2019-08-21.tar.gz
-rw-r--r-- 1 rsync rsync      0 Aug 21 14:37 flag_192.168.16.106_2019-08-21

成功后放在定时任务里,定时执行

------rsync_server对备份情况进行验证,并给管理员发送邮件------

[root@rsync_server scripts]# cat mail.sh 
#!/bin/bash
for dir in `ls /backup/`
do 
    if [ -f /backup/$dir/flag_${dir}_$(date +%F) ]
    then
        echo "$dir backup is ok" >>/tmp/backup_result_$(date +%F).log
    else
        echo "$dir backup is fail" >>/tmp/backup_result_$(date +%F).log
    fi
done

[ -f /tmp/backup_result_$(date +%F).log ] &&\
mail -s "$(date +%F) server backup" [email protected]


------测试脚本------

[root@rsync_server backup]# mkdir 192.168.16.107
[root@rsync_server backup]# mkdir 192.168.16.108
[root@rsync_server backup]# chown -R rsync.rsync 192.168.16.107
[root@rsync_server backup]# chown -R rsync.rsync 192.168.16.108
[root@rsync_server backup]# tree
.
├── 192.168.16.106
│  ├── bak_2019-08-21.tar.gz
│  └── flag_192.168.16.106_2019-08-21
├── 192.168.16.107
│  └── 123.txt
└── 192.168.16.108
    └── 1113.txt

3 directories, 4 files


[root@rsync_server scripts]# ./mail.sh 
[root@rsync_server scripts]# 
[root@rsync_server scripts]# cat /tmp/backup_result_2019-08-21.log 
192.168.16.106 backup is ok
192.168.16.107 backup is fail
192.168.16.108 backup is fail

你可能感兴趣的:(利用rsync将多台服务器的日志定时备份到备份服务器)