目的:了解生产环境中存储同步到备份节点是如何搭配使用的;
只要当前目录发生变化则会触发一个事件,事件触发 后将变化的目录同步至远程服务器。
实时同步需要借助 Inotify 通知接口,用来监控本 地目录的变化,如果监控本地的目录发生变更。则触 发动作,这个动作可以是进行同步操作,或其他操 作。 create、modify、write、
场景一、解决 nfs 单点故障。保证同步的数据连续性。 nfs --> backup;
场景二(上云业务)本地无缝迁移云端。


sersync 是国人基于 rsync+inotify 基础之上开发的工具,强化了实时监控,文件过滤,简化配置等功能,帮助用户提高运行效率,节省时间和网络资源,社区项目不再更新;(实际生产环境,数据抖动率较小,百万张数据可以做到实时同步)
lsyncd 是一款开源的数据实时同步工具,基于 inotify 和 rsync 基础之上进行开发,主要用于网站数据备份、网站搬家等等。
目的:实现 web 上传视频文件,实则是写入 NFS 至存储,当 NFS 存在新的数据则会实时的同步到备份服务

实验拓扑
//安装 nfs服务组件[root@nfs_0.14[ ~]# rpm -aq nfs-utils //有的话不用再安装nfs-utils-1.3.0-0.68.el7.2.x86_64[root@nfs_0.14[ ~] yum install -y nfs-utils//配置NFS,自己指定一个用户ID为666的www用户[root@nfs_0.14[ ~] cat /etc/exports/share_data001 10.0.0.0/24(rw,sync,all_squash,anonuid=666,anongid=666)//系统中要存在这个用户,NFS服务端创建用户/组Id为666的www用户[root@nfs_0.14[ /]# groupadd -g 666 www[root@nfs_0.14[ /]# useradd -u 666 -g 666 www[root@nfs_0.14[ /]# id wwwuid=666(www) gid=666(www) 组=666(www)//创建共享目录 实际生产环境要大一点的空间[root@nfs_0.14[ /]# mkdir /share_data001/[root@nfs_0.14[ /]# chown -R www.www /share_data001/[root@nfs_0.14[ /]# ls -ld /share_data001/drwxr-xr-x 2 www www 37 7月 7 21:53 /share_data001///开机自启动&启动[root@nfs_0.14[ /] systemctl enable nfs-server ^C[root@nfs_0.14[ /] systemctl start nfs-server[root@nfs_0.14[ /] netstat -lntup | grep 111tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 1131/rpcbindtcp6 0 0 :::111 :::* LISTEN 1131/rpcbindudp 0 0 0.0.0.0:111 0.0.0.0:* 1131/rpcbindudp6 0 0 :::111 :::* 1131/rpcbind//便于部署,暂用apache_web服务[root@web01_0.21[ ~] rpm -qa httpd //有的话不用安装httpd-2.4.6-99.el7.centos.1.x86_64root@web01_0.21[ ~]# yum install -y httpd已加载插件:fastestmirrorLoading mirror speeds from cached hos....//启动apache服务[root@web01_0.21[ ~] systemctl start httpd[root@web01_0.21[ ~] netstat -lntup | grep 80tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1480/sshdtcp6 0 0 :::22 :::* LISTEN 1480/sshdtcp6 0 0 :::80 :::* LISTEN 1827/httpd
//假设用户上传的资料文件,在此目录下/var/www/html/.//web站点中,apache默认的执行用户身份是apache;[root@web01_0.21[ /var/www/html]# ps aux | grep httpdroot 1799 0.0 0.0 230448 5228 ? Ss 19:25 0:00 /usr/sbin/httpd -DFOREGROUNDapache 1800 0.0 0.0 230584 3740 ? S 19:25 0:00 /usr/sbin/httpd -DFOREGROUNDapache 1801 0.0 0.0 230584 3748 ? S 19:25 0:00 /usr/sbin/httpd -DFOREGROUNDapache 1802 0.0 0.0 230448 2984 ? S 19:25 0:00 /usr/sbin/httpd -DFOREGROUNDapache 1803 0.0 0.0 230448 2984 ? S 19:25 0:00 /usr/sbin/httpd -DFOREGROUNDapache 1804 0.0 0.0 230448 2984 ? S 19:25 0:00 /usr/sbin/httpd -DFOREGROUNDapache 1805 0.0 0.0 230448 2984 ? S 19:25 0:00 /usr/sbin/httpd -DFOREGROUNDapache 1806 0.0 0.0 230448 2984 ? S 19:25 0:00 /usr/sbin/httpd -DFOREGROUNDapache 1807 0.0 0.0 230448 2984 ? S 19:25 0:00 /usr/sbin/httpd -DFOREGROUNDroot 1829 0.0 0.0 112828 984 pts/0 S+ 19:31 0:00 grep --color=auto httpd//实际上文件上传的目录所有者是root[root@web01_0.21[ /var/www/html]# ls -ld /var/www/html/drwxr-xr-x 2 root root 6 5月 30 2023 /var/www/html///修改上传静态资源的目录属主是www,和NFS保持的用户一致[root@web01_0.21[ /var/www/html]# pwd/var/www/htmlroot@web01_0.21[ /var/www/html] chown -R www.www /var/www/html/[root@web01_0.21[ /var/www/html]# ls -ld ./drwxr-xr-x 2 www www 6 5月 30 2023 .///在web服务器上添加统一的进程验证用户[root@web01_0.21[ /var/www/html] groupadd -g 666 www[root@web01_0.21[ /var/www/html] useradd -u 666 -g 666 www[root@web01_0.21[ /var/www/html] id wwwuid=666(www) gid=666(www) 组=666(www)//需要修改web服务器的web配置文件。将原有apche进程用户身份修改成www[root@web01_0.21[ /var/www/html] grep -E '^(User|Group)' /etc/httpd/conf/httpd.conf | grep -v '^#'User wwwGroup www//当然,也可以使用sed取值修改,c的意思是指:以xxx开头的一整行进行替换,连同xxx后面所有的一整行内容都替换掉[root@web01_0.21[ /var/www/html] cp /etc/httpd/conf/httpd.conf /etc/httpd/conf/httpd.conf.back[root@web01_0.21[ /var/www/html] sed -i '/^User/c User apache' /etc/httpd/conf/httpd.conf.back[root@web01_0.21[ /var/www/html] sed -i '/^Group/c Group apache' /etc/httpd/conf/httpd.conf.back**[root@web01_0.21[ /var/www/html] grep -E '^(User|Group)' /etc/httpd/conf/httpd.conf.back | grep -v '^#'User apacheGroup apache// 模拟用户上传资料到该目录上[root@web01_0.21[ /var/www/html]# rz -y[root@web01_0.21[ /var/www/html]# ls -l总用量 4-rw-r--r-- 1 root root 3814 1月 5 2008 eula.2052.txt//按照需求,此目录是作为NFS中共享目录的入口[root@web01_0.21[ /]# mount -t nfs 10.0.0.14:/share_data001 /var/www/html/[root@web01_0.21[ /]# df -h文件系统 容量 已用 可用 已用% 挂载点devtmpfs 3.9G 0 3.9G 0% /devtmpfs 3.9G 0 3.9G 0% /dev/shmtmpfs 3.9G 12M 3.9G 1% /runtmpfs 3.9G 0 3.9G 0% /sys/fs/cgroup/dev/mapper/centos_centos--moude-root 44G 2.6G 42G 6% //dev/sda1 1014M 169M 846M 17% /boot10.0.0.14:/share_data001 44G 2.6G 42G 6% /mnttmpfs 797M 0 797M 0% /run/user/0


<<< 左右滑动见更多 >>>

//我之前安装过RSYNC服务端 [root@rsync-server_0.12[ /]# rpm -qa | grep rsyncrsync-3.1.2-12.el7_9.x86_64//修改Rsync服务端配置文件[root@rsync-server_0.12[ /]# cat /etc/rsyncd.confuid = www //这是要创建的www进程执行用户gid = wwwport = 873fake super = yesuse chroot = nomax connections = 200timeout = 600#ignore errorsread only = falselist = trueauth users = rsync_backupsecrets file = /etc/rsync.passwdlog file = /var/log/rsyncd.log####################################[linshi01]comment = “这是临时rsync的测试文件”path = /linshi01[sharedata101]"comment = "这是NFS、Web01的业务数据""path = /sharedata-101[share_data001-rsync-NFS] //这是新增的模块path = /share_data001//查看rsync服务端是否有www这个用户[root@rsync-server_0.12[ /]# groupadd -g 666 www[root@rsync-server_0.12[ /]# useradd -u 666 -g 666 www[root@rsync-server_0.12[ /]# id wwwuid=666(www) gid=666(www) 组=666(www)//创建备份目录[root@rsync-server_0.12[ /]# mkdir /share_data001[root@rsync-server_0.12[ /]# chown -R www.www /share_data001/[root@rsync-server_0.12[ /]# ls -ld /share_data001/drwxr-xr-x 2 www www 6 7月 10 21:22 /share_data001///重启服务[root@rsync-server_0.12[ /]# systemctl restart rsyncd
[root@rsync-server_0.12[ /] rpm -aq nfs-utils[root@rsync-server_0.12[ /] yum install -y nfs-utils已加载插件:fastestmirror//将NFS服务器的配置拉取到rsync备份服务器中[root@rsync-server_0.12[ /]# rpm -qa nfs-utilsnfs-utils-1.3.0-0.68.el7.2.x86_64[root@rsync-server_0.12[ /] rsync -avz 10.0.0.14:/etc/exports /etc/exportsThe authenticity of host '10.0.0.14 (10.0.0.14)' can't be established.ECDSA key fingerprint is SHA256:Rsz40jXQHU7ASd2WGAc5BJggESaRkKIFYWs1lZiY+8s.ECDSA key fingerprint is MD5:7f:4b:53:47:2f:a9:cd:64:d5:84:d5:4f:2c:44:02:20.Are you sure you want to continue connecting (yes/no)? yesWarning: Permanently added '10.0.0.14' (ECDSA) to the list of known hosts.root@10.0.0.14's password: receiving incremental file listexportssent 43 bytes received 152 bytes 26.00 bytes/sectotal size is 73 speedup is 0.37[root@rsync-server_0.12[ /] cat /etc/exports //同步配置/share_data001 10.0.0.0/24(rw,sync,all_squash,anonuid=666,anongid=666)//在rsync服务端中重启Nfs[root@rsync-server_0.12[ /]# systemctl restart nfs-server[root@rsync-server_0.12[ /]# systemctl enable nfs-serverCreated symlink from /etc/systemd/system/multi-user.target.wants/nfs-server.service to /usr/lib/systemd/system/nfs-server.service.//NFS节点服务器上下载 rsync和inotify工具[root@nfs_0.14[ /]# yum install rsync inotify-tools -y//在NFS节点下载 sersync ,由于它托管在github上,需要翻q才可以,因此用其他链接的镜像下载[root@nfs_0.14[ /tools] tar xf sersync2.5.4_64bit_binary_stable_final.tar.gz //解压[root@nfs_0.14[ /tools]# ls -ldrwxr-xr-x 2 root root 41 10月 26 2011 GNU-Linux-x86 /发现GNU开头的文件夹//移动文件夹并改名,方便后续配置(版本未有变化的前提下,可以不做软链)[root@nfs_0.14[ /tools]# mv GNU-Linux-x86/ /usr/local/sersync[root@nfs_0.14[ /tools]# cd /usr/local/sersync/[root@nfs_0.14[ /usr/local/sersync]# ls -l总用量 1772-rwxr-xr-x 1 root root 2214 10月 26 2011 confxml.xml //配置文件,格式为xml,重点了解-rwxr-xr-x 1 root root 1810128 10月 26 2011 sersync2 //命令工具包;//使用file命令查看这两个文件类型//这是二进制文件--执行程序[root@nfs_0.14[ /usr/local/sersync]# file sersync2 sersync2: ELF 64-bit LSB executable, x86-64, version 1 (GNU/Linux), statically linked, for GNU/Linux 2.6.15, BuildID[sha1]=c41f3d73f3d4fe1d6931d93a4e64fe805769b28f, stripped//配置文件的格式[root@nfs_0.14[ /usr/local/sersync]# file confxml.xml confxml.xml: XML 1.0 document, ASCII textsersync配置文件详解

//原sersync的配置文件[root@nfs_0.14[ /usr/local/sersync]# cat 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"/> //这里需要修改文件系统类型true <filter start="false"> //排除,正则表达式的方式,用来做代码同步这些格式 <exclude expression="(.*)\.svn"></exclude> <exclude expression="(.*)\.gz"></exclude> <exclude expression="^info/*"></exclude> <exclude expression="^static/*"></exclude> </filter> <inotify> //监控本地文件的事件 ,这里都需要改成true <delete start="true"/> //删除 <createFolder start="true"/> //创建文件夹 <createFile start="false"/> //创建文件夹 <closeWrite start="true"/> //写入后的关闭 <moveFrom start="true"/> //移动的从 <moveTo start="true"/> //移动的到 <attrib start="false"/> //属性 <modify start="false"/> //修改 </inotify> <sersync> //当事件发生后,监控的本地目录 <localpath watch="/opt/tongbu"> //这里修改监控的本地目录 <remote ip="127.0.0.1" name="tongbu1"/> //监控到发生事件后,推送到远程服务器的哪个模块之上 <!--<remote ip="192.168.8.39" name="tongbu"/>--> <!--<remote ip="192.168.8.40" name="tongbu"/>--> </localpath> <rsync> <commonParams params="-artuz"/> //rsync的选项,这里要成-avz <auth start="false" users="root" passwordfile="/etc/rsync.pas"/> //rsync的认证选项要打开 <userDefinedPort start="false" port="874"/><!-- port=874 --> //端口保持874 <timeout start="false" time="100"/><!-- timeout=100 --> //这里修改成true <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_0.14[ /usr/local/sersync]# cat 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="/share_data001"> <remote ip="10.0.0.12" name="share_data001-rsync-NFS"/> <!--<remote ip="192.168.8.39" name="tongbu"/>--> <!--<remote ip="192.168.8.40" name="tongbu"/>--> </localpath> <rsync> <commonParams params="-avz"/> <auth start="true" users="rsync_backup" passwordfile="/etc/rsync.client"/> <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>
//启动前,查看sersycn的参数,这里保持不变[root@nfs_0.14[ /]# /usr/local/sersync/sersync2 -hset the system paramexecute:echo 50000000 > /proc/sys/fs/inotify/max_user_watchesexecute:echo 327679 > /proc/sys/fs/inotify/max_queued_eventsparse 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@nfs_0.14[ /]# /usr/local/sersync/sersync2 -dro /usr/local/sersync/confxml.xml set the system paramexecute:echo 50000000 > /proc/sys/fs/inotify/max_user_watchesexecute:echo 327679 > /proc/sys/fs/inotify/max_queued_eventsparse the command paramoption: -d run as a daemonoption: -r rsync all the local files to the remote servers before the sersync workoption: -o config xml name: /usr/local/sersync/confxml.xmldaemon thread num: 10parse xml config filehost ip : localhost host port: 8008will ignore the inotify createFile event WARNING XFS FILE SYSTEM WORKdaemon start,sersync run behind the console use rsync password-file :user is rsync_backuppasswordfile is /etc/rsync.clientconfig xml parse successplease set /etc/rsyncd.conf max connections=0 Manuallysersync 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 onceworking please wait...execute command: cd /share_data001 && rsync -avz -R --delete ./ --timeout=100 rsync_backup@10.0.0.12::share_data001-rsync-NFS --password-file=/etc/rsync.client >/dev/null 2>&1 run the sersync: watch path is: /share_data001
//因为sersync是二进制文件,是不提供命令关闭的,只能强制杀掉进程[root@nfs_0.14[ /share_data001]# ps aux | grep sersyncroot 1784 0.0 0.0 92324 700 ? Ssl 19:53 0:00 /usr/local/sersync/sersync2 -dro /usr/local/sersync/confxml.xmlroot 1800 0.0 0.0 112824 988 pts/0 S+ 19:54 0:00 grep --color=auto sersync[root@nfs_0.14[ /share_data001]# ps aux | grep sersync | grep -v greproot 1784 0.0 0.0 92324 700 ? Ssl 19:53 0:00 /usr/local/sersync/sersync2 -dro /usr/local/sersync/confxml.xml[root@nfs_0.14[ /share_data001]# ps aux | grep sersync | grep -v grep | awk '{print $2}'1784//强制杀掉[root@nfs_0.14[ /share_data001]# kill $(ps aux | grep sersync | grep -v grep | awk '{print $2}')[root@nfs_0.14[ /share_data001]# ps aux | grep sersync | grep -v grep | awk '{print $2}'[root@nfs_0.14[ /share_data001]# //没有结果表示已杀掉sersyc的进程//如果需要再次启动即可[root@nfs_0.14[ /]# /usr/local/sersync/sersync2 -dro /usr/local/sersync/confxml.xml 

<<< 左右滑动见更多 >>>


