Linux网络文件共享服务(四)数据的实时同步

    技术2025-09-19  8

    成功不易,加倍努力!

    网络文件共享服务本章总目录5 数据的实时同步5.1 实时同步技术介绍5.2 实现inotify5.2.1 内核支持5.2.2 inotify-tools工具 5.3 rsync5.3.1 rsync命令5.3.2 以独立服务方式运行rsync并实现验证功能 5.4 inotify+rsync+shell 脚本实现实时数据同步5.5 sersync 实现实时数据同步5.5.1 sersync 介绍5.5.2 基于rsync daemon 实现 sersync5.5.3 基于远程shell 实现 sersy 5.6 实战案例:实现基于分布式的LAMP架构,并将NFS实时同步到备份服务器

    网络文件共享服务

    本章总目录

    存储类型FTP服务NFS服务SAMBA服务网络数据同步

    5 数据的实时同步

    在生产环境,有时会需要两台主机的特定目录实现实时同步。比如,将NFS共享目录的数据文件,自动实时同步到备份的服务器特定目录中

    5.1 实时同步技术介绍

    实现实时同步的方法

    inotify + rsync 方式实现数据同步sersync :前金山公司周洋(花椒直播)在 inotify 软件基础上进行开发的,功能更加强大

    工作原理:

    要利用监控服务(inotify),监控同步数据服务器目录中信息的变化发现目录中数据产生变化,就利用rsync服务推送到备份服务器上

    inotify:

    异步的文件系统事件监控机制,利用事件驱动机制,而无须通过诸如cron等的轮询机制来获取事件,linux内核从2.6.13起支持 inotify,通过inotify可以监控文件系统中添加、删除,修改、移动等各种事件

    [root@data-centos8 ~]#grep -i inotify /boot/config-4.18.0-80.el8.x86_64 CONFIG_INOTIFY_USER=y #被集成在内核文件里

    实现inotify软件:

    inotify-toolssersynclrsyncd

    inotify+rsync使用方式

    inotify 对同步数据目录信息的监控rsync 完成对数据的同步利用脚本进行结合

    5.2 实现inotify

    5.2.1 内核支持

    内核是否支持inotify

    Linux支持inotify的内核最小版本为 2.6.13,参看man 7 inotify

    #列出下面的文件,说明服务器内核支持inotify [root@centos8 ~]#ls -l /proc/sys/fs/inotify -rw-r--r-- 1 root root 0 Jul 7 08:49 max_queued_events -rw-r--r-- 1 root root 0 Jul 7 08:49 max_user_instances -rw-r--r-- 1 root root 0 Jul 7 08:49 max_user_watches [root@centos8 ~]#cat /proc/sys/fs/inotify/max_queued_events 16384 [root@centos8 ~]#cat /proc/sys/fs/inotify/max_user_instances 128 [root@centos8 ~]#cat /proc/sys/fs/inotify/max_user_watches 8192

    inotify内核参数说明:

    max_queued_events:inotify 事件队列最大长度,如值太小会出现 Event Queue Overflow 错误,默认值:16384, 生产环境建议调大,比如:327679max_user_instances:每个用户创建inotify实例最大值,默认值:128max_user_watches:可以监视的文件的总数量(inotifywait 单进程),默认值:8192,建议调大

    范例:修改inotify内核参数默认值

    [root@data-centos8 ~]#vim /etc/sysctl.conf fs.inotify.max_queued_events=66666 fs.inotify.max_user_watches=100000 [root@centos8 ~]#sysctl -p fs.inotify.max_queued_events = 66666 fs.inotify.max_user_watches = 100000 [root@centos8 ~]#cat /proc/sys/fs/inotify/* 66666 128 100000
    5.2.2 inotify-tools工具

    inotify-tools参考文档:https://github.com/rvoicilas/inotify-tools/wiki

    安装inotify-tools:基于epel源

    [root@data-centos8 ~]# yum -y install inotify-tools

    inotify-tools包主要工具:

    inotifywait: 在被监控的文件或目录上等待特定文件系统事件(open ,close,delete等)发生,会在屏幕上打印相关信息,常用于实时同步的目录监控(相对常用)inotifywatch:收集被监控的文件系统使用的统计数据,指文件系统事件发生的次数统计(相对少用)

    inotifywait 命令

    格式:

    inotifywait [ options ] file1 [ file2 ] [ file3 ] [ ... ]

    常用选项:

    -m, --monitor 始终保持事件监听 -d, --daemon 以守护进程方式执行,和-m相似,配合-o使用 -r, --recursive 递归监控目录数据信息变化 -q, --quiet 输出少量事件信息 --exclude <pattern> 指定排除文件或目录,使用扩展的正则表达式匹配的模式实现 --excludei <pattern> 和exclude相似,不区分大小写 -o, --outfile <file> 打印事件到文件中,相当于标准正确输出,注意:使用绝对路径 -s, --syslogOutput 发送错误到syslog相当于标准错误输出 --timefmt <fmt> 指定时间输出格式 --format <fmt> 指定的输出格式;即实际监控输出内容 -e 指定监听指定的事件,如果省略,表示所有事件都进行监听

    inotifywait 的–timefmt 时间格式

    参考 man 3 strftime

    %Y 年份信息,包含世纪信息 %y 年份信息,不包括世纪信息 %m 显示月份,范围 01-12 %d 每月的第几天,范围是 01-31 %H 小时信息,使用 24小时制,范围 00-23 %M 分钟,范围 00-59 %S 秒,范例 0-60

    范例:

    --timefmt "%Y-%m-%d %H:%M:%S"

    inotifywait 的 --format 格式定义

    %T 输出时间格式中定义的时间格式信息,通过 --timefmt option 语法格式指定时间信息 %w 事件出现时,监控文件或目录的名称信息,相当于dirname %f 事件出现时,将显示监控目录下触发事件的文件或目录信息,否则为空,相当于basename %e 显示发生的事件信息,不同的事件默认用逗号分隔 %Xe 显示发生的事件信息,不同的事件指定用X进行分隔

    范例:

    --format "%T %w%f event: %;e" --format '%T %w %f'

    inotifywait -e 选项指定的事件类型

    create 文件或目录创建 delete 文件或目录被删除 modify 文件或目录内容被写入 attrib 文件或目录属性改变 close_write 文件或目录关闭,在写入模式打开之后关闭的 close_nowrite 文件或目录关闭,在只读模式打开之后关闭的 close 文件或目录关闭,不管读或是写模式 open 文件或目录被打开 lsdir 浏览目录内容 moved_to 文件或目录被移动到监控的目录中 moved_from 文件或目录从监控的目录中被移动 move 文件或目录不管移动到或是移出监控目录都触发事件 access 文件或目录内容被读取 delete_self 文件或目录被删除,目录本身被删除 unmount 取消挂载

    范例:只把关注的事件显示出来

    -e create,delete,moved_to,close_write, attrib

    范例:使用inotifywait

    #监控一次性事件 inotifywait /data/www #跟踪一个文件夹,一次性的任务 Setting up watches. Watches established. /data/www/ CREATE f1.txt #-m持续前台监控,-r递归监控目录,-q输出少量信息,排除.swx和swp为后缀文件 inotifywait -mrq /data/www --exclude=".*\.swx|\.swp" /data/www/ OPEN f1.txt /data/www/ ACCESS f1.txt /data/www/ CLOSE_NOWRITE,CLOSE f1.txt #持续后台监控,并记录日志 inotifywait -o /root/inotify.log -drq /data/www --timefmt "%Y-%m-%d %H:%M:%S" --format "%T %w%f event: %e" #-o输出信息传到文件里,-d后台执行 #持续前台监控特定事件 inotifywait -mrq /data/www --timefmt "%F %H:%M:%S" --format "%T %w%f event:%;e" -e create,delete,moved_to,close_write,attrib #-e只把我们关注的事件显示出来

    5.3 rsync

    rsync 常用于做为 linux系统下的数据镜像备份工具,实现远程同步,支持本地复制,或者与其他SSH、rsync主机同步数据,支持增量备份,配合任务计划,rsync能实现定时或间隔同步,配合inotify或sersync,可以实现触发式的实时数据同步

    官方网站: http://rsync.samba.org/

    软件包:rsync,rsync-daemon(CentOS 8)

    服务文件:/usr/lib/systemd/system/rsyncd.service

    配置文件:/etc/rsyncd.conf

    端口:873/tcp

    5.3.1 rsync命令

    rsync 格式

    #Local: rsync [OPTION...] SRC... [DEST] #Access via remote shell: Pull:远程文件拉到本机 rsync [OPTION...] [USER@]HOST:SRC... [DEST] Push:本机文件推到目标机器 rsync [OPTION...] SRC... [USER@]HOST:DEST #Access via rsync daemon: Pull: rsync [OPTION...] [USER@]HOST::SRC... [DEST] rsync [OPTION...] rsync://[USER@]HOST[:PORT]/SRC... [DEST] Push: rsync [OPTION...] SRC... [USER@]HOST::DEST rsync [OPTION...] SRC... rsync://[USER@]HOST[:PORT]/DEST #The ':' usages connect via remote shell, while '::' & 'rsync://' usages connect to an rsync daemon, and require SRC or DEST to start with a module name. #一个:就代表是远程ssh,两个:代表是守护进程

    rsync有三种工作方式:

    本地文件系统上实现同步。命令行语法格式为上述"Local"段的格式。

    本地主机使用远程shell和远程主机通信。命令行语法格式为上述"Access via remote shell"段的格式。

    本地主机通过网络套接字连接远程主机上的rsync daemon。命令行语法格式为上述"Access via rsync daemon"段的格式。

    前两者的本质是通过本地或远程shell,而第3种方式则是让远程主机上运行rsyncd服务,使其监听在一个端口上,等待客户端的连接。

    常见选项:

    -v: 显示rsync过程中详细信息。可以使用"-vvvv"获取更详细信息。 -P: 显示文件传输的进度信息。(实际上"-P"="--partial --progress",其中的"--progress"才是显示进度信息的)。 -n --dry-run : 仅测试传输,而不实际传输。常和"-vvvv"配合使用来查看rsync是如何工作的。 -a --archive : 归档模式,表示递归传输并保持文件属性。等同于"-rtopgDl"。 -r --recursive:递归到目录中去。 -t --times: 保持mtime属性。强烈建议任何时候都加上"-t",否则目标文件mtime会设置为系统时间,导致下次更新 : 检查出mtime不同从而导致增量传输无效。 -o --owner: 保持owner属性(属主)。 -g --group: 保持group属性(属组)。 -p --perms: 保持perms属性(权限,不包括特殊权限)。 -D : 是"--device --specials"选项的组合,即也拷贝设备文件和特殊文件。 -l --links: 如果文件是软链接文件,则拷贝软链接本身而非软链接所指向的对象 -z : 传输时进行压缩提高效率 -R --relative: 使用相对路径。意味着将命令行中指定的全路径而非路径最尾部的文件名发送给服务端,包括它们的属性。用法见下文示例。 --size-only : 默认算法是检查文件大小和mtime不同的文件,使用此选项将只检查文件大小。 -u --update : 仅在源mtime比目标已存在文件的mtime新时才拷贝。注意,该选项是接收端判断的,不会影响删除行为。 -d --dirs : 以不递归的方式拷贝目录本身。默认递归时,如果源为"dir1/file1",则不会拷贝dir1目录,使用该选项将拷贝dir1但不拷贝file1。 --max-size : 限制rsync传输的最大文件大小。可以使用单位后缀,还可以是一个小数值(例如:"--max-size=1.5m") --min-size : 限制rsync传输的最小文件大小。这可以用于禁止传输小文件或那些垃圾文件。 --exclude : 指定排除规则来排除不需要传输的文件。 --delete : 以SRC为主,对DEST进行同步。多则删之,少则补之。注意"--delete"是在接收端执行的,所以它是在 :exclude/include规则生效之后才执行的。 -b --backup : 对目标上已存在的文件做一个备份,备份的文件名后默认使用"~"做后缀。 --backup-dir: 指定备份文件的保存路径。不指定时默认和待备份文件保存在同一目录下。 -e : 指定所要使用的远程shell程序,默认为ssh。 --port : 连接daemon时使用的端口号,默认为873端口。 --password-file:daemon模式时的密码文件,可以从中读取密码实现非交互式。注意,这不是远程shell认证的密码,而是rsync模块认证的密码。 -W --whole-file:rsync将不再使用增量传输,而是全量传输。在网络带宽高于磁盘带宽时,该选项比增量传输更高效。 --existing : 要求只更新目标端已存在的文件,目标端还不存在的文件不传输。注意,使用相对路径时如果上层目录不存在也不会传输。 --ignore-existing:要求只更新目标端不存在的文件。和"--existing"结合使用有特殊功能,见下文示例。 --remove-source-files:要求删除源端已经成功传输的文件

    范例:两种格式访问 rsync daemon 服务(这两种方式最大的缺陷就是任何人不用key验证就可以进行同步,很不安全)

    #在备份服务器启动 rsync 进程 [root@backup-centos8 ~]#rsync --daemon Failed to parse config file: /etc/rsyncd.conf [root@backup-centos8 ~]#touch /etc/rsyncd.conf [root@backup-centos8 ~]#rsync --daemon [root@backup-centos8 ~]#ss -ntlp|grep rsync LISTEN 0 5 0.0.0.0:873 0.0.0.0:* users:(("rsync",pid=2697,fd=4)) LISTEN 0 5 [::]:873 [::]:* users:(("rsync",pid=2697,fd=5)) [root@backup-centos8 ~]# [root@backup-centos8 ~]#cat /etc/rsyncd.conf [backup] #模块名,代表指定的共享资源的目录 path = /data/backup/ read only = no #指定可读写,默认只读 #指定目录给nobody权限,默认用户以nobody访问此目录 [root@backup-centos8 ~]#setfacl -m u:nobody:rwx /data/backup/ #查看rsync服务器的模块名称 [root@data-centos8 ~]#rsync rsync://backup-server backup [root@data-centos8 ~]#rsync backup-server:: backup #访问rsync服务器的共享目录 [root@data-centos8 ~]#rsync /etc/networks root@backup-server::backup [root@data-centos8 ~]#rsync /etc/shells rsync://root@backup-server/backup [root@data-server ~]#rsync 10.0.0.18::backup/* /opt [root@data-server ~]#rsync rsync://10.0.0.18/backup/* /mnt
    5.3.2 以独立服务方式运行rsync并实现验证功能

    范例:以独立服务方式运行 rsync

    [root@backup-centos8 ~]#dnf install rsync-daemon #创建rsync服务器的配置文件 [root@centos8 ~]#vi /etc/rsyncd.conf uid = root #提定以哪个用户来访问共享目录,将之指定为生成的文件所有者,默认为nobody gid = root #默认为nobody #port = 874 可指定非标准端口,默认873/tcp #use chroot = no max connections = 0 ignore errors exclude = lost+found/ log file = /var/log/rsyncd.log pid file = /var/run/rsyncd.pid lock file = /var/run/rsyncd.lock reverse lookup = no #hosts allow = 10.0.0.0/24 [backup] #每个模块名对应一个不同的path目录,如果同名后面模块生效 path = /data/backup/ comment = backup dir read only = no #默认是yes,即只读 auth users = rsyncuser #默认anonymous可以访问rsync服务器,这里指定用户访问 secrets file = /etc/rsync.pas #用户密码 #服务器端准备目录 [root@backup-centos8 ~]#mkdir -pv /data/backup #服务器端生成验证文件 [root@backup-centos8 ~]#echo "rsyncuser:magedu" > /etc/rsync.pas [root@backup-centos8 ~]#chmod 600 /etc/rsync.pas #服务器端启动rsync服务 [root@backup-centos8 ~]#rsync --daemon #可加入/etc/rc.d/rc.local实现开机启动 [root@backup-centos8 ~]#systemctl start rsyncd #CentOS 7 以上版本 #客户端配置密码文件 #也可将密码赋值给环境变量RSYNC_PASSWORD变量,但不安全 #export RSYNC_PASSWORD=magedu [root@data-centos8 ~]#echo "magedu" > /etc/rsync.pas [root@data-centos8 ~]#chmod 600 /etc/rsync.pas #此为必要项,权限必须修改 #查看远程rsync服务器的模块信息 [root@data-server ~]#rsync rsync://10.0.0.18 backup backup dir #查看具体模块内的文件需要验证 [root@data-server ~]#rsync rsync://10.0.0.18/backup Password: #客户端测试同步数据 [root@data-centos8 ~]#rsync -avz --delete --password-file=/etc/rsync.pas /data/www/ rsyncuser@rsync服务器IP::backup [root@data-centos8 ~]#rsync -avz --delete --password-file=/etc/rsync.pas rsyncuser@rsync服务器IP::backup /data/www/

    5.4 inotify+rsync+shell 脚本实现实时数据同步

    按 5.3 搭建好 rsyncd的备份服务器,在数据服务器上创建inotify_rsync.sh脚本

    注意: 此脚本执行前先确保两主机初始数据处于同步状态,此脚本实现后续的数据同步

    [root@data-centos8 ~]#vim inotify_rsync.sh #!/bin/bash SRC='/data/www/' #注意最后的/ DEST='rsyncuser@rsync服务器IP::backup' rpm -q rsync &> /dev/null || yum -y install rsync inotifywait -mrq --exclude=".*\.swp" --timefmt '%Y-%m-%d %H:%M:%S' --format '%T %w %f' -e create,delete,moved_to,close_write,attrib ${SRC} |while read DATE TIME DIR FILE;do FILEPATH=${DIR}${FILE} rsync -az --delete --password-file=/etc/rsync.pas $SRC $DEST && echo "At ${TIME} on ${DATE}, file $FILEPATH was backuped up via rsync" >> /var/log/changelist.log done #查看文件传输日志 [root@data-server www]#touch f1.txt [root@data-centos8 ~]#tail -f /var/log/changelist.log At 10:13:38 on 2020-07-08, file /data/www/f1.txt was backuped up via rsync At 10:13:38 on 2020-07-08, file /data/www/f1.txt was backuped up via rsync At 10:13:38 on 2020-07-08, file /data/www/f1.txt was backuped up via rsync

    5.5 sersync 实现实时数据同步

    5.5.1 sersync 介绍

    sersync类似于inotify,同样用于监控,但它克服了inotify的缺点.

    inotify最大的不足是会产生重复事件,或者同一个目录下多个文件的操作会产生多个事件,例如,当监控目录中有5个文件时,删除目录时会产生6个监控事件,从而导致重复调用rsync命令。比如:vim文件时,inotify会监控到临时文件的事件,但这些事件相对于rsync来说是不应该被监控的

    sersync 优点:

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

    sersync项目地址: https://code.google.com/archive/p/sersync/

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

    5.5.2 基于rsync daemon 实现 sersync
    #在数据服务器上下载sersync,并拷贝至相应的目录,设置PATH变量 [root@data-centos8 ~]#wget https://storage.googleapis.com/google-code-archivedownloads/v2/code.google.com/sersync/sersync2.5.4_64bit_binary_stable_final.tar.gz [root@data-centos8 ~]#tar xf sersync2.5.4_64bit_binary_stable_final.tar.gz [root@data-centos8 ~]#cp -a GNU-Linux-x86 /usr/local/sersync [root@data-centos8 ~]#echo 'PATH=/usr/local/sersync:$PATH' > /etc/profile.d/sersync.sh [root@data-centos8 ~]#source /etc/profile.d/sersync.sh #sersync目录只有两个文件:一个是二进制程序文件,一个是xml格式的配置文件 [root@data-centos8 ~]#ls /usr/local/sersync/ confxml.xml sersync2 #确认安装rsync客户端工具 [root@data-centos8 ~]#rpm -q rsync &> /dev/null || dnf -y install rsync #备份sersync配置文件 [root@data-centos8 ~]#cp /usr/local/sersync/confxml.xml{,.bak} #修改sersync配置文件 [root@data-centos8 ~]#vim /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="false"/> <filter start="false"> #不开启文件过滤功能,当为true时,以下类型的文件将不同步 <exclude expression="(.*)\.svn"></exclude> <exclude expression="(.*)\.gz"></exclude> <exclude expression="^info/*"></exclude> <exclude expression="^static/*"></exclude> </filter> <inotify> # 监控事件,默认监控 delete/close_write/moved_from/moved_to/create folder <delete start="true"/> <createFolder start="true"/> <createFile start="false"/> <closeWrite start="true"/> <moveFrom start="true"/> <moveTo start="true"/> <attrib start="true"/> #修改此行为true,文件属性变化后也会同步 <modify start="false"/> </inotify> <sersync> # rsync命令的配置段 <localpath watch="/data/www"> #修改此行,需要同步的源目录或文件,建议同步目录 <remote ip="备份服务器IP" name="backup"/> #修改此行,指定备份服务器地址和rsync daemon的模块名,如果下面开启了ssh start,此时name为远程shell方式运行时的目标目录 <!--<remote ip="192.168.8.39" name="tongbu"/>--> <!--<remote ip="192.168.8.40" name="tongbu"/>--> </localpath> <rsync> <commonParams params="-artuz"/> # 指定rsync选项 <auth start="true" users="rsyncuser" passwordfile="/etc/rsync.pas"/> #修改此行,为true,定备份服务器的rsync配置的用户和密码文件 <userDefinedPort start="false" port="874"/><!-- port=874 -->#指定rsync的非标准端口号 <timeout start="false" time="100"/><!-- timeout=100 --> <ssh start="false"/> #默认使用rsync daemon运行rsync命令,true为使用远程shell模式 </rsync> <failLog path="/tmp/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once--> #错误重传及日志文件路径 <crontab start="false" schedule="600"><!--600mins--> #不开启crontab功能 <crontabfilter start="false"> #不开启crontab定时传输的筛选功能 <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> #创建连接rsynd服务器的用户密码文件,并必须修改权限 [root@data-centos8 ~]#echo magedu > /etc/rsync.pas [root@data-centos8 ~]#chmod 600 /etc/rsync.pas #查看帮助 [root@data-centos8 ~]#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参数,则默认执行同步程序 #以后台方式执行同步 [root@data-centos8 ~]#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 daemon start,sersync run behind the console use rsync password-file : user is rsyncuser passwordfile is /etc/rsync.pas 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... #如果同步失败,可以手动执行下面命令,观察过程 [root@data-centos8 ~]# cd /data/www && rsync -artuz -R --delete ./ rsyncuser@backup-server::backup --password-file=/etc/rsync.pas >/dev/null 2>&1 run the sersync: watch path is: /data/www ________________________________________________________________ #sersync支持多实例,也即监控多个目录时,只需分别配置不同配置文件,然后使用sersync2指定对应配置文件运行 [root@data-centos8 ~]#sersync2 -rd -o /etc/sersync.d/nginx.xml [root@backup-server ~]#watch -n0.1 ls -l /data/backup/ #不想用了可以杀掉: killall sersync2
    5.5.3 基于远程shell 实现 sersy
    #不需要配置rsync daemon,只需要配置基于key验证的ssh即可 [root@data-centos8 ~]#ssh-keygen [root@data-centos8 ~]#ssh-copy-id backup-server #下载sersync,并拷贝至相应的目录,设置PATH变量同5.5.2 #修改sersync配置文件 [root@data-centos8 ~]#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="false"/> <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="false"/> <closeWrite start="true"/> <moveFrom start="true"/> <moveTo start="true"/> <attrib start="true"/> #修改此行为true <modify start="false"/> </inotify> <sersync> <localpath watch="/data/www"> #修改此行,指定源数据目录 <remote ip="备份服务器IP" name="/data/backup"/> #修改此行指定备份服务器地址和备份目标目录 <!--<remote ip="192.168.8.39" name="tongbu"/>--> <!--<remote ip="192.168.8.40" name="tongbu"/>--> </localpath> <rsync> <commonParams params="-artuz"/> <auth start="false" users="root" passwordfile="/etc/rsync.pas"/> #必须修改此行,不启用认证 <userDefinedPort start="false" port="874"/><!-- port=874 --> <timeout start="false" time="100"/><!-- timeout=100 --> <ssh start="true"/> #修改此行为true,使用远程shell方式的rsync连接方式,无需在目标主机上配置启动rsync daemon服务 #####################################以下行不需要修改 #################################### </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> #将中间的行可以删除 </head> [root@data-centos8 ~]#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: /apps/sersync/confxml.xml daemon thread num: 10 parse xml config file host ip : localhost host port: 8008 daemon start,sersync run behind the console 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 -auz -R --delete ./ -e ssh 10.0.0.18:/data/backup >/dev/null 2>&1 run the sersync: watch path is: /data/www

    5.6 实战案例:实现基于分布式的LAMP架构,并将NFS实时同步到备份服务器

    Processed: 0.010, SQL: 9