CentOS 7 Sersync+Rsync 实现数据文件实时同步

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

1、rsync+inotify-tools

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

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

    

2、rsync+sersync

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

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

同步过程:

1.  在同步服务器上开启sersync服务,sersync负责监控配置路径中的文件系统事件变化;

2.  调用rsync命令把更新的文件同步到目标服务器;

3.  需要在主服务器配置sersync,在同步目标服务器配置rsync server

同步原理:

1.  用户实时的往sersync服务器上写入更新文件数据;

2.  此时需要在同步主服务器上配置sersync服务;

3.  在另一台服务器开启rsync守护进程服务,以同步拉取来自sersync服务器上的数据;

通过rsync的守护进程服务后可以发现,实际上sersync就是监控本地的数据写入或更新事件;然后,在调用rsync客户端的命令,将写入或更新事件对应的文件通过rsync推送到目标服务器

CentOS 7 192.168.94.33(Sersync server)

CentOS 7 192.168.94.44(Rsync server)

先安装Rsync

[root@Rsync ~]# yum -y install rsync
[root@Rsync ~]# vim /etc/rsyncd.conf 
#Rsync server
uid = root                         #运行进程的身份
gid = root                         #运行进程的组
use chroot = yes                   #是否锁定家目录
max connections = 100           #最大连接数
timeout = 600                    #超时时间
log file = /var/log/rsyncd.log     #日志文件
ignore errors                     #忽略错误
read only = false                  #设置服务端文件读写权限
list = false                        #不显示服务端资源列表
hosts allow = 192.168.94.0/24    #*代表所有
hosts deny = 0.0.0.0/32
auth users = backup
secrets file = /etc/rsync.password

[www]
comment = www 
path = /var/www/html

创建备份目录和密码文件

[root@Rsync ~]# mkdir -p /var/www/html
[root@Rsync ~]# ls /var/www/html/

[root@Rsync ~]# echo "backup:damowang" > /etc/rsync.password
[root@Rsync ~]# chmod 600 /etc/rsync.password

在数据源端创建密码文件 , 然后在rsync命令中使用rsync --password-file 指定该文件

[root@Sersync ~]# echo "damowang" > /etc/rsync.password
[root@Sersync ~]# chmod 600 /etc/rsync.password

测试

[root@Sersync ~]# cp /etc/passwd .
[root@Sersync ~]# rsync -azvp /root/passwd backup@192.168.94.44::www/ --password-file=/etc/rsync.password 
sending incremental file list
passwd

sent 641 bytes  received 43 bytes  1,368.00 bytes/sec
total size is 1,337  speedup is 1.95

[root@Rsync ~]# ls /var/www/html/
passwd

测试这步一定要成功,不然进行不了下一步

部署Sersync服务

下载sersync :

goodle code地址 : https://code.google.com/archive/p/sersync/downloads

Git Hub 镜像地址 : https://github.com/orangle/sersync

[root@Sersync ~]# unzip sersync-master.zip 
[root@Sersync ~]# tar xf sersync-master/release/sersync2.5.4_64bit_binary_stable_final.tar.gz -C /usr/local/
[root@Sersync ~]# cd /usr/local/
[root@Sersync local]# mv GNU-Linux-x86/ sersync
[root@Sersync local]# cd sersync/
[root@Sersync sersync]# cp confxml.xml confxml.xml.$(date +%F)
[root@Sersync sersync]# vim confxml.xml
# 修改24、25行
    <sersync>
        <localpath watch="/var/www/html">    # 本地同步目录
        <remote ip="192.168.94.44" name="www"/>     # rsync模块名

# 修改31行
<rsync>
        <commonParams params="-artuz"/>
        <auth start="true" users="backup" passwordfile="/etc/rsync.password"/> 

开启sersync守护进程 同步数据

[root@Sersync sersync]# sersync2 -d -r -o /usr/local/sersync/confxml.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:  /usr/local/sersync/confxml.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    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 /var/www/html && rsync -artuz -R --delete ./ backup@192.168.94.44::www --password-file=/etc/rsync.password >/dev/null 2>&1 
run the sersync: 
watch path is: /var/www/html

测试

添加数据

 [root@Sersync sersync]# cd /var/www/html
 [root@Sersync html]# ls
 [root@Sersync html]# mkdir damowang.cn
 [root@Sersync html]# cd damowang.cn/
 [root@Sersync damowang.cn]# touch {1..5}.txt
 [root@Sersync damowang.cn]# ll
 总用量 0
 -rw-r--r-- 1 root root 0 8月 28 01:08 1.txt
 -rw-r--r-- 1 root root 0 8月 28 01:08 2.txt
 -rw-r--r-- 1 root root 0 8月 28 01:08 3.txt
 -rw-r--r-- 1 root root 0 8月 28 01:08 4.txt
 -rw-r--r-- 1 root root 0 8月 28 01:08 5.txt

验证

[root@Rsync ~]# ll /var/www/html/
总用量 0
drwxr-xr-x 2 root root 71 8月  28 01:08 damowang.cn
[root@Rsync ~]# ll /var/www/html/damowang.cn/
总用量 0
-rw-r--r-- 1 root root 0 8月  28 01:08 1.txt
-rw-r--r-- 1 root root 0 8月  28 01:08 2.txt
-rw-r--r-- 1 root root 0 8月  28 01:08 3.txt
-rw-r--r-- 1 root root 0 8月  28 01:08 4.txt
-rw-r--r-- 1 root root 0 8月  28 01:08 5.txt

猜你喜欢

转载自www.cnblogs.com/bigdevilking/p/9545715.html