Sersync实时同步

本文详细介绍了如何使用Sersync实现实时文件同步,包括NFS节点的搭建、web服务器的配置、Sersync工具的安装与配置,以及在NFS故障时的切换策略。通过Sersync结合inotify,可以实现目录变化即时同步,并通过监控确保数据一致性。同时,文章还展示了如何处理上传文件时的权限问题,确保服务的稳定运行。
摘要由CSDN通过智能技术生成

Sersync实时同步

1.实时同步概述
2.实时同步案例
3.实时同步扩展

本章课程大纲

1.什么是实时同步?
2.实时同步的原理
3.实时同步的场景
4.实时同步的工具的选择
5.实时同步的案例演示

1.实时同步的概述

1.什么是实时同步
只要当前目录发生变化则会出发一个事件,事件触发后将变化的目录同步至远程服务器。
2.实时同步的原理
实时同步需要借助inotify通知接口,用来监控本地目录的变化,如果监控本地的目录发生变化,则会触发动作,这个动作可以是进行同步操作,或其他操作。
3.实时同步场景
场景1、解决NFS单点故障、保证同步的数据连续性
场景2、本地无缝迁移云端
4.实时同步工具的选择
sersync(√)、inotify+rsync,通常我们会选择sersync,因为sersync是国人基于rsync+inotify基础之上开发的工具,不仅保留了其优点同时还强化了实时监控,文件过滤,简化配置等功能,帮助用户提高运行效率,sersync项目地址

2.实时同步案例

案例:实现web上传视频文件,实则是写入NFS至存储,当NFS存在新的数据则会实时复制到备份服务器
在这里插入图片描述
5.搭建web节点(使用了解)10.0.0.7服务器 对外提供80访问的端口
(1)安装httpd服务
[root@web ~]# yum -y install httpd php
(2)启动
[root@web ~]# systemctl start httpd
(3)页面/var/www/html的这个目录是空的则页面显示这样
[root@web ~]# ll /var/www/html/
total 0
在这里插入图片描述
(4)切换到/var/www/html目录上传代码到/var/www/html目录上
在这里插入图片描述
[root@web ~]# cd /var/www/html/
[root@web html]# rz 上传到目录中
[root@web html]# ls
kaoshi-download.zip
[root@web html]# unzip kaoshi-download.zip
Archive: kaoshi-download.zip
inflating: upload_file.php
inflating: info.php
inflating: bg.jpg
inflating: index.html
[root@web html]# ls
bg.jpg index.html info.php kaoshi-download.zip upload_file.php
[root@web html]# systemctl restart httpd 上传代码解压并重启一下服务
在这里插入图片描述
(5)显示上传成功但是在目录/var/www/html目录查看没有上传的文件
[root@web html]# ll
total 80
-rw-r–r–. 1 root root 38772 Apr 27 2018 bg.jpg
-rw-r–r–. 1 root root 2633 May 4 2018 index.html
-rw-r–r–. 1 root root 52 May 10 2018 info.php
-rw-r–r–. 1 root root 26921 Oct 20 2020 kaoshi-download.zip
-rw-r–r–. 1 root root 1114 Oct 20 2020 upload_file.php
查看日志定位问题
[root@web html]# tail -f /var/log/httpd/error_log
[Wed Apr 13 00:00:27.823706 2022] [core:notice] [pid 1803] SELinux policy enabled; httpd running as context system_u:system_r:httpd_t:s0
[Wed Apr 13 00:00:27.825342 2022] [suexec:notice] [pid 1803] AH01232: suEXEC mechanism enabled (wrapper: /usr/sbin/suexec)
AH00558: httpd: Could not reliably determine the server’s fully qualified domain name, using fe80::20c:29ff:feeb:f041. Set the ‘ServerName’ directive globally to suppress this message
[Wed Apr 13 00:00:28.041570 2022] [lbmethod_heartbeat:notice] [pid 1803] AH02282: No slotmem from mod_heartmonitor
[Wed Apr 13 00:00:28.049176 2022] [mpm_prefork:notice] [pid 1803] AH00163: Apache/2.4.6 (CentOS) PHP/5.4.16 configured – resuming normal operations
[Wed Apr 13 00:00:28.049224 2022] [core:notice] [pid 1803] AH00094: Command line: ‘/usr/sbin/httpd -D FOREGROUND’
[Wed Apr 13 00:03:42.572387 2022] [:error] [pid 1805] [client 10.0.0.1:63046] PHP Notice: Use of undefined constant download - assumed ‘download’ in /var/www/html/upload_file.php on line 8, referer: http://10.0.0.7/
[Wed Apr 13 00:03:42.572530 2022] [:error] [pid 1805] [client 10.0.0.1:63046] PHP Warning: mkdir(): Permission denied in /var/www/html/upload_file.php on line 15, referer: http://10.0.0.7/
[Wed Apr 13 00:03:42.572574 2022] [:error] [pid 1805] [client 10.0.0.1:63046] PHP Warning: move_uploaded_file(download/1_zhx.txt.txt): failed to open stream: No such file or directory in /var/www/html/upload_file.php on line 45, referer: http://10.0.0.7/
[Wed Apr 13 00:03:42.572582 2022] [:error] [pid 1805] [client 10.0.0.1:63046] PHP Warning: move_uploaded_file(): Unable to move ‘/tmp/phpea8zIh’ to ‘download/1_zhx.txt.txt’ in /var/www/html/upload_file.php on line 45, referer: http://10.0.0.7/
(6)查看服务的进程是啥用户在查看目录是什么用户在运行权限
在这里插入图片描述
(7)修改httpd服务进程配置用户身份为www用户
[root@web html]# sed -i ‘/^User/c User www’ /etc/httpd/conf/httpd.conf
[root@web html]# sed -i ‘/^Group/c Group www’ /etc/httpd/conf/httpd.conf
[root@web html]# egrep “User|Group” /etc/httpd/conf/httpd.conf
#User/Group: The name (or #number) of the user/group to run httpd as.
User www
Group www
LogFormat “%h %l %u %t “%r” %>s %b “%{Referer}i” “%{User-Agent}i”” combined
LogFormat “%h %l %u %t “%r” %>s %b “%{Referer}i” “%{User-Agent}i” %I %O” combinedio
(8)创建www用户
[root@web html]# groupadd -g666 www
[root@web html]# useradd -u666 -g666 www
[root@web html]# id www
uid=666(www) gid=666(www) groups=666(www)
(9)修该对应的目录权限/var/www/html为www用户
[root@web html]# chown -R www.www /var/www/html/
[root@web html]# ll -d /var/www/html/
drwxr-xr-x. 2 www www 104 Apr 12 23:56 /var/www/html/
(10)此时查看长传成功
[root@web html]# ll
total 80
-rw-r–r–. 1 www www 38772 Apr 27 2018 bg.jpg
drwxr-xr-x. 2 www www 27 Apr 13 00:26 download
-rw-r–r–. 1 www www 2633 May 4 2018 index.html
-rw-r–r–. 1 www www 52 May 10 2018 info.php
-rw-r–r–. 1 www www 26921 Oct 20 2020 kaoshi-download.zip
-rw-r–r–. 1 www www 1114 Oct 20 2020 upload_file.php
[root@web html]# ll download/
total 4
-rw-r–r–. 1 www www 23 Apr 13 00:26 1_zhx.txt.txt
在这里插入图片描述6.搭建NFS节点 服务器10.0.0.31
(1)安装NFS服务
[root@nfs ~]# yum install nfs-untils -y
(2)修改配置
[root@nfs ~]# cat /etc/exports
/data 172.16.1.0/24(rw,async,all_squash,anonuid=666,anongid=666)
创建/data目录
[root@nfs ~]# mkdir /data
修改/data目录属主、属组的用户身份为www用户
[root@nfs ~]# chown -R www.www /data/
[root@nfs ~]#useradd -u666 -g666 www
[root@nfs ~]#groupadd -g666 www
(3)重启
[root@nfs ~]#systemctl restart nfs
7.web节点上传的资源(写入到NFS共享中)
(1)在web服务器挂载操作
[root@web download]# mount -t nfs 172.16.1.31:/data /var/www/html/download/
(2)在nfs服务器查看/data目录
[root@nfs ~]# ll /data/
total 4
-rw-r–r–. 1 www www 23 Apr 12 23:40 1_zhx.txt.txt
8.配置Backup服务器172.16.1.41
(1)(增加一个data模块,让其安装NFS服务器,与31完全保持一致)
1.安装配置Rsync服务器
[root@backup ~]# yum -y install rsync
2.配置Rsync服务器
[root@backup ~]# cat /etc/rsyncd.conf
uid = www
gid = www
port = 873
fake super = yes
use chroot = no
max connections = 200
timeout = 600
ignore errors
read only = false
list = false
auth users = rsync_backup
secrets file = /etc/rsync.passwd
log file = /var/log/rsyncd.log
[backup]
comment = welcome to backup server
path = /backup

[data]
path = /data
3.初始化操作
[root@backup ~]#groupadd -g666 www
[root@backup ~]# useradd -u666 -g666 www
[root@backup ~]# mkdir /data
[root@backup ~]# chown -R www.www /data/ /backup/启动你·
4.准备连接用户的密码文件:
[root@backup ~]# cat /etc/rsync.passwd
rsync_backup:1
[root@backup ~]# chmod 600 /etc/rsync.passwd
5.重启服务
[root@backup ~]# systemctl restart rsyncd
6.配置NFS
[root@backup ~]# yum -y install nfs-utils
[root@backup ~]# cat /etc/exports
/data 172.16.1.0/24(rw,async,all_squash,anonuid=666,anongid=666)
7.启动NFS
[root@backup ~]# systemctl enable nfs
[root@backup ~]# systemctl start nfs
(2)在NFS服务端上安装Sersync实时同步工具 (监控data目录)
1.安装命令
[root@nfs ~]# yum -y install inotify-tools rsync
2.上传软件包并解压软件包和移动到目录/
[root@nfs ~]# rz sersync2.5.4_64bit_binary_stable_final.tar.gz
[root@nfs ~]# ls
anaconda-ks.cfg sersync2.5.4_64bit_binary_stable_final.tar.gz
[root@nfs ~]# tar xf sersync2.5.4_64bit_binary_stable_final.tar.gz
[root@nfs ~]# ls
anaconda-ks.cfg GNU-Linux-x86 sersync2.5.4_64bit_binary_stable_final.tar.gz
[root@nfs ~]# mv GNU-Linux-x86/ /usr/local/sersync
3.配置Sersync
在这里插入图片描述
[root@nfs ~]# file /usr/local/sersync/confxml.xml -------------这是二进制的文件
/usr/local/sersync/confxml.xml: XML 1.0 document, ASCII text
(1)我们要监控的目录
(2)被监控的目录如果有如下事件发生,就会触发动作
(3) 同步到172.16.1.41的data模块 rsync
(4)使用什么选项 -avz
(5)使用什么连接用户
rsync_backup
(6)使用的密码是什么 存储在文件中 /etc/rsync.pass
echo “1” /ect/rsync.pass
chmod 600 /etc/rsync.pass

[root@nfs ~]# cat /usr/local/sersync/confxml.xml 
<?xml version="1.0" encoding="ISO-8859-1"?>
<head version="2.5">
    <host hostip="localhost" port="8008"></host>
    <debug start="false"/>
    <fileSystem xfs="true"/>
    <filter start="false">
	<exclude expression="(.*)\.svn"></exclude>
	<exclude expression="(.*)\.gz"></exclude>
	<exclude expression="^info/*"></exclude>
	<exclude expression="^static/*"></exclude>
    </filter>
    <inotify>
	<delete start="true"/>
	<createFolder start="true"/>
	<createFile start="true"/>
	<closeWrite start="true"/>
	<moveFrom start="true"/>
	<moveTo start="true"/>
	<attrib start="true"/>
	<modify start="true"/>
    </inotify>

    <sersync>
	<localpath watch="/data">
	    <remote ip="172.16.1.41" name="data"/>    
	</localpath>
	<rsync>
	    <commonParams params="-avz"/>
	    <auth start="true" users="rsync_backup" passwordfile="/etc/rsync.pass"/>
	    <userDefinedPort start="false" port="874"/><!-- port=874 -->
	    <timeout start="true" time="100"/><!-- timeout=100 -->
	    <ssh start="false"/>
	</rsync>
	<failLog path="/tmp/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once-->
	<crontab start="false" schedule="600"><!--600mins-->
	    <crontabfilter start="false">
		<exclude expression="*.php"></exclude>
		<exclude expression="info/*"></exclude>
	    </crontabfilter>
	</crontab>
	<plugin start="false" name="command"/>
    </sersync>

    <plugin name="command">
	<param prefix="/bin/sh" suffix="" ignoreError="true"/>	<!--prefix /opt/tongbu/mmm.sh suffix-->
	<filter start="false">
	    <include expression="(.*)\.php"/>
	    <include expression="(.*)\.sh"/>
	</filter>
    </plugin>
    <plugin name="socket">
	<localpath watch="/opt/tongbu">
	    <deshost ip="192.168.138.20" port="8009"/>
	</localpath>
    </plugin>
    <plugin name="refreshCDN">
	<localpath watch="/data0/htdocs/cms.xoyo.com/site/">
	    <cdninfo domainname="ccms.chinacache.com" port="80" username="xxxx" passwd="xxxx"/>
	    <sendurl base="http://pic.xoyo.com/cms"/>
	    <regexurl regex="false" match="cms.xoyo.com/site([/a-zA-Z0-9]*).xoyo.com/images"/>
	</localpath>
    </plugin>
</head>

[root@nfs ~]# /usr/local/sersync/sersync2 -h
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


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


4.启动sersync命令
[root@nfs ~]# /usr/local/sersync/sersync2 -dro /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
will ignore the inotify createFile event
WARNING XFS FILE SYSTEM WORK
daemon start,sersync run behind the console
use rsync password-file :
user is rsync_backup
passwordfile is /etc/rsync.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 /data && rsync -avz -R --delete ./ --timeout=100 rsync_backup@172.16.1.41::data --password-file=/etc/rsync.pass >/dev/null 2>&1
run the sersync:
watch path is: /data
5.sersync启动与停止:没有提供任何脚本:systemctl
停止:[root@nfs ~]# kill $(ps -ef |grep sersync|grep -v grep |awk ‘{print $2}’)
启动:/usr/local/sersync/sersync2 -dro /usr/local/sersync/confxml.xml

6.模拟NFS故障切换 :[root@nfs ~]# systemctl stop nfs

web服务器操作:强制卸载并挂载172.16.1.41服务器
[root@web ~]# umount -lf /var/www/html/download && mount -t nfs 172.16.1.41:/data /var/www/html/download

3.基于sersync海量文件实时同步

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值