Rsync+Inotify实现目录实时同步

7年前 (2017-05-18) gtj linux 0评论 已收录 1319℃

rsync 端口号:873
安装rsync软件后,系统默认不存在rsync配置文件,rsync主要为三个配置文件:rsyncd.conf(主配置文件)、rsyncd.secrets(密码文件)、rsyncd.motd(服务器信息文件)

rsync服务端:
rsync服务器需要创建至少一个配置文件,使用守护进程模式启动rsync程序即可
yum -y install rsync xinetd #安装
vi /etc/xinetd.d/rsync #编辑配置文件,设置开机启动rsync
disable = no #修改为no
:wq! #保存退出
/etc/init.d/xinetd start #启动(CentOS中是以xinetd来管理Rsync)

创建rsync.conf配置文件
vim /etc/rsync.conf
#/etc/rsyncd.conf
motd file = /etc/rsyncd.motd #设置服务器信息提示文件名称,在该文件中编写提示信息
transfer logging = yes #开启Rsync数据传输日志功能
log file = /var/run/rsyncd.log #设置日志文件名称,可以通过log format参数设置日志格式
pid file = /var/run/rsync.pid #设置rsync进程号保存文件名称
lock file = /var/run/rsync.lock #设置锁文件名称
port = 873 #设置服务器监听的端口号,默认问873
address = 192.168.10.154 #设置服务器所监听网卡接口的IP地址,这台服务器IP地址为192.168.10.154
uid = nobody #设置进行数据传输时所使用的账户名称或ID号,默认使用nobody
gid = nobody #设置进行数据传输时所使用的组名称或GID号,默认使用nobody
use chroot = no #设置user chroot为yes后,rsync会首先进行chroot设置,将跟映射到path参数路径下,对客户端而言,
#系统地跟就是path参数所指定的路径。但这样做需要root权限,并且在同步符号连接资料时仅会同步名称,而内容将不会同步
read only = yes #是否允许客户端上传数据,这里设置为只读
max connections = 10 #设置并发连接数,0代表无限制。超出并发数后,如果依然有客户端连接请求,则将会收到稍后重试的提示消息

[common]
comment = Web content #定义注释说明自串
path = /common #同步目录的真实路径
ignore errors = ture #忽略一些IO错误
read only = no #允许客户端上传数据
#exclude = test/ #exclude可以指定common目录下的某个目录为不同步数据
auth users = rsync,oyb #设置允许连接服务器的账户,账户可以是系统中不存在的用户
secrets file = /etc/rsyncd.secrets #设置密码验证文件名称,注意该文件的权限要求为只读,建议权限为600,仅在设置auth users参数后有效
hosts allow=192.168.0.0/255.255.0.0 #设置允许哪些主机可以同步数据,可以是单个IP,也可以是网段,多个IP与网段之间使用空格分隔
hosts deny=* #设置拒绝所有(除hosts allow定义的主机外)
list = no #客户端请求显示模块列表时,本模块名称是否显示,默认为true
timeout = 600 #设置超时时间

创建rsync用户和组,修改/common模块用户和组
useradd -s /sbin/nologin -M rsync
groupadd rsync ; mkdir /common
chown rsync.rsync /common ; cp /etc/init.d/* /common/

创建rsyncdsecrets配置文件
echo “rsync:pass” > /etc/rsyncd.secrets
echo "oybyb" >> /etc/rsyncd.secrets
chmod 600 /etc/rsyncd.secrets

创建rsyncd.motd配置文件
echo "welcome to access " > /etc/rsyncd.motd

启动rsync服务
rsync --daemon
echo "/usr/bin/rsync --daemon" >> /etc/rc.local
iptables -I INPUT -p tcp --dport 873 -J ACCEPT
service iptables save

rsync客户端:
yum -y install rsync

echo "pass" >> /etc/rsyncd.secrets
chmod 600 /etc/rsyncd.secrets

rsync -azp --delete test.txt rsync@192.168.10.154::common --password-file=/etc/rsyncd.secrets

-a 保存所有属性,递归方式
-z 压缩传输
-p 显示进度
--delete 删除那些目标文件有而原始位置没有的文件
--password-file

inotify

可以监控文件系统地各种变化情况,实时了解文件系统发生的所有变化
inux内核从2.6.13起,加入了inotify支持,通过inotify可以监控文件系统中添加、删除、修改、移动等各种事件,利用这个内核接口,第三方软件就可以监控文件系统下文件的各种变化情况,而inotify-tools正是实施监控的软件。
随着应用系统规模的不断扩大,对数据的安全性和可靠性也提出的更好的要求,rsync在高端业务系统中也逐渐暴露出了很多不足,首先,rsync同步数据时,需要扫描所有文件后进行比对,进行差量传输。如果文件数量达到了百万甚至千万量级,扫描所有文件将是非常耗时的。而且正在发生变化的往往是其中很少的一部分,这是非常低效的方式。其次,rsync不能实时的去监测、同步数据,虽然它可以通过Linux守护进程的方式进行触发同步,但是两次触发动作一定会有时间差,这样就导致了服务端和客户端数据可能出现不一致,无法在应用故障时完全的恢复数据。基于以上原因,rsync+inotify组合出现了!

查看内核是否支持inotify:
方法一:
[root@template ~]# ll /proc/sys/fs/inotify
total 0
-rw-r--r-- 1 root root 0 Apr 22 17:58 max_queued_events 表示调用inotify_init时分配给inotify instance中可排队的event的数目的最大值,超出这个值的事件被丢弃,但会触发IN_Q_OVERFLOW事件。
-rw-r--r-- 1 root root 0 Apr 22 17:58 max_user_instances 表示每一个real user ID可创建的inotify instatnces的数量上限。
-rw-r--r-- 1 root root 0 Apr 22 17:58 max_user_watches 表示每个inotify instatnces可监控的最大目录数量。如果监控的文件数目巨大,需要根据情况,适当增加此值的大小。
显示这三个文件则证明支持inotify
方法二:
uname -r 查看内核版本,如果内核版本在2.6.13之后,那么该内核支持inotify,

安装inotify:
wget http://github.com/downloads/rvoi ... y-tools-3.14.tar.gz
tar -xzvf inotify-tools-3.14.tar.gz -C /tmp ; cd /tmp/inotify-tools-3.14
./configure --prefix=/usr/local/inotify-3.14
make && make install

inotifywait命令:
-e 指定监控那些事件,时间包括:create、delete、move、modify、attrib(文件或目录属性被修改)、access(文件或目录被读取)、umount(文件系统被卸载)、open(文件或目录被打开)、close
-m 持续监控
-r 递归整个目录
-q 简化输出信息
inotifywait -mrq -e create、delete、move、modify、attrib /common

inotify脚本:
[root@inotify-master scripts]# cat inotify.sh
#!/bin/bash
/usr/local/webserver/inotify-3.13/bin/inotifywait -mrq -e create,delete,move,modify,attrib /tmp | while read file
do
rsync -azp --delete $file rsync@192.168.10.154::common --password-file=/etc/rsyncd.secrets
done
[root@inotify-master scripts]# sh inotify.sh & #将脚本加入后台执行
[1] 13438

博主

这货来去如风,什么鬼都没留下!!!

相关推荐

嗨、骚年、快来消灭0回复。

×
订阅图标按钮
Less is more!!!