Linux rsync - inotify - sersync

目录

rsync介绍

rsync是什么?

rsync的优点和缺点

rsync的工作过程

rsync认证方式

inotify介绍

Inotify是什么?

查看内核是否支持inotify

Sersync介绍

什么是Sersync?

sersync 优点

Rsync+inotify(文件实时同步)

环境准备

两台Linux服务器(centos7.9)

实验步骤

1.关闭防火墙和seLinux(临时关闭)

2.安装rsync、xinetd软件

3.新建目录和文件

rsync-client上操作

1.编辑xinetd配置文件

2.编辑rsync配置文件

3.编辑账号认证文件,并给予600权限

4.启动xinetd服务

5.rsync-server上测试

rsync-server上操作

1.编辑xinetd配置文件

2.编辑rsync配置文件

3.编辑账号认证文件,并给予600权限

4.启动xinetd服务

5.rsync-client上测试

整体测试

配置 inotify 实时同步

1.安装inotify-tools

2.创建密码文件,设置600权限

3.编辑脚本,在后台运行

4.测试

rsync+sersync(文件实时同步)

安装sersync工具,实时触发rsync进行同步

1、安装sersync

2、修改inotify参数

3、备份配置文件

4、修改配置 data_configxml.xml 文件

5、修改环境变量

6、启动sersync并且设置sersync开机启动


rsync介绍

rsync是什么?

rsync(remote synchronize,远程同步):是linux系统下的数据镜像备份工具。可以远程同步,支持本地复制,或者与其他SSH、rsync主机同步。
rsync 可通过LAN/WAN快速同步多台主机间的文件和目录,并适当利用rsync算法(差分编码)以减少数据的传输。

rsync 监听端口:873

rsync 运行模式:C/S架构

rsync 软件的服务器端和客户端是同一个软件包

rsync的优点和缺点

1、支持跨平台,可以在Windows与Linux间进行数据同步。
2、可以镜像保存整个目录树和文件系统。
3、可以很容易做到保持原来文件的权限、时间、软硬链接等。
4、rysnc在第一次同步时会复制全部内容,但在下一次只传输修改过的文件。
5、rysnc在传输的过程中可以实行压缩及解压缩操作,可以使用更少的带宽
6、rsync不仅可以远程同步数据,还可以本地同步数据(做差异同步)
7、无需特殊权限即可安装。
8、支持匿名传输,以方便进行网站镜像。

1、rsync不适合频繁改动的场景,例如数据库文件,如果源路径频繁改动,rsync将时刻处于高频计算中。
2、不适合同步大文件,远程同步到主机时,远端主机的rsync需要把之前文件的内容与该改动的内容拼成一个新文件。当文件过大时,很消耗远程主机的cpu资源。

rsync的工作过程

1、核实用户
2、检测出需要传输的文件后(不是将文件全部传输到远端,而是只传文件改动的部分),远端再依据之前的内容与传过来的新内容按照某个算法拼成一个新文件。
3、进行传输

rsync认证方式

rsync-daemon方式

ssh方式

inotify介绍

Inotify是什么?

Inotify 是一个 Linux特性,它监控文件系统操作,比如读取、写入和创建。

Inotify 反应灵敏,用法非常简单,并且比 cron 任务的繁忙轮询高效得多。

Inotify 只能记录下被监听的目录发生了变化(增,删,改)并没有把具体是哪个文件或者哪个目录发生了变化记录下来

查看内核是否支持inotify

[root@rsync-client ~]#  ls /proc/sys/fs/inotify/
max_queued_events  max_user_instances  max_user_watches

[root@rsync-client ~]# cat max_queued_events 
16384
[root@rsync-client ~]# cat max_user_instances 
128
[root@rsync-client ~]# cat max_user_watches 
8192

max_queued_events   # inotify事件队列最大长度(默认16384),如果值太小会出现 Event Queue Overflow 错误。
max_user_watches    # 可以监视的文件数量(默认8192)
max_user_instances  # 每个用户创建inotify实例最大值(默认128)

Sersync介绍

什么是Sersync?

Sersync可以记录下被监听目录中发生变化的(包括增加、删除、修改)具体某一个文件或者某一个目录的名字,使用rsync同步的时候,只同步发生变化的文件或者目录,因此效率更高。

sersync 是使用c++编写,可以对linux文件系统产生的临时文件和重复的文件操作进行过滤,

sersync 优点

sersync 结合rsync同步,可以节省Linux系统运行时耗和网络资源。
sersync配置简单,提供了静态编译好的二进制文件和xml配置文件(可直接使用)。
sersync使用多线程进行同步,尤其在同步较大文件时,能够保证多个服务器实时保持同步状态。
sersync有出错处理机制,通过失败队列对出错的文件重新同步,如果仍旧失败,则按设定时长对同步失败的文件重新同步。
sersync不仅可以实现实时同步,另外还自带crontab功能,只需在xml配置文件中开启,即也可以按要求隔一段时间整体同步一次,而无需再额外配置crontab功能。


Rsync+inotify(文件实时同步)

环境准备

两台Linux服务器(centos7.9)

rsync-server192.168.0.12
rsync-client192.168.0.11

实验步骤

1.关闭防火墙和seLinux(临时关闭)

[root@rsync-client ~]# systemctl stop firewalld
[root@rsync-client ~]# setenforce 0

[root@rsync-server ~]# systemctl stop firewalld
[root@rsync-server ~]# setenforce 0

2.安装rsync、xinetd软件

[root@rsync-server ~]# yum -y install rsync xinetd

[root@rsync-client ~]# yum -y install rsync xinetd

3.新建目录和文件

[root@rsync-client ~]# mkdir /test
[root@rsync-client /]# cd test
[root@rsync-client test]# echo "hello world" >a.txt
[root@rsync-client test]# touch b.txt
[root@rsync-client test]# vim c.txt
[root@rsync-client test]# cat c.txt 
welcome to changsha
[root@rsync-client test]# ls
a.txt  b.txt  c.txt

[root@rsync-client test]# mkdir out
[root@rsync-client test]# ls
a.txt  b.txt  c.txt  out
[root@rsync-client test]# cd out
[root@rsync-client out]# ls
[root@rsync-client out]# echo 'xixi' >aa.txt
[root@rsync-client out]# touch bb.txt
[root@rsync-client out]# ls
aa.txt  bb.txt
[root@rsync-server ~]# mkdir /r_test
[root@rsync-server ~]# cd /r_test
[root@rsync-server r_test]# echo 'haha' >1.txt
[root@rsync-server r_test]# touch 2.txt
[root@rsync-server r_test]# vim 3.txt
[root@rsync-server r_test]# cat 3.txt 
123
[root@rsync-server r_test]# ls
1.txt  2.txt  3.txt

rsync-client上操作

1.编辑xinetd配置文件
[root@rsync-client xinetd.d]# cat /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
        flags           = IPv6
        socket_type     = stream
        wait            = no
        user            = root
        server          = /usr/bin/rsync
        server_args     = --daemon
        log_on_failure  += USERID
}
2.编辑rsync配置文件
[root@rsync-client xinetd.d]# cat /etc/rsyncd.conf 
# /etc/rsyncd: configuration file for rsync daemon mode

# See rsyncd.conf man page for more options.

# configuration example:

# uid = nobody
# gid = nobody
# use chroot = yes
# max connections = 4
# pid file = /var/run/rsyncd.pid
# exclude = lost+found/
# transfer logging = yes
# timeout = 900
# ignore nonreadable = yes
# dont compress   = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2

# [ftp]
#        path = /home/ftp
#        comment = ftp export area
[test]
    path = /test
    auth users = com_user
    secrets file = /etc/rsyncd.secrets
    read only = false

[out]
    path = /test/out/
    auth users = out_user
    secrets file = /etc/rsyncd.secrets
    read only = false
3.编辑账号认证文件,并给予600权限
[root@rsync-client xinetd.d]# cat /etc/rsyncd.secrets 
com_user:1234
out_user:123123

chmod 600 /etc/rsyncd.secrets
4.启动xinetd服务
[root@rsync-client xinetd.d]# service xinetd start
Redirecting to /bin/systemctl start xinetd.service

[root@rsync-client xinetd.d]# ps aux|grep xinetd
root       1850  0.0  0.0  27168  1072 ?        Ss   19:59   0:00 /usr/sbin/xinetd -stayalive -pidfile /var/run/xinetd.pid
root       1854  0.0  0.0 112824   980 pts/0    S+   19:59   0:00 grep --color=auto xinetd

5.rsync-server上测试

[root@rsync-server r_test]# rsync -a 192.168.0.11::
test           	
out  

rsync-server上操作

1.编辑xinetd配置文件
[root@rsync-server ~]# cat /etc/rsyncd.conf 
# /etc/rsyncd: configuration file for rsync daemon mode

# See rsyncd.conf man page for more options.

# configuration example:

# uid = nobody
# gid = nobody
# use chroot = yes
# max connections = 4
# pid file = /var/run/rsyncd.pid
# exclude = lost+found/
# transfer logging = yes
# timeout = 900
# ignore nonreadable = yes
# dont compress   = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2

# [ftp]
#        path = /home/ftp
#        comment = ftp export area
uid = root
gid = root
[wtest]
    path = /r_test
    auth users = w_user
    secrets file = /etc/rsyncd.secrets
    read only = false
2.编辑rsync配置文件
[root@rsync-server ~]# cat /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
        flags           = IPv6
        socket_type     = stream
        wait            = no
        user            = root
        server          = /usr/bin/rsync
        server_args     = --daemon
        log_on_failure  += USERID
}
3.编辑账号认证文件,并给予600权限
[root@rsync-server ~]# cat /etc/rsyncd.secrets 
r_user:q234

[root@rsync-server ~]# chmod 600 /etc/rsyncd.secrets
4.启动xinetd服务
[root@rsync-server ~]# service xinetd start
Redirecting to /bin/systemctl start xinetd.service

[root@rsync-server ~]# ps aux|grep xinetd
root       2264  0.0  0.0  25044   592 ?        Ss   20:11   0:00 /usr/sbin/xinetd -stayalive -pidfile /var/run/xinetd.pid
root       2266  0.0  0.0 112824   980 pts/0    S+   20:11   0:00 grep --color=auto xinetd
5.rsync-client上测试
[root@rsync-client ~]# rsync -a 192.168.0.12::
wtest   

整体测试

[root@rsync-server log]# rsync -rltDvz  /r_test/  out_user@192.168.0.11::out
Password: 
sending incremental file list
rsync: failed to set times on "/." (in out): Operation not permitted (1)
./
1.txt
2.txt
3.txt
rsync: mkstemp "/.1.txt.61Zi9u" (in out) failed: Permission denied (13)
rsync: mkstemp "/.2.txt.vRmMdp" (in out) failed: Permission denied (13)
rsync: mkstemp "/.3.txt.UAQfij" (in out) failed: Permission denied (13)

sent 245 bytes  received 381 bytes  21.96 bytes/sec
total size is 9  speedup is 0.01
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1179) [sender=3.1.2]
[root@rsync-server log]# 

[root@rsync-client ~]# cd /test/out
[root@rsync-client out]# ls
1.txt  2.txt  3.txt  aa.txt  bb.txt


[root@rsync-client out]# rsync -avt /test/ w_user@192.168.0.12::wtest
Password: 
sending incremental file list
./
a.txt
b.txt
c.txt
out/
out/1.txt
out/2.txt
out/3.txt
out/aa.txt
out/bb.txt

sent 659 bytes  received 183 bytes  80.19 bytes/sec
total size is 46  speedup is 0.05

[root@rsync-server ~]# cd /r_test/
[root@rsync-server r_test]# ls
1.txt  2.txt  3.txt  a.txt  b.txt  c.txt  out

配置 inotify 实时同步

1.安装inotify-tools

wget http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz

tar xf inotify-tools-3.14.tar.gz
cd inotify-tools-3.14
./configure
make
make install
cp /usr/local/bin/inotifywa* /usr/sbin/

2.创建密码文件,设置600权限

[root@rsync-server ~]# mkdir -p /home/work/ftppass
[root@rsync-server ~]# vim /home/work/ftppass/passwd
123456
[root@rsync-server ~]# chmod 600 /home/work/ftppass/passwd

# 测试
rsync -avt --password-file=/home/work/ftppass/passwd /r_test out_user@192.168.0.11::out


[root@rsync-client ~]# mkdir -p /home/work/ftppass
[root@rsync-client ~]# vim /home/work/ftppass/passwd
qwezivj12

[root@rsync-client ~]# chmod 600 /home/work/ftppass/passwd

# 测试
rsync -avt --password-file=/home/work/ftppass/passwd /test/out w_user@192.168.0.12::wtest

3.编辑脚本,在后台运行

[root@rsync-server ~]# vim wtest_out.sh
#!/bin/bash

# 备份
inotifywait -mrq -e modify,create,move,delete,attrib /r_test |while read events
    do
        rsync -avt --password-file=/home/work/ftppass/passwd /r_test out_user@192.168.0.11::out
        echo "$(date "+%Y%m%d%H%M%S) $events" >> /var/log/sync.log
    done
[root@rsync-server ~]# nohup bash wtest_out.sh &



[root@rsync-client ~]# vim out_wtest.sh 
#!/bin/bash

# 备份文件/test/out
inotifywait -mrq -e modify,create,move,delete,attrib /test/out |while read events
    do
        rsync -avt --password-file=/home/work/ftppass/passwd /test/out w_user@192.168.0.12::wtest
        echo "[`date "+%Y-%m-%d %H:%M:%S"`]  $events" >> /var/log/sync.log
    done
[root@rsync-client ~]# nohup bash out_wtest.sh & 


#还可用通过创建计划任务定时定点执行脚本(例如每天2点半执行脚本)
crontab -e
30 2 * * *  bash wtest_out.sh
30 2 * * *  bash out_wtest.sh

4.测试

[root@rsync-client ~]#  touch /test/out/inotify{1..10}


[root@rsync-client ~]#  ls /test/out
1.txt  3.txt   bb.txt    inotify10  inotify3  inotify5  inotify7  inotify9
2.txt  aa.txt  inotify1  inotify2   inotify4  inotify6  inotify8


[root@rsync-server ~]# ls /r_test
1.txt  2.txt  3.txt  a.txt  b.txt  c.txt  out

[root@rsync-server ~]# ls /r_test/out
1.txt  3.txt   bb.txt    inotify10  inotify3  inotify5  inotify7  inotify9
2.txt  aa.txt  inotify1  inotify2   inotify4  inotify6  inotify8

rsync+sersync(文件实时同步)

安装sersync工具,实时触发rsync进行同步

1、安装sersync

wget http://down.whsir.com/downloads/sersync2.5.4_64bit_binary_stable_final.tar.gz

2、修改inotify参数

[root@rsync-client ~]#  vim /etc/sysctl.conf  
fs.inotify.max_queued_events=99999999
fs.inotify.max_user_watches=99999999
fs.inotify.max_user_instances=65535

[root@rsync-client ~]# /usr/sbin/sysctl –p

3、备份配置文件

[root@rsync-client ~]#  cp confxml.xml confxml.xml.bak
[root@rsync-client ~]#  cp confxml.xml data_configxml.xml
[root@rsync-client ~]#  ls
confxml.xml  confxml.xml.bak  data_configxml.xml  sersync2

4、修改配置 data_configxml.xml 文件

[root@rsync-server ~]# vim data_configxml.xml
# 第24行后的配置
       
       <localpath watch="/test">            		 	# 本地数据源路径             
		<remote ip="192.168.0.12" name="wtest"/>        # 备份服务器地址信息
       </localpath>
          <rsync>
             <commonParams params="-artuz"/>
             <auth start="true" users="w_uesr" passwordfile="/home/work/ftppass/passwd"/>        # 启用身份验证,密码文件路径"/home/work/ftppass/passwd"
             <userDefinedPort start="false" port="874"/><!-- port=874 -->
             <timeout start="false" time="100"/><!-- timeout=100 -->
           <ssh start="false"/>
    	  </rsync>

5、修改环境变量

PATH=/usr/local/sersync/:$PATH

echo 'PATH=/usr/local/sersync/:$PATH'  >>/root/.bashrc

6、启动sersync并且设置sersync开机启动

[root@rsync-server ~]# sersync2 -d -r -o  /usr/local/sersync/data_configxml.xml
 
[root@rsync-server ~]# vim /etc/rc.local 
/usr/local/sersync/sersync2 -d -r -o  /usr/local/sersync/data_configxml.xml

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

韩未零

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值