张金芳 發表於 2019-8-19 19:21:00

在中标麒麟操作系统下实现async+inotify 数据同步

<p><strong>一、背景</strong></p>
<p>我们为轻轨售检票系统搭建高可用服务,系统用的是中标麒麟7.2,HA软件也是用麒麟的,不要问我为啥选这么奇葩的方案😂。</p>
<p><span class="fontstyle0">我要实现的是正常工作状态下,将主机的数据同步到备机上,在主机宕机时,备机操作主机备份过来的数据,当主机恢复时,数据会同步回主机。</span>中标麒麟的HA 不具备数据同步功能,问售后有没有同步数据的解决方案,得到的答案是用<span class="fontstyle0">DRBD 可以解决,但是他们没有把握可以实现。既然他们都没有把握我还是自己想办法把。在网上查了一下数据同步的方法,觉得实现起来并没有那么复杂,下面我就将我实现的过程和使用起来感受整理一下,供大家参考。</span></p>
<p><strong>二、实现</strong></p>
<p><span class="fontstyle0">网上用async + inotify 实现数据同步的例子还是很多的。我是参照这个连接实现的:https://cloud.tencent.com/developer/article/1333909。</span></p>
<p><span class="md-plain">inotify 是一种强大的、细粒度的、异步的文件系统事件控制机制,</span><span class="md-plain md-expand">linux 内核从 2.6.13 起,加入了 inotify 支持,通过 inotify 可以监控文件系统中添加、删除、修改、移动等各种事件,利用这个内核接口,第三方软件就可以监控文件系统下文件的各种变化情况,而 inotify-tools 正是实施监控的软件。</span></p>
<p class="md-end-block md-p"><span class="md-plain md-expand">我的两台服务器情况:</span></p>
<table style="height: 88px; width: 1072px" border="0">
<tbody>
<tr>
<td><span class="md-plain md-expand">服务器名称</span></td>
<td>&nbsp;<span class="md-plain">IP-addr<span class="md-tab">&nbsp;</span></span></td>
<td>status<span class="md-tab">&nbsp;</span></td>
<td>工具安装<span class="md-tab">&nbsp;</span></td>
<td>系统版本</td>
<td>&nbsp;<span class="md-plain">操作目录</span></td>
</tr>
<tr>
<td>数据服务器</td>
<td>192.168.10.62</td>
<td>client</td>
<td>rsync、inotify-tools</td>
<td>NeoKylinOS-V7.2(类似Centos7.2)</td>
<td>/opt/ftp</td>
</tr>
<tr>
<td>备份服务器</td>
<td>192.168.10.52</td>
<td>server</td>
<td>rsync、inotify-tools</td>
<td>NeoKylinOS-V7.2(类似Centos7.2)</td>
<td>/opt/ftp</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
</tbody>
</table>
<p class="md-end-block md-p"><strong><span class="fontstyle0">安装和配置环境</span></strong></p>
<p><span class="fontstyle0">async 这个功能是 linux系统默认安装的(只要linux内核不是很旧)i</span>notify是需要安装的,具体的实现过程请参照:https://cloud.tencent.com/developer/article/1333909。我就不写重复的东西了。</p>
<p><strong>编写inotify 脚本</strong></p>
<p>大家在自己实现inotify 同步的时候肯会在自己的环境中遇到不一样的问题,在我的环境中遇到的主要是 同步数据的命令,以及inotify 脚本的编写。下面我把我自己的inotify 脚本贴出供大家参考。</p>
<p>制作inotify脚本,注册inofity为服务。写一个 inotify 脚本放到/etc/rc.d/init.d/ 目录下,/etc/rc.d/init.d/inotify start ,启动后,可以用systemctl start inotify.service 启动。inotify 脚本如下:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">!/bin/bash</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)">chkconfig: 2345 38 46</span>
. /etc/init.d/<span style="color: rgba(0, 0, 0, 1)">functions

</span><span style="color: rgba(0, 0, 255, 1)">if</span> [ $<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> -ne 1 ];then</span>
usage: <span style="color: rgba(128, 0, 128, 1)">$0</span> |start|stop|<span style="color: rgba(0, 0, 0, 1)">
exit </span>1<span style="color: rgba(0, 0, 0, 1)">
fi

case </span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">$1</span><span style="color: rgba(128, 0, 0, 1)">"</span> <span style="color: rgba(0, 0, 255, 1)">in</span><span style="color: rgba(0, 0, 0, 1)">
start)
</span>/bin/sh /etc/rsyncd/inotify.sh &amp;<span style="color: rgba(0, 0, 0, 1)">
echo </span>$$ &gt; /var/run/<span style="color: rgba(0, 0, 0, 1)">inotify.pid
</span><span style="color: rgba(0, 0, 255, 1)">if</span> [ `ps -ef | grep inotify | grep -v grep | wc -l` <span style="color: rgba(0, 128, 128, 1)">-gt</span> 2<span style="color: rgba(0, 0, 0, 1)"> ];then
action </span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">inotify service is started</span><span style="color: rgba(128, 0, 0, 1)">"</span> /bin/<span style="color: rgba(0, 0, 255, 1)">true</span>
<span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)">
action </span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">inotify service is started</span><span style="color: rgba(128, 0, 0, 1)">"</span> /bin/<span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">
fi
;;
stop)
kill </span>-9 `cat /var/run/inotify.pid` &gt; /dev/null 2&gt;&amp;1<span style="color: rgba(0, 0, 0, 1)">
pkill inotifywait
sleep </span>1
<span style="color: rgba(0, 0, 255, 1)">if</span> [ `ps -ef | grep inotify | grep -v grep | wc -l` <span style="color: rgba(0, 128, 128, 1)">-eq</span> 0<span style="color: rgba(0, 0, 0, 1)"> ];then
action </span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">inotify service is stopped</span><span style="color: rgba(128, 0, 0, 1)">"</span> /bin/<span style="color: rgba(0, 0, 255, 1)">true</span>
<span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)">
action </span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">inotify service is stopped</span><span style="color: rgba(128, 0, 0, 1)">"</span> /bin/<span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">
fi
;;
</span>*<span style="color: rgba(0, 0, 0, 1)">)
usage: </span><span style="color: rgba(128, 0, 128, 1)">$0</span> |start|stop|<span style="color: rgba(0, 0, 0, 1)">
exit </span>1<span style="color: rgba(0, 0, 0, 1)">
esac</span></pre>
</div>
<p>inotify的脚本/etc/rsyncd/inotify.sh,创建inotify 脚本:</p>
<p># vim /etc/rsyncd/inotify.sh</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">!/bin/bashq</span>
host=192.168.100.22 <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">server的ip(备份服务器)</span>
src=/opt/ftp/ <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">所要监控的备份目录(此处可以自定义,但是要保证存在)</span>
des=ftp <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">自定义的模块名,需要与client端定义的一致</span>
password=/etc/rsyncd/rsync.password <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">密码文件</span>
user=root <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">用户名(一个实际存在的账号)</span>
inotify=/usr/local/inotify <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">inotify的安装目录</span>

<span style="color: rgba(128, 0, 128, 1)">${inotify}</span>/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f%e' -e modify,delete,create,attrib <span style="color: rgba(128, 0, 128, 1)">$src</span><span style="color: rgba(0, 0, 0, 1)"> \
</span>| <span style="color: rgba(0, 0, 255, 1)">while</span><span style="color: rgba(0, 0, 0, 1)"> read files
</span><span style="color: rgba(0, 0, 255, 1)">do</span><span style="color: rgba(0, 0, 0, 1)">
rsync </span>-avzP --delete --timeout=100 --password<span style="color: rgba(0, 128, 128, 1)">-file</span>=<span style="color: rgba(128, 0, 128, 1)">${password}</span> <span style="color: rgba(128, 0, 128, 1)">$src</span> <span style="color: rgba(128, 0, 128, 1)">$user</span>@<span style="color: rgba(128, 0, 128, 1)">$host</span>::<span style="color: rgba(128, 0, 128, 1)">$des</span><span style="color: rgba(0, 0, 0, 1)">
echo </span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">${files} was rsynced</span><span style="color: rgba(128, 0, 0, 1)">"</span> &gt;&gt;/tmp/rsync.log 2&gt;&amp;1<span style="color: rgba(0, 0, 0, 1)">
done</span></pre>
</div>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p><span class="fontstyle0"><span class="md-plain md-expand"><span class="md-softbreak"> <span class="md-plain"><br></span></span></span></span></p>
<p>&nbsp;</p>
<p><span class="fontstyle0"><br>&nbsp;</span></p><br><br>
来源:https://www.cnblogs.com/mingjie-c/p/11379096.html
頁: [1]
查看完整版本: 在中标麒麟操作系统下实现async+inotify 数据同步