rsync+inotify 实现两个服务端数据实时同步

一、rsync服务端部署

两个服务端 192.168.1.234、172.16.8.85。在此192.168.1.234作为数据运行的客户端,172.16.8.85作为数据备份的服务端。
而rsync服务端则放在172.16.8.85上,用以不断的从192.168.1.234上接收数据进行备份。在此我把192.168.1.234叫做客户端,172.16.8.85叫做服务端。

1、查看服务端rsync版本
在这里插入图片描述
2、创建配置文件
默认安装rsync程序后,不会自动创建rsync的主配置文件,需要手工创建,在 /etc 下创建rsyncd.conf文件,并配置如下内容:

#rsync server 为 rsyncd 服务编辑配置文件,默认没有,需自己编辑
#create by sherwin 13:41 2019-3-29
##rsyncd.conf start##

#rsync运行权限为sherwin
uid = sherwin

#rsync运行权限为sherwin
gid = sherwin

#是否让进程离开工作目录
use chroot = no

#客户端最大并发连接数 ,0为不限制
max connections = 2000 

#连接超时
timeout = 600   

#指定rsync的pid存放路径
pid file = /var/run/rsyncd.pid   

#指定rsync的锁文件存放路径
lock file = /var/run/rsyncd.lock

#指定rsync的日志存放路径
log file = /var/log/rsyncd.log

#忽略一些无关的I/O错误
ignore errors

#客户端可以上传
read only = false

#客户端可以下载
#write only = false 
list = false

#允许连接的客户端主机ip
hosts allow = 192.168.1.0/24

#黑名单,*表示任何主机
hosts deny = 0.0.0.0/23

#认证此模块的用户名/#授权远程连接的用户
auth users = sherwin

#存放用户和密码的文件 "user:pwd"格式
secrets file = /etc/rsync.password

###############################################
[backup]#backup是模块的名称,后面传递过程中使用
comment = backup by sherwin 13:41 2019-3-29
#该模块存放文件的基础路径
path = /home/sherwin/backup

3、启动服务
创建好配置文件后,使用如下命令启动与查看

[root@servicel06 /]# rsync --daemon
failed to create pid file /var/run/rsyncd.pid: File exists	//这是已经有一个rsync服务起来
[root@servicel06 /]# pkill rsync	//使用pkill命令杀死进程
[root@servicel06 /]# 
[root@servicel06 /]# 
[root@servicel06 /]# rsync --daemon	//启动服务
[root@servicel06 /]# ps -ef | grep rsync	//查看rsync进程
root     11321     1  0 16:22 ?        00:00:00 rsync --daemon
root     11341 20836  0 16:22 pts/3    00:00:00 grep rsync
[root@servicel06 /]#
[root@servicel06 /]# netstat -lntup | grep rsync	//查看rsync对应的端口有无起来
tcp        0      0 0.0.0.0:873                 0.0.0.0:*                   LISTEN      11321/rsync         
tcp        0      0 :::873                      :::*                        LISTEN      11321/rsync         
[root@servicel06 /]# lsof -i :873
COMMAND   PID USER   FD   TYPE   DEVICE SIZE/OFF NODE NAME
rsync   11321 root    3u  IPv6 45884134      0t0  TCP *:rsync (LISTEN)
rsync   11321 root    5u  IPv4 45884135      0t0  TCP *:rsync (LISTEN)

4、创建rsync配套的虚拟用户
检查在配置文件中所写的用户是否存在,如果不存在则创建一个

#rsync运行权限为sherwin							--配置文件中
uid = sherwin
#rsync运行权限为sherwin
gid = sherwin
[root@servicel06 /]# useradd sherwin	 //使用useradd命令创建用户;或者创建虚拟用户 useradd sherwin 	-s /sbin/nologin
useradd: user sherwin exists
[root@servicel06 /]# id sherwin
uid=511(sherwin) gid=511(sherwin) groups=511(sherwin)
[root@servicel06 /]# 
[root@servicel06 /]# 

5、创建backup目录(也就是要备份文件的目录),并且修改目录的所属用户为sherwin,与配置文件中的uid、gid相同。

[root@servicel06 sherwin]# mkdir backup
[root@servicel06 sherwin]# ls -ld backup/
drwxr-xr-x 2 root root 4096 Apr  4 16:37 backup/
[root@servicel06 sherwin]#
[root@servicel06 sherwin]# chown -R sherwin backup/
[root@servicel06 sherwin]# ls -ld backup/
drwxr-xr-x 2 sherwin root 4096 Apr  4 16:37 backup/
[root@servicel06 sherwin]#
[root@servicel06 sherwin]# chgrp -R sherwin backup/
[root@servicel06 sherwin]# ls -ld backup/
drwxr-xr-x 2 sherwin sherwin 4096 Apr  4 16:37 backup/

6、配置客户端登录的用户名跟密码,并修改存储密码的文件权限为600。(这个用户名就是配置文件中 auth users)

#认证此模块的用户名/#授权远程连接的用户
auth users = sherwin
[root@servicel06 sherwin]# echo "sherwin:sherwin">>/etc/rsync.password //配置客户端登录用户和密码
[root@servicel06 sherwin]# cat /etc/rsync.password 
sherwin:sherwin
[root@servicel06 sherwin]# ll /etc/rsync.password 
-rw-r--r-- 1 root root 16 Apr  4 16:44 /etc/rsync.password
[root@servicel06 sherwin]# chmod 600 /etc/rsync.password //修改/etc/rsync.password密码文件的权限为600, 只允许root访问
[root@servicel06 sherwin]# ll /etc/rsync.password 
-rw------- 1 root root 16 Apr  4 16:44 /etc/rsync.password
[root@servicel06 sherwin]# 

7、将“rsync --daemon”加入开机自启动,修改/etc/rc.local文件

[root@servicel06 sherwin]# echo "rsync --daemon" >>/etc/rc.local
[root@servicel06 sherwin]# cat /etc/rc.local
#!/bin/sh
#
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don't
# want to do the full Sys V style init stuff.
/sbin/service crond start
touch /var/lock/subsys/local
echo deadline > /sys/block/sda/queue/scheduler
rsync --daemon --address=172.16.8.85  //绑定制定IP:172.16.8.85提供服务
rsync --daemon
[root@servicel06 sherwin]# 

8、同步安全优化(可选)

[root@servicel06 sherwin]# lsof -i :873	//查看873端口是否起来
COMMAND   PID USER   FD   TYPE   DEVICE SIZE/OFF NODE NAME
rsync   11321 root    3u  IPv6 45884134      0t0  TCP *:rsync (LISTEN)
rsync   11321 root    5u  IPv4 45884135      0t0  TCP *:rsync (LISTEN)
[root@servicel06 sherwin]# netstat -lntup | grep rsync
tcp        0      0 0.0.0.0:873                 0.0.0.0:*                   LISTEN      15038/rsync         
tcp        0      0 :::873                      :::*                        LISTEN      15038/rsync         
[root@servicel06 sherwin]# pkill rsync	//杀进程
[root@servicel06 sherwin]# rsync --daemon --address=172.16.8.85	//绑定指定IP:192.168.1.17提供服务
[root@servicel06 sherwin]# lsof -i :873
COMMAND   PID USER   FD   TYPE   DEVICE SIZE/OFF NODE NAME
rsync   15113 root    3u  IPv4 45995034      0t0  TCP ssdb.readwrite.host:rsync (LISTEN)
[root@servicel06 sherwin]# netstat -lntup | grep rsync
tcp        0      0 172.16.8.85:873             0.0.0.0:*                   LISTEN      15113/rsync         
[root@servicel06 sherwin]# 

二、inotify + rsync 客户端部署

1、配置客户端密码(登陆到rsync服务端的密码)
就是在服务端配置密码文件中的用户名与密码,后面在同步时直接使用文件,不需要再次输入密码。

[root@DF-Svr234 sherwin]# echo "sherwin">>/etc/rsync.password
[root@DF-Svr234 sherwin]# cat /etc/rsync.password 
sherwin
[root@DF-Svr234 sherwin]# chmod 600 /etc/rsync.password 
[root@DF-Svr234 sherwin]# ls -ld /etc/rsync.password 
-rw------- 1 root root 8 Apr  4 17:42 /etc/rsync.password
[root@DF-Svr234 sherwin]#

2、备份/home/sherwin/tmp/目录下所有文件到服务端的backup/目录下

[root@DF-Svr234 sherwin]# ll tmp/
total 0
[root@DF-Svr234 sherwin]# touch tmp/{a..f}
[root@DF-Svr234 sherwin]# ll tmp/
total 0
-rw-r--r-- 1 root root 0 Apr  4 17:49 a
-rw-r--r-- 1 root root 0 Apr  4 17:49 b
-rw-r--r-- 1 root root 0 Apr  4 17:49 c
-rw-r--r-- 1 root root 0 Apr  4 17:49 d
-rw-r--r-- 1 root root 0 Apr  4 17:49 e
-rw-r--r-- 1 root root 0 Apr  4 17:49 f
[root@DF-Svr234 sherwin]#  rsync -avz ./tmp/ sherwin@172.16.8.85::backup --password-file=/etc/rsync.password	//push备份
sending incremental file list
./
a
b
c
d
e
f

sent 281 bytes  received 125 bytes  270.67 bytes/sec
total size is 0  speedup is 0.00
[root@DF-Svr234 sherwin]# 

3、检查服务端backup目录下的文件

[root@servicel06 backup]# ll
total 0
-rw-r--r-- 1 sherwin sherwin 0 Apr  4  2019 a
-rw-r--r-- 1 sherwin sherwin 0 Apr  4  2019 b
-rw-r--r-- 1 sherwin sherwin 0 Apr  4  2019 c
-rw-r--r-- 1 sherwin sherwin 0 Apr  4  2019 d
-rw-r--r-- 1 sherwin sherwin 0 Apr  4  2019 e
-rw-r--r-- 1 sherwin sherwin 0 Apr  4  2019 f
[root@servicel06 backup]# 

4、安装inotify工具
inotify-tools下载地址:http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
查看当前系统是否支持inotify

[root@DF-Svr234 sherwin]# uname -r	//内核版本,从kernel 12.6.13开始支持
2.6.18-409.el5
[root@DF-Svr234 sherwin]# ls -l /proc/sys/fs/inotify/		//查看是否支持
total 0
-rw-r--r-- 1 root root 0 Apr  8 09:17 max_queued_events
-rw-r--r-- 1 root root 0 Apr  8 09:17 max_user_instances
-rw-r--r-- 1 root root 0 Apr  8 09:17 max_user_watches
[root@DF-Svr234 sherwin]# 	//显示当前三个文件表示支持

5、编译安装inotify

[root@DF-Svr234 sherwin]# ls -ld inotify-tools-3.14.tar.gz 
-rw-r--r-- 1 root root 358772 Mar 29 10:53 inotify-tools-3.14.tar.gz
[root@DF-Svr234 sherwin]# tar -zxvf inotify-tools-3.14.tar.gz 
[root@DF-Svr234 sherwin]# 
[root@DF-Svr234 sherwin]# cd inotify-tools-3.14
[root@DF-Svr234 inotify-tools-3.14]# ./configure --prefix=/usr/local/inotify-tools-3.14
[root@DF-Svr234 inotify-tools-3.14]# make & make install
[root@DF-Svr234 inotify-tools-3.14]# ln -s /usr/local/inotify-tools-3.14/ /usr/local/inotify-tools	//创建软连接
[root@DF-Svr234 inotify-tools-3.14]# ll /usr/local/
total 112
drwxr-xr-x 17 root root 4096 May 17  2016 anaconda2
drwxr-xr-x 17 root root 4096 May 16  2016 anaconda3
drwxr-xr-x  2 root root 4096 Nov  8 17:03 bin
drwxr-xr-x  2 root root 4096 May 11  2011 etc
drwxr-xr-x  2 root root 4096 May 11  2011 games
drwxr-xr-x  4 root root 4096 Nov  8 16:22 include
lrwxrwxrwx  1 root root   30 Mar 29 10:58 inotify-tools -> /usr/local/inotify-tools-3.14/
drwxr-xr-x  6 root root 4096 Apr  8 09:26 inotify-tools-3.14
drwxr-xr-x  6 root root 4096 Nov  8 17:03 lib
drwxr-xr-x  2 root root 4096 Jan 22  2018 lib64
drwxr-xr-x  3 root root 4096 Nov  8 15:42 libexec
drwxr-xr-x  3 root root 4096 Sep  5  2016 man
drwxr-xr-x  2 root root 4096 May 11  2011 sbin
drwxr-xr-x 11 root root 4096 Nov  8 17:03 share
drwxr-xr-x  2 root root 4096 May 11  2011 src
drwxr-xr-x  3 root root 4096 Nov  8 17:03 var
[root@DF-Svr234 inotify-tools-3.14]# 

提示编译成功后生成4个目录,分别是:

[root@DF-Svr234 local]# cd inotify-tools
[root@DF-Svr234 inotify-tools]# ll
total 16
drwxr-xr-x 2 root root 4096 Mar 29 10:57 bin		//inotify执行命令(二进制)
drwxr-xr-x 3 root root 4096 Mar 29 10:57 include 	//inotify程序所需要的头文件
drwxr-xr-x 2 root root 4096 Mar 29 10:57 lib	//动态链接的库文件
drwxr-xr-x 4 root root 4096 Mar 29 10:57 share	//帮助文档
[root@DF-Svr234 inotify-tools]# 

工具集和介绍:
一共安装了2个工具(命令),即inotifywait和inotifywatch
inotifywait:在被监控的文件或目录上等待特定文件系统事件(open、close、delete等)发生,执行后处于阻塞状态,适合在shell脚本中使用
inotifywatch:收集被监视的文件系统使用统计数据,指定文件系统事件发生的次数统计

6、一般工作中使用到:

[root@DF-Svr234 sherwin]# ./inotifywait  -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e modify,create,move,delete,attrib,close_write /home/sherwin/tmp

“%y %m %d %H %M”----“年 月 日 小时 分钟”
“%T %w %f”-----“时间 路径 文件名” (%w%f 表达的是路径+文件名,也就是绝对路径)

通过脚本实时同步:

#!/bin/bash
inotify=/usr/local/inotify-tools/bin/inotifywait
$inotify  -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e modify,create,move,delete,attrib,close_write /home/sherwin/tmp | while read events
do
        cd /home/sherwin/ &&
        rsync -avzP ./tmp/ --delete sherwin@172.16.8.85::backup --password-file=/etc/rsync.password
        echo "'date + '%F%T'' new events: $events" >> /home/sherwin/rsync.log 2>&1
done

脚本作如下修改改进(可略过):

#!/bin/bash

inotify=/usr/local/inotify-tools/bin/inotifywait
path=/home/sherwin/tmp
backup_server=172.16.8.85

$inotify  -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e create,delete,close_write $path | while read events

do
        cd $path &&\
        rsync -avzP ./ --delete sherwin@$backup_server::backup --password-file=/etc/rsync.password

        echo "new events: $events" >> /home/sherwin/rsync.log 2>&1
done

脚本改进,结合rsync的特性,分开判断来实现一个目录的增删改查对应的操作:

#!/bin/bash
src=/home/sherwin/tmp                         	# 需要同步的源路径
dst=backup                             			# 目标服务器上 rsync --daemon 发布的名称,rsync --daemon这里就不做介绍了,网上搜一下,比较简单。
rsync_passwd_file=/etc/rsync.password           # rsync验证的密码文件
ip1=172.16.8.85                 				# 目标服务器1
#ip2=192.168.0.19                		 		# 目标服务器2
user=sherwin                            		# rsync --daemon定义的验证用户名
inotify=/usr/local/inotify-tools/bin/inotifywait	# inotify目录

cd ${src}                              			# 此方法中,由于rsync同步的特性,这里必须要先cd到源目录,inotify再监听 ./ 才能rsync同步后目录结构一致

$inotify -mrq --timefmt '%d/%m/%y %H:%M' --format  '%T %Xe %w%f' -e modify,create,delete,attrib,close_write,move ./ | while read file         # 把监控到有发生更改的"文件路径列表"循环

do
        INO_EVENT=$(echo $file | awk '{print $3}')      # 把inotify输出切割 把事件类型部分赋值给INO_EVENT
        INO_FILE=$(echo $file | awk '{print $4}')       # 把inotify输出切割 把文件路径部分赋值给INO_FILE
        echo "-------------------------------$(date)------------------------------------"
        echo $file
        #增加、修改、写入完成、移动进事件
        #增、改放在同一个判断,因为他们都肯定是针对文件的操作,即使是新建目录,要同步的也只是一个空目录,不会影响速度。
        if [[ $INO_EVENT =~ 'CREATE' ]] || [[ $INO_EVENT =~ 'MODIFY' ]] || [[ $INO_EVENT =~ 'CLOSE_WRITE' ]] || [[ $INO_EVENT =~ 'MOVED_TO' ]]         # 判断事件类型
        then 
				echo "'date + '%F%T'' new events: $file" >> /home/sherwin/rsync.log 2>&1
                rsync -avzcR --password-file=${rsync_passwd_file} $(dirname ${INO_FILE}) ${user}@${ip1}::${dst}         # INO_FILE变量代表路径哦  -c校验文件内容
                 #仔细看 上面的rsync同步命令 源是用了$(dirname ${INO_FILE})变量 即每次只针对性的同步发生改变的文件的目录(只同步目标文件的方法在生产环境的某些极端环境下会漏文件 现在可以在不漏文件下也有不错的速度 做到平衡) 然后用-R参数把源的目录结构递归到目标后面 保证目录结构一致性
        fi
        #删除、移动出事件
        if [[ $INO_EVENT =~ 'DELETE' ]] || [[ $INO_EVENT =~ 'MOVED_FROM' ]]
        then
                echo "'date + '%F%T'' new events: $file" >> /home/sherwin/rsync.log 2>&1
                rsync -avzR --delete --password-file=${rsync_passwd_file} $(dirname ${INO_FILE}) ${user}@${ip1}::${dst}
                #看rsync命令 如果直接同步已删除的路径${INO_FILE}会报no such or directory错误 所以这里同步的源是被删文件或目录的上一级路径,并加上--delete来删除目标上有而源中没有的文件,这里不能做到指定文件删除,如果删除的路径越靠近根,则同步的目录月多,同步删除的操作就越花时间。这里有更好方法的同学,欢迎交流。
        fi
        #修改属性事件 指 touch chgrp chmod chown等操作
        if [[ $INO_EVENT =~ 'ATTRIB' ]]
        then
                echo "'date + '%F%T'' new events: $file" >> /home/sherwin/rsync.log 2>&1
                if [ ! -d "$INO_FILE" ]                 # 如果修改属性的是目录 则不同步,因为同步目录会发生递归扫描,等此目录下的文件发生同步时,rsync会顺带更新此目录。
                then
                        rsync -avzcR --password-file=${rsync_passwd_file} $(dirname ${INO_FILE}) ${user}@${ip1}::${dst}            
                fi
        fi
done

以上脚本的修改只有在文件有变化时才会同步,则需要追加一个每天或者两个小时进行一次全同步的脚本,来保证短线或者重启时没有同步的文件:

#!/bin/sh

#src={{ dict_dir_update_server_v9 }}/DataDictionary                         	# 需要同步的源路径
src=/opt/donghua/datafeeds/UpdateServerV9/data/SSDictionary/DataDictionary     # 需要同步的源路径
dst=backup                             										# 目标服务器上 rsync --daemon 发布的名称
rsync_passwd_file=/etc/rsync.password           							# rsync验证的密码文件
dst_ip=172.16.8.85                 											# 目标服务器1
user=donghua                            									# rsync --daemon定义的验证用户名

cd ${src} 

#once a day
has_run="false"	
trigger_time=2300
reset_time=0100

while false ; do
	cur_time=`date +%H%M`

    if [ $cur_time -gt $trigger_time ] && [ $has_run = "false" ] ; then
		echo "'date + '%F%T'' new events: $(date) all backup"
		rsync -avz --no-iconv --password-file=${rsync_passwd_file} ./ ${user}@${dst_ip}::${dst} 
		rsync -avz --no-iconv --password-file=${rsync_passwd_file} ${user}@${dst_ip}::${dst} ./
		has_run="true"	
    fi

	if [ $cur_time -le $reset_time ] ; then
		has_run="false"
	fi

	sleep 10

done


#Every two hours
last_time=`date +%s`
 
while true ; do
	end_time=`date +%s`
	total_time=$(( end_time - last_time ))
	
	if [ $total_time -gt 7200 ] ; then
		last_time=${end_time}
		echo "'date + '%F%T'' new events: $(date) all backup"
		rsync -avz --no-iconv --password-file=${rsync_passwd_file} ./ ${user}@${dst_ip}::${dst} 
		rsync -avz --no-iconv --password-file=${rsync_passwd_file} ${user}@${dst_ip}::${dst} ./
	else
		sleep 10
	fi

done

注意:
在rsync命令中加入–no-iconv 参数,是因为同步文件时,遇到了一个错误:
这个错误在其官网上也没有明确的定论,则只能增大延时时间与 添加这个参数

rsync: writefd_unbuffered failed to write 4 bytes to socket [sender]: Connection reset by peer (104)
rsync: connection unexpectedly closed (314587 bytes received so far) [sender]
rsync error: error in rsync protocol data stream (code 12) at io.c(600) [sender=3.0.6]
rsync: failed to connect to 172.16.8.85: Connection refused (111)
rsync error: error in socket IO (code 10) at clientserver.c(124) [receiver=3.0.6]
'date + '%F%T'' new events: 2019年 04月 10日 星期三 16:22:55 CST all backup
rsync: failed to connect to 172.16.8.85: Connection refused (111)
rsync error: error in socket IO (code 10) at clientserver.c(124) [sender=3.0.6]
rsync: failed to connect to 172.16.8.85: Connection refused (111)
rsync error: error in socket IO (code 10) at clientserver.c(124) [receiver=3.0.6]
'date + '%F%T'' new events: 2019年 04月 10日 星期三 16:26:15 CST all backup

脚本开机可以加入启动:

[root@DF-Svr234 sherwin]# cat /etc/rc.local 
#!/bin/sh
#
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don't
# want to do the full Sys V style init stuff.

touch /var/lock/subsys/local
/opt/donghua/data-service/qpid/qpidd.sh
~                                                                                                          
[root@DF-Svr234 sherwin]# echo "/bin/sh /home/sherwin/inotify.sh &" >> /etc/rc.local
[root@DF-Svr234 sherwin]# 
提示:
一个& 代表从后台开始运行该条命令。

关键参数调整优化:
在/proc/sys/fs/inotify目录下有三个文件,对inotify机制有一定的限制
max_user_watches:设置inotifywait或inotifywatch命令可以监视的文件数量(单进程)
max_user_instances:设置每个用户可以运行的inotifywait或inotifywatch命令的进程数
max_queued_events:设置inotify实例事件(event)队列可容纳的事件数量。

[root@DF-Svr234 test]# cat /proc/sys/fs/inotify/max_user_watches 
8192
[root@DF-Svr234 test]# echo "50000000" >/proc/sys/fs/inotify/max_user_watches 
[root@DF-Svr234 test]# cat /proc/sys/fs/inotify/max_user_watches              
50000000
[root@DF-Svr234 test]# cat /proc/sys/fs/inotify/max_queued_events 
16384
[root@DF-Svr234 test]# echo "50000000" >/proc/sys/fs/inotify/max_queued_events 
[root@DF-Svr234 test]# cat /proc/sys/fs/inotify/max_queued_events              
50000000
[root@DF-Svr234 test]# sysctl -p
net.ipv4.ip_forward = 0
net.ipv4.conf.default.rp_filter = 1
net.ipv4.conf.default.accept_source_route = 0
kernel.sysrq = 0
kernel.core_uses_pid = 1
net.ipv4.tcp_syncookies = 1
kernel.msgmnb = 65536
kernel.msgmax = 65536
kernel.shmmax = 68719476736
kernel.shmall = 4294967296
[root@DF-Svr234 test]# 

7、测试:
客户端:

脚本属于开启状态:
[root@DF-Svr234 sherwin]# sh -x inotify.sh 
+ inotify=/usr/local/inotify-tools/bin/inotifywait
+ /usr/local/inotify-tools/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e modify,create,move,delete,attrib,close_write /home/sherwin/tmp
+ read events
+ cd /home/sherwin/
+ rsync -avzP ./tmp/ --delete sherwin@172.16.8.85::backup --password-file=/etc/rsync.password
sending incremental file list
./
deleting f
deleting e
deleting d
deleting c
deleting b
deleting a
1
           0 100%    0.00kB/s    0:00:00 (xfer#1, to-check=3/5)
2
           0 100%    0.00kB/s    0:00:00 (xfer#2, to-check=2/5)
3
           0 100%    0.00kB/s    0:00:00 (xfer#3, to-check=1/5)
4
           0 100%    0.00kB/s    0:00:00 (xfer#4, to-check=0/5)

sent 201 bytes  received 87 bytes  192.00 bytes/sec
total size is 0  speedup is 0.00
+ echo ''\''date + '\''%F%T'\'''\'' new events: 08/04/19 09:55 /home/sherwin/tmp/1'
+ read events
+ cd /home/sherwin/
+ rsync -avzP ./tmp/ --delete sherwin@172.16.8.85::backup --password-file=/etc/rsync.password
sending incremental file list

sent 54 bytes  received 8 bytes  124.00 bytes/sec
total size is 0  speedup is 0.00
+ echo ''\''date + '\''%F%T'\'''\'' new events: 08/04/19 09:55 /home/sherwin/tmp/1'
+ read events
+ cd /home/sherwin/
+ rsync -avzP ./tmp/ --delete sherwin@172.16.8.85::backup --password-file=/etc/rsync.password
sending incremental file list
新打开客户端的一个窗口,新建几个目录:
[root@DF-Svr234 sherwin]# cd tmp/
[root@DF-Svr234 tmp]# ll
total 0
-rw-r--r-- 1 root root 0 Apr  4 17:49 a
-rw-r--r-- 1 root root 0 Apr  4 17:49 b
-rw-r--r-- 1 root root 0 Apr  4 17:49 c
-rw-r--r-- 1 root root 0 Apr  4 17:49 d
-rw-r--r-- 1 root root 0 Apr  4 17:49 e
-rw-r--r-- 1 root root 0 Apr  4 17:49 f
[root@DF-Svr234 tmp]# touch {1..4}
[root@DF-Svr234 tmp]# 

服务端:

服务端显示如下:
[root@servicel06 backup]# ll
total 0
-rw-r--r-- 1 sherwin sherwin 0 Apr  8  2019 1
-rw-r--r-- 1 sherwin sherwin 0 Apr  8  2019 2
-rw-r--r-- 1 sherwin sherwin 0 Apr  8  2019 3
-rw-r--r-- 1 sherwin sherwin 0 Apr  8  2019 4
[root@servicel06 backup]# 

三、参考文档

inotify+rsync实现实时同步部署
rsync同步架构
rsync 简介
linux inotify-tools 安装
Rsync 故障排查整理
真正的inotify+rsync实时同步 彻底告别同步慢

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
rsyncinotify可以结合使用实现实时数据同步rsync是一个远程数据同步工具,可以通过LAN/WAN快速同步多台主机间的文件和目录,并利用rsync算法以减少数据的传输。\[2\]而inotify是Linux内核提供的一个监控文件系统事件的机制,可以实时监测文件或目录的变化。通过结合rsyncinotify,可以实现文件的实时同步。 具体实现步骤如下: 1. 首先,需要配置rsync,包括设置同步源和使用rsync命令进行同步。可以通过配置Master主机和Slave主机来实现下行同步。\[1\] 2. 其次,需要调整inotify的内核参数以优化性能,并使用inotify-tools辅助工具来监控文件系统事件。可以编写同步脚本来实现实时同步。\[1\] 3. 最后,验证同步是否成功,可以通过验证Master主机和Slave主机之间的同步情况来确认实时同步是否正常工作。\[1\] 总结来说,通过配置rsync和使用inotify实时监控文件系统事件,可以实现rsyncinotify实时同步,从而实现文件的实时备份和同步。这种方法可以有效减少数据传输量,提高同步效率。 #### 引用[.reference_title] - *1* [rsync数据同步+inotify实时同步](https://blog.csdn.net/qxdbb/article/details/124528049)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [rsync+inotify实现实时同步](https://blog.csdn.net/weixin_34341229/article/details/92459285)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [rsync+inotify文件实时同步](https://blog.csdn.net/qq_40907977/article/details/110454418)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值