rsync 生产中的使用

rsync详解:https://blog.csdn.net/cyt0906/article/details/105686617

需求: 应用服务器的硬盘资源较小,需要针对这些服务的日志做备份。

方案: 采用rsync把需要备份的日志更新到远端备份服务器上面,同时删除备份过后的日志文件。最后使用Jenkins定时执行备份任务。

实施步骤:


一、编写过滤规则

echo $(date +*%Y%m%d*) > exclude.txt
echo $(date +*%Y%m%d* --date '1 days ago') >> exclude.txt
echo $(date +*%Y%m%d* --date '2 days ago') >> exclude.txt
echo $(date +*%Y-%m-%d*) >> exclude.txt
echo $(date +*%Y-%m-%d* --date '1 days ago') >> exclude.txt
echo $(date +*%Y-%m-%d* --date '2 days ago') >> exclude.txt
echo $(date +*%Y_%m_%d*) >> exclude.txt
echo $(date +*%Y_%m_%d* --date '1 days ago') >> exclude.txt
echo $(date +*%Y_%m_%d* --date '2 days ago') >> exclude.txt
# cat exclude.txt
*20200915*
*20200914*
*20200913*
*2020-09-15*
*2020-09-14*
*2020-09-13*
*2020_09_15*
*2020_09_14*
*2020_09_13*

要保留三天的日志文件,所以过滤掉最近三天的日志文件。上面生成了几个时间戳,对应不同的日志文件名格式。*是通配符。


二、编写包含规则

#  cat include.txt
var/app/logs/a
var/app/logs/b
var/app/logs/c
var/app/logs/d

此文件编写我们要备份的目录或文件,可以填写多个规则。

这里介绍一下include的使用

[root@i-7ymj36b6 ccc]# cd /var/app/logs/
[root@i-7ymj36b6 logs]# ls
test1  test2  test3
root@i-7ymj36b6 ~]# cat include.txt
var/app/logs/
var/app/logs/
[root@i-7ymj36b6 ~]# rsync  -arvzu --files-from=/root/include.txt / /root/aaa
[root@i-7ymj36b6 ~]# cd aaa/
[root@i-7ymj36b6 aaa]# tree
.
└── var
    └── app
        └── logs
            ├── test1
            │   ├── test1_1.log
            │   ├── test1_2.log
            │   └── test1_3.log
            ├── test2
            │   ├── test2_1.log
            │   ├── test2_2.log
            │   └── test2_3.log
            └── test3
                ├── test3_1.log
                ├── test3_2.log
                └── test3_3.log

6 directories, 9 files

[root@i-7ymj36b6 ~]# cat include.txt
var/app/logs/test1
var/app/logs/test3
[root@i-7ymj36b6 ~]# rsync  -arvzu --files-from=/root/include.txt / /root/bbb

[root@i-7ymj36b6 ~]# cd bbb/
[root@i-7ymj36b6 bbb]# tree
.
└── var
    └── app
        └── logs
            ├── test1
            │   ├── test1_1.log
            │   ├── test1_2.log
            │   └── test1_3.log
            └── test3
                ├── test3_1.log
                ├── test3_2.log
                └── test3_3.log

5 directories, 6 files

其实大家看一下就能看明白,include文件的作用了,和exclude类似。里面也可以用正则、通配符来进行匹配。所以include编写能够满足我们的需求,就可以了。大家可以多去尝试,去发现适合自己的用法。


三、备份命令

rsync  -arvzu --remove-source-files --exclude-from=/root/exclude.txt --files-from=/root/include.txt / rsync_blm@192.168.1.93::1-29-backup --password-file=/etc/rsync.password

–remove-source-files 备份过后的文件执行删除操作
–exclude-from 指定过滤文件的位置
–files-from 指定包含规则的文件位置
/ 源端,也就是发送端
rsync_blm@192.168.1.93::1-29-backup 目标端,也就是接收端,这是rsync daemon模式下的写法
–password-file 因为使用的是rsync daemon模式 所以要指定一下密码,因为服务端配置了需要密码

解释一下,就是把 / 从根目录开始,然后匹配包含规则,备份包含规则里面的目录和文件。同时匹配排除规则,过滤掉不需要处理的文件。最后把需要处理的文件移动到远端备份服务器上面。

这里不多说 rsync daemon 模式。看我上篇文章,里面有。


四、最后配置服务端

配置文件内容

[root@i-tolh8p25 ~]# cat /etc/rsyncd.conf
# /etc/rsyncd: configuration file for rsync daemon mode

# See rsyncd.conf man page for more options.

# configuration example:

uid = root
gid = root
port = 873
use chroot = no
max connections = 2000
pid file = /var/run/rsyncd.pid
lock file = /var/run/rync.lock
log file = /var/log/rsync.log
#hosts allow = 192.168.1.0/95
#hosts deny = 0.0.0.0/32
auth users = rsync_blm
secrets file = /etc/rsync.password
# exclude = lost+found/
# transfer logging = yes
timeout = 90000
# ignore nonreadable = yes
# dont compress   = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2
#######################
[0-37-backup]
    path = /data/192-168-0-37-backup
    ignore errors
    read only = no
    list = no
[1-27-backup]
    path = /data/192-168-1-27-backup
    ignore errors
    read only = no
    list = no
[0-6-backup]
    path = /data/192-168-0-6-backup
    ignore errors
    read only = no
    list = no

关于配置文件详解,上篇文章也有提到。这里只是一部分的配置文件,其实是有很多模块的,每一个模块代表一台服务器的备份目录。模块名我直接用服务器IP地址表示了。

其实本篇文章只是说一个生产中的备份方案,和实施过程,如果对应rsync很了解的,相信都能看的明白,看不明白的可以先看看我上篇文章。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值