加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

三、sersync+rsync实现服务器文件实时同步

发布时间:2020-12-14 00:19:39 所属栏目:Linux 来源:网络整理
导读:一、为什么要用rsync+sersync架构? 1、sersync是基于inotify开发的,类似于inotify-tools的工具 2、sersync可以记录下被监听目录中发生变化的(包括增加、删除、修改)具体某一个文件或者某一个目录的名字,然后使用rsync同步的时候,只同步发生变化的文件

一、为什么要用rsync+sersync架构?

1、sersync是基于inotify开发的,类似于inotify-tools的工具

2、sersync可以记录下被监听目录中发生变化的(包括增加、删除、修改)具体某一个文件或者某一个目录的名字,然后使用rsync同步的时候,只同步发生变化的文件或者目录

二、rsync+inotify-tools与rsync+sersync架构的区别?

1、rsync+inotify-tools

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

b、rsync在同步的时候,并不知道具体是哪个文件或目录发生了变化,每次都是对整个目录进行同步,当数据量很大时,整个目录同步非常耗时(rsync要对整个目录遍历查找对比文件),因此效率很低

?

2、rsync+sersync

a、sersync可以记录被监听目录中发生变化的(增,删,改)具体某个文件或目录的名字;

b、rsync在同步时,只同步发生变化的文件或目录(每次发生变化的数据相对整个同步目录数据来说很小,rsync在遍历查找对比文件时,速度很快),因此效率很高。

?

总结:

当同步的目录数据量不大时,建议使用rsync+inotify

当同步的目录数据量很大时(几百G甚至1T以上)文件很多时,建议使用rsync+sersync

?

?

#rsync服务器上配置

?

1.部署rsync服务

yum install rsync? #安装rsync,如果嫌yum版本过低也可以源码安装

2.vim /etc/rsyncd.conf #默认rsync没有配置文件,创建一个,文件中#和汉字仅为注释,使用中请将所有注释清除

?

#Rsync server
uid = root
gid = root
use chroot = no???????????????????????? # 安全相关
max connections = 2000????????????????? # 并发连接数
timeout = 600?????????????????????????? # 超时时间(秒)
pid file =/var/run/rsyncd.pid?????????? # 指定rsync的pid目录
lock file =/var/run/rsync.lock????????? # 指定rsync的锁文件【重要】
log file = /var/log/rsyncd.log????????? # 指定rsync的日志目录
ignore errors???????????????????????????? #忽略一些I/O错误
read only = false?????????????????????? #设置rsync服务端文件为读写权限
list = false??????????????????????????? #不显示rsync服务端资源列表
hosts allow = 10.1.0.0/16?????????????? #允许进行数据同步的客户端IP地址,可以设置多个,用英文状态下逗号隔开
hosts deny = 0.0.0.0/32???????????????? #禁止数据同步的客户端IP地址,可以设置多个,用英文状态下逗号隔开
auth users = rsync_backup?????????????? #执行数据同步的用户名,可以设置多个,用英文状态下逗号隔开
secrets file =/etc/rsync.password?????? #用户认证配置文件,里面保存用户名称和密码
#################################################
[www]?????????????????????????????????? # 模块
comment = www
path = /data/www/
#################################################
[bbs]
comment = bbs
path = /data/bbs/
#################################################
[blog]
comment = blog
path = /data/blog/
#rsync_config____________end



3、创建用户认证文件

echo? "rsync_backup:123456">/etc/rsync.password? #配置文件,添加以下内容

?

4、设置文件权限

chmod 600 /etc/rsync.password?

5.启动守护进程,并写入开机自启动

rsync --daemon
vim /etc/rc.local
# rsync server progress
/usr/bin/rsync --daemon

?

6.创建相关待同步的目录

mkdir -p /data/{www,bbs,blog}

?

#rsync客户端配置

?

1.安装rsync,方法同上

2.创建rsync配置文件,客户端创建即可,无需内容

touch /etc/rsyncd.conf

3.配置rsync客户端相关权限认证:

?echo "123456">/etc/rsync.password
?chmod 600 /etc/rsync.password

4.创建待同步数据,在客户端创建一些数据

mkdir -p /data/{www,blog}
touch /data/www/www.log /data/bbs/bbs.log? /data/blog/blog.log

5.测试rsync是否同步

rsync? -avzP /data/www/ [email?protected]::www? --password-file=/etc/rsync.password
#rsync? -avzP /backup/ rsync://[email?protected]/backup/?? --password-file=/etc/rsync.password 两种方法
#rsync? -avzP /backup/ rsync://[email?protected]/backup/test/?? --password-file=/etc/rsync.password #test为服务器上的目录
参数: --delete 无差异同步
????????? --bwlimit=KB/S? 限速
?????????? --exclude=PATTERN?????? exclude files matching PATTERN
?????????? --exclude-from=FILE???? read exclude patterns from FILE
?????????? --include=PATTERN?????? don’t exclude files matching PATTERN
?????????? --include-from=FILE???? read include patterns from FILE

#此步骤必须成功才能进行下一步

?

6.开始部署sersync服务

tar fxz sersync2.5.4_64bit_binary_stable_final.tar.gz -C /usr/local/
cd /usr/local/
mv GNU-Linux-x86 sersync

7.配置sersync

?

cp sersync/confxml.xml? sersync/confxml.xml-bak
vim? sersync/confxml.xml

修改24--28行
?24???????? <localpath watch="/opt/tongbu">?? ##监控的目录
?25???????????? <remote ip="127.0.0.1" name="tongbu1"/> ##备份服务器的ip和模块
?26???????????? <!--<remote ip="192.168.8.39" name="tongbu"/>-->
?27???????????? <!--<remote ip="192.168.8.40" name="tongbu"/>-->
?28???????? </localpath>
修改后的内容为:
24???????? <localpath watch="/data/www">
25???????????? <remote ip="10.1.20.109" name="www"/>
26???????? </localpath>

修改29--35行,认证部分(rsync密码认证)
29???????? <rsync>
30???????????? <commonParams params="-artuz"/>?? ##rsync命令的参数
31???????????? <auth start="false" users="root" passwordfile="/etc/rsync.pas"/> ##认证用户和密码文件的位置
32???????????? <userDefinedPort start="false" port="874"/><!-- port=874 -->
33???????????? <timeout start="false" time="100"/><!-- timeout=100 -->
34???????????? <ssh start="false"/>
35???????? </rsync>

修改后的内容如下:
27???????? <rsync>
28???????????? <commonParams params="-artuz"/>
29???? ????????<auth start="true" users="rsync_backup" passwordfile="/etc/rsync.password"/>
30???????????? <userDefinedPort start="false" port="874"/><!-- port=874 -->
31???????????? <timeout start="true" time="100"/><!-- timeout=100 -->
32???????????? <ssh start="false"/>
33???????? </rsync>

?

?

?

8.开启sersync守护进程同步数据

?

/usr/local/sersync/bin/sersync? -d -r -o /usr/local/sersync/conf/confxml.xml
配置sersync环境变量
echo"PATH=$PATH:/usr/local/sersync/">>/etc/profile
source /etc/profil

启动命令后返回结果如下为正常:**

set the system param
execute:echo 50000000 > /proc/sys/fs/inotify/max_user_watches
execute:echo 327679 > /proc/sys/fs/inotify/max_queued_events
parse the command param
option: -d???? run as a daemon
option: -r???? rsync all the local files to the remote servers before the sersync work
option: -o???? config xml name:? ./confxml.xml
daemon thread num: 10
parse xml config file
XML Parsing error inside file ‘./confxml.xml‘.
Error: File not found
At line 0,column 0.

?

同步测试

[[email?protected] GNU-Linux-x86]# ./sersync2 -d -r -o ./confxml-www.xml

set the system param
execute:echo 50000000 > /proc/sys/fs/inotify/max_user_watches
execute:echo 327679 > /proc/sys/fs/inotify/max_queued_events
parse the command param
option: -d???? run as a daemon
option: -r???? rsync all the local files to the remote servers before the sersync work
option: -o???? config xml name:? ./confxml-www.xml
daemon thread num: 10
parse xml config file
host ip : localhost??? host port: 8008
daemon start,sersync run behind the console
use rsync password-file :
user is??? rsync_backup
passwordfile is???? /etc/rsync.password
config xml parse success
please set /etc/rsyncd.conf max connections=0 Manually
sersync working thread 12? = 1(primary thread) + 1(fail retry thread) + 10(daemon sub threads)
Max threads numbers is: 22 = 12(Thread pool nums) + 10(Sub threads)
please according your cpu ,use -n param to adjust the cpu rate
------------------------------------------
rsync the directory recursivly to the remote servers once
working please wait...
execute command: cd /data/www && rsync -artuz -R --delete ./? --timeout=100 [email?protected]::www --password-file=/etc/rsync.password >/dev/null 2>&1
run the sersync:
watch path is: /data/www

?

?

9.多实例情况

?

?

?? 1、配置多个confxml.xml文件(比如:www、bbs、blog....等等)
??? confxml-bbs.xml? confxml-blog.xml? confxml-www.xml(按照单个实例配置即可)
??? 2、根据不同的需求同步对应的实例文件
??? rsync -avzP /data/www/ [email?protected]::www/?? --password-file=/etc/rsync.password
??? rsync -avzP /data/bbs/ [email?protected]::bbs/?? --password-file=/etc/rsync.password
??? rsync -avzP /data/test/ [email?protected]::blog/?? --password-file=/etc/rsync.passwor
分别启动即可

?

rsync缺点:

1.大量小文件同步时,比对时间长,有时候rsync进程会停止

2.同步大文件,10G这样的大文件有时也会有问题,会中断。未完整同步之前是隐藏文件,可通过参数续传

?

sersync的参数信息

参数-d:启用守护进程模式 参数-r:在监控前,将监控目录与远程主机用rsync命令推送一遍 c参数-n: 指定开启守护线程的数量,默认为10个 参数-o:指定配置文件,默认使用confxml.xml文件 参数-m:单独启用其他模块,使用 -m refreshCDN 开启刷新CDN模块 参数-m:单独启用其他模块,使用 -m socket 开启socket模块 参数-m:单独启用其他模块,使用 -m http 开启http模块 不加-m参数,则默认执行同步程序

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读