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

rsync+inotify-tools与rsync+sersync架构的区别
1,rsync+inotify-tools
只能记录下被监听的目录发生的变化(增删改)并没有把具体变化的文件或目录记录下来
在同步时,并不知道具体哪个文件或目录发送变化而是每次都对整个目录树来进行便利查找变更文件
然触发全部数据进行同步,当数据量很大时,整个目录同步非常耗时,因此效率很低
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 172.20.10.2(sersync server)
Centos 7 172.20.10.6 (rsync server

关闭防火墙

关闭selinux

iptable -F

setenforce 0

systemctl stop firewalld

systemctl distable firewalld

目标服务器172.20.10.6安装rsync实现同步数据
[root@cheng ~]# yum -y install rsync httpd
[root@cheng ~]# vim /etc/rsyncd.conf

uid = root
gid = root
use chroot = yes
max connections = 100
timeout = 600
ignore errors
list = false
address = 172.20.10.6
port = 873
log file = /var/log/rsyncd.log
pid file = /var/run/rsyncd.pid
hosts allow = 172.20.10.0/24
[wwwroot]
path = /var/www/html
comment = Document Root os www.zc.com
read only = no
dont compress = *.gz *.bz2 *.tgz *.zip *.rar *.z
auth users = backuper
secrets file = /etc/rsyncd_users.db
基于安全性考虑,对于rsync的备份源最好仅允许以只读的方式做下同步,另外,同步可以采用匿名的方式,只要将其中的
auth users和secrets file 配置记录去掉就可以了
2,为备份账号创建数据文件,以冒号分割,密码信息在文件中以明文方式存放,为避免信息泄露,需要调整权限
[root@cheng ~]# vim /etc/rsyncd_users.db
backuper:pwd123
[root@cheng ~]# chmod 600 /etc/rsyncd_users.db
备份用户backuper 也需要对/var/www/html 有相应的读取权限,实际上只要other组具有读取权限,
则备份用户backuper和运行nobody用户也就有读取权限了
[root@cheng ~]# ls -ls /var/www/html
3,启动服务,以独立监听服务的方式运行 ,若关闭rsync服务科采取kill进程方式
[root@cheng ~]# rsync --daemon
[root@cheng ~]# netatst -anpt |grep rsync
回显内容:tcp 0 0 172.20.10.6 :873 0.0.0.0:* LISTEN 3650/rsync

在数据源创建密码文件,172.20.10.2,然后在rsync命令中使用rsync --password-file 指定该文件
[root@zhao ~]# echo "pwd123" > /etc/server.pass
[root@zhao ~]# chmod 600 /etc/server.pass
[root@zhao ~]# rsync -az --delete --password-file=/etc/serer.pass /etc/hosts backuper@172.20.10.6::wwwroot

[root@cheng ~]#在另一机器可以看到
cat /var/www/html
有个hosts
测试这一步一定要成功,不然进行不了下一步
部署sersync服务 172.20.10.2部署

[root@zhao ~]# tar xf sersync2.5.4_64bit_binary_stable_final.tar.gz -C /usr/local
[root@zhao ~]# cd /usr/local
[root@zhao local]# ls
bin boost etc games GNU-Linux-x86 include lib lib64 libexec mysql nginx php5 sbin share src
[root@zhao local]# mv GNU-Linux-x86/ sersync
[root@zhao local]# cd sersync/
[root@zhao sersync]# cp confxml.xml confxml.xml.$(date +%F)
[root@zhao sersync]# vim confxml.xml
修改31行内容 <auth start="true" users="backuper" passwordfile="/etc/server.pass"/>


修改24行 <localpath watch="/var/www/html"
修改25行 <remote ip="172.20.10.6" name="wwwroot"/>

开启server 守护进程,同步数据
[root@zhao sersync]# yum -y install httpd
[root@zhao sersync]# ./sersync2 -d -r -o /usr/local/sersync/confxml.xml set the system param
use rsync password-file :
user is backuper
passwordfile is /etc/server.pass
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 ./ backuper@172.20.10.6::wwwroot --password-file=/etc/server.pass >/dev/null 2>&1

测试同步、添加数据
[root@zhao sersync]# cd /var/www/html
[root@zhao html]# ls
[root@zhao html]# mkdir zc
[root@zhao html]# cd zc
[root@zhao zc]# touch {1..5}.txt
[root@zhao zc]# ls -l
总用量 0
-rw-r--r--. 1 root root 0 3月 7 18:54 1.txt
-rw-r--r--. 1 root root 0 3月 7 18:54 2.txt
-rw-r--r--. 1 root root 0 3月 7 18:54 3.txt
-rw-r--r--. 1 root root 0 3月 7 18:54 4.txt
-rw-r--r--. 1 root root 0 3月 7 18:54 5.txt

[root@cheng ~]# ls /var/www/html
zc
[root@cheng ~]# ls -l /var/www/html/zc
总用量 0
-rw-r--r--. 1 root root 0 3月 7 18:54 1.txt
-rw-r--r--. 1 root root 0 3月 7 18:54 2.txt
-rw-r--r--. 1 root root 0 3月 7 18:54 3.txt
-rw-r--r--. 1 root root 0 3月 7 18:54 4.txt
-rw-r--r--. 1 root root 0 3月 7 18:54 5.txt

原文地址:https://www.cnblogs.com/zc1741845455/p/10491674.html