欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

rsync+inotify实时同步文件

程序员文章站 2024-03-14 09:30:40
...

随着应用系统规模的不断扩大,对数据的安全性和可靠性也提出的更高的要求,rsync在高端业务系统中也逐渐暴露出了很多不足。
首先,rsync在同步数据时,需要扫描所有文件后进行比对,进行差量传输。如果文件数量达到了百万甚至千万量级,扫描所有文件将是非常耗时的,并且正在发生变化的往往是其中很少的一部分,这是非常低效的方式。
其次,rsync不能实时的去监测、同步数据,虽然它可以通过linux守护进程的方式进行触发同步,但是两次触发动作一定会有时间差,这样就导致了服务端和客户端数据可能出现不一致,无法在应用故障时完全的恢复数据。

基于以上两种情况,可以使用rsync+inotify的组合来解决,可以实现数据的实时同步。

inotify是一种强大的、细粒度的、异步的文件系统事件控制机制。linux内核从2.6.13起,加入了inotify支持,通过inotify可以监控文件系统中添加、删除、修改、移动等各种事件,利用这个内核接口,第三方软件就可以监控文件系统下文件的各种变化情况,而inotify-tools正是实施监控的软件。
在使用rsync首次全量同步后,结合inotify对源目录进行实时监控,只要有文件变动或新文件产生,就会立刻同步到目标目录下,非常高效使用!

一、需求说明

分别将
192.168.0.121的/data/upload1实时同步到192.168.0.123的/backup/upload1
192.168.0.122的/data/upload2实时同步到192.168.0.123的/backup/upload2
需要说明的是:
源服务器的/data/upload1和/data/upload2的属主和属组都是dev,希望同步到目标服务器的属主和属组都是nobody,这个在配置目标服务器192.168.0.123的配置文件的时候会用到,根据自己的实际情况填写。

二、详细部署过程

1.在目标服务器192.1680.123上部署rsync服务端

1)关闭selinux

[[email protected]~]# vim /etc/selinux/config
SELINUX=disabled 
[[email protected]~]# setenforce 0

2)若开启了防火墙,则防火墙允许以上两台源服务器访问它的22端口和873端口

[[email protected]~]# vim /etc/sysconfig/iptables
-A INPUT -s 192.168.0.121 -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
-A INPUT -s 192.168.0.122 -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
-A INPUT -s 192.168.0.121 -m state --state NEW -m tcp -p tcp --dport 873 -j ACCEPT
-A INPUT -s 192.168.0.122 -m state --state NEW -m tcp -p tcp --dport 873 -j ACCEPT

3)安装部署rsync服务
注意:CentOS中是以xinetd来管理Rsync服务的,CentOS7中不存在/etc/xinetd.d/rsync(CentOS6中存在),需自己创建,把下面这段复制进去即可。

[[email protected] ~]# yum install -y rsync xinetd
[[email protected] ~]# vim /etc/xinetd.d/rsync 

# default: off
# description: The rsync server is a good addition to an ftp server, as it \
#       allows crc checksumming etc.
service rsync
{
        disable = no    #由默认的yes改为no,设置开机启动rsync
        flags           = IPv6
        socket_type     = stream
        wait            = no
        user            = root
        server          = /usr/bin/rsync
        server_args     = --daemon
        log_on_failure  += USERID
}

启动xineted服务

centos6:/etc/init.d/xinetd start
centos7:systemctl start xinetd

创建/etc/rsyncd.conf文件,并配置同步策略。添加如下代码

[[email protected] ~]# vim /etc/rsyncd.conf 

log file = /var/log/rsyncd.log          #日志文件位置,启动rsync后自动产生这个文件,无需提前创建
pidfile = /var/run/rsyncd.pid           #pid文件的存放位置
lock file = /var/run/rsync.lock        #支持max connections参数的锁文件
secrets file = /etc/rsync.pass         #用户认证配置文件,里面保存用户名称和密码,后面会创建这个文件
motd file = /etc/rsyncd.Motd          #rsync启动时欢迎信息页面文件位置(自己创建这个文件,内容随便自定义)

[upload1]                            #自定义名称
path = /backup/upload1           #rsync服务端数据目录路径,即同步到目标目录后的存放路径
comment = upload1                                             #模块名称与[upload1]自定义名称相同
uid = nobody                                 #设置rsync运行的uid权限。这个要保证同步到目标目录后的权限和源目录一致,即都是nobody!
gid = nobody                                #设置rsync运行的gid权限。
port=873                                    #默认的rsync端口
use chroot = no                          #默认为true,修改为no或false,增加对目录文件软连接的备份
read only = no                            #设置rsync服务端文件为读写权限
list = no                                      #不显示rsync服务端资源列表
max connections = 200              #最大连接数
timeout = 600                           #设置超时时间
auth users = dev                   #执行数据同步的用户名,需要后面手动设置。可以设置多个,用英文状态下逗号隔开
hosts allow = 192.168.0.121        #允许进行数据同步的客户端IP地址,可以设置多个,用英文状态下逗号隔开
#hosts deny = 192.168.1.194       #禁止数据同步的客户端IP地址,可以设置多个,用英文状态下逗号隔开(如果没有禁止,就不用设置这一行)

[upload2] 
path = /backup/upload2
comment = upload2
uid = nobody 
gid = nobody
port=873 
use chroot = no 
read only = no
list = no
max connections = 200 
timeout = 600 
auth users =dev
hosts allow = 192.168.0.122

创建用户认证文件 (多个模块用的是这同一个认证文件)

[[email protected] ~]# vim /etc/rsync.pass
dev:[email protected]

设置文件权限,即rsyncd.conf和rsync.pass认证文件都是600权限!

[[email protected] ~]# chmod 600 /etc/rsyncd.conf 
[[email protected] ~]# chmod 600 /etc/rsync.pass

4)创建rsync同步过来后的目标目录

mkdir -p /data/upload1 /data/upload2
chown -R nobody:nobody /data/upload1 /data/upload2

2.在源服务器192.168.0.121、192.168.0.122部署rsync客户端和inotify监控

以下步骤在两台源服务器上都执行
1)关闭selinux,作为客户端的rsync可以不用在iptables里开放873端口

[[email protected]~]# vim /etc/selinux/config
SELINUX=disabled 
[[email protected]~]# setenforce 0

2)安装rsync
注意:CentOS中是以xinetd来管理Rsync服务的,CentOS7中不存在/etc/xinetd.d/rsync(CentOS6中存在),需自己创建,把下面这段复制进去即可。

[[email protected] ~]# yum install -y rsync xinetd
[[email protected] ~]# vim /etc/xinetd.d/rsync 

# default: off
# description: The rsync server is a good addition to an ftp server, as it \
#       allows crc checksumming etc.
service rsync
{
        disable = no    #由默认的yes改为no,设置开机启动rsync
        flags           = IPv6
        socket_type     = stream
        wait            = no
        user            = root
        server          = /usr/bin/rsync
        server_args     = --daemon
        log_on_failure  += USERID
}

启动xineted服务

centos6:/etc/init.d/xinetd start
centos7:systemctl start xinetd

创建同步的密码文件,这个文件名可以跟服务端的认证文件不一样,但是里面的密码必须一致!用于rsync同步命令中。不过,最好两边的文件设置成一样,便于管理

[[email protected] ~]# vim /etc/rsync.pass
[email protected]

设置rsync.pass密码文件为600权限

[[email protected] ~]# chmod 600 /etc/rsync.pass

3)安装inotify
查看服务器内核是否支持inotify,出现下面的内容,说明服务器内核支持inotify

[[email protected] ~]# ll /proc/sys/fs/inotify
total 0
-rw-r--r-- 1 root root 0 Jun  3 13:49 max_queued_events
-rw-r--r-- 1 root root 0 Jun  3 13:49 max_user_instances
-rw-r--r-- 1 root root 0 Jun  3 13:49 max_user_watches

安装inotify-tools

[[email protected] ~]# yum install -y make gcc gcc-c++
[[email protected] src]# wget https://raw.githubusercontent.com/wangchaoforever/peizhiwenjian/master/rsync/inotify-tools-3.14.tar.gz
[[email protected] src]# tar -zxvf inotify-tools-3.14.tar.gz 
[[email protected] src]# cd inotify-tools-3.14
[[email protected] inotify-tools-3.14]# ./configure --prefix=/usr/local/inotify
[[email protected] inotify-tools-3.14]# make && make install

设置系统环境变量

[[email protected] ~]# vim /etc/profile
export PATH=$PATH:/usr/local/inotify/bin
[[email protected] ~]# source /etc/profile

修改inotify默认参数(inotify默认内核参数值太小)
查看系统默认参数值

[[email protected] ~]# sysctl -a | grep max_queued_events
fs.inotify.max_queued_events = 16384
[[email protected] ~]# sysctl -a | grep max_user_watches
fs.epoll.max_user_watches = 378327
[[email protected] ~]# sysctl -a | grep max_user_instances
fs.inotify.max_user_instances = 128

修改参数:

[[email protected] ~]# sysctl -w fs.inotify.max_queued_events="99999999"
[[email protected] ~]# sysctl -w fs.inotify.max_user_watches="99999999"
[[email protected] ~]# sysctl -w fs.inotify.max_user_instances="65535"

参数说明:

max_queued_events:
inotify队列最大长度,如果值太小,会出现"** Event Queue Overflow **"错误,导致监控文件不准确
max_user_watches:
要同步的文件包含多少目录,可以用:find /Data/xqsj_upload -type d | wc -l 统计这些源目录下的目录数,必须保证max_user_watches值大于统计结果(这里/Data/xqsj_upload为同步的源文件目录)
max_user_instances:
每个用户创建inotify实例最大值

3.第一次全量同步

1)在192.168.0.121服务器上

[[email protected] ~]# rsync -vlzrtogp  --port=873 --progress --delete /data/upload1/ [email protected]::upload1 --password-file=/etc/rsync.passcd 

2)在192.168.0.122服务器上

[[email protected] ~]# rsync -vlzrtogp  --port=873 --progress --delete /data/upload2/ [email protected]::upload2 --password-file=/etc/rsync.passcd 

4.rsync+inotify实时同步脚本操作

1)在192.168.0.121服务器上

[[email protected] upload1]# cd /home/rsync/
[[email protected] rsync]# cat rsync_upload1_inotify.sh 
#!/bin/bash
SRCDIR=/data/upload1/
USER=dev
REMOTEIP=192.168.0.123
DESTDIR=upload1
/usr/local/inotify/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f%e' -e close_write,modify,delete,create,attrib,move $SRCDIR | while read file
do
/usr/bin/rsync -vlzrtogp --port=873 --progress --delete-before $SRCDIR [email protected]$REMOTEIP::$DESTDIR --password-file=/etc/rsync.pass
echo " ${file} was rsynced" >> /tmp/rsync.log 2>&1
done

启动同步脚本,放在后台执行

nohup sh rsync_upload1_inotify.sh &

测试
在源目录中创建一个文件,会自动实时同步到目标机器192.168.0.123的目标目录/backup/upload1中。
2)在192.168.0.122服务器上

[[email protected] ~]# cd /home/rsync/
[[email protected] rsync]# cat rsync_upload2_inotify.sh 
#!/bin/bash
SRCDIR=/data/upload2/
USER=dev
REMOTEIP=192.168.0.123
DESTDIR=upload2
/usr/local/inotify/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f%e' -e close_write,modify,delete,create,attrib,move $SRCDIR | while read file
do
/usr/bin/rsync -vlzrtogp --port=873 --progress --delete-before $SRCDIR [email protected]$REMOTEIP::$DESTDIR --password-file=/etc/rsync.pass
echo " ${file} was rsynced" >> /tmp/rsync.log 2>&1
done

启动同步脚本,放在后台执行

nohup sh rsync_upload2_inotify.sh &

测试
在源目录中创建一个文件,会自动实时同步到目标机器192.168.0.123的目标目录/backup/upload2中。

参考文章:
https://www.cnblogs.com/kevingrace/p/6001252.html

相关标签: rsync