<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[Young for you]]></title> 
<link>179401.cn/index.php</link> 
<description><![CDATA[吃好喝好！喝好吃好]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[Young for you]]></copyright>
<item>
<link>179401.cn/read.php?14</link>
<title><![CDATA[csync2+inotify实现文件实时同步]]></title> 
<author> &lt;&gt;</author>
<category><![CDATA[存储]]></category>
<pubDate>Thu, 09 Jul 2009 12:53:16 +0000</pubDate> 
<guid>179401.cn/read.php?14</guid> 
<description>
<![CDATA[ 
	<span style="font-size: 18px;">注：本文参考资料来源于<a href="http://www.linuxtone.org" target="_blank">http://www.linuxtone.org</a>，作者:NetSeek，这里是原pdf文档：</span><br/><a href="http://179401.cn/file/csync2+inotify.pdf">点击这里下载文件</a><br/><br/><span style="font-size: 18px;">一、关键技术</span><br/><br/>利用csync2+sqlite实现数据的高效实时的增量备份，相关目录及文件信息都存储在sqlite数据库中,<br/>大大提升了同步的速率和效率;利用inotify触发式（实时）执行csync同步命令更新文件。<br/><br/><span style="font-size: 18px;">二、方案适用</span><br/><br/>图片服务器集群方案: ReiserFS4+csync2+inotify<br/>网站服务程序分布式方案：csync2+inotify+NFS<br/>[注]（如,网站程序中存在缓存目录则排除此目录同步，将缓存目录挂载在NFS上）<br/>分布式集群软件配置更新同步方案: csync2+inotify<br/>[注](如,集群中heartbeat相关的配置文件，如apache,nginx的配置文件，并能执行相关脚本)<br/><br/><span style="font-size: 18px;">三、实际配置过程</span><br/><br/>系统：rhel5.3-x86_64<br/>结构：两台fileserver<br/><div class="quote"><div class="quote-title">引用</div><div class="quote-content"># vi /etc/hosts<br/>node1 192.168.10.50<br/>node2 192.168.10.51</div></div><br/><br/>1、创建下载文件列表<br/><div class="quote"><div class="quote-title">引用</div><div class="quote-content">cd /usr/local/src/<br/>vi list //以下内容：<br/>ftp://ftp.gnupg.org/gcrypt/libgpg-error/libgpg-error-1.7.tar.gz<br/>ftp://ftp.gnupg.org/gcrypt/libgcrypt/libgcrypt-1.4.4.tar.gz<br/>ftp://ftp.gnutls.org/pub/gnutls/libtasn1/libtasn1-2.1.tar.gz<br/>http://www.sqlite.org/sqlite-2.8.17.tar.gz<br/>http://internode.dl.sourceforge.net/sourceforge/librsync/librsync-0.9.7.tar.gz<br/>ftp://ftp.gnu.org/pub/gnu/gnutls/gnutls-2.6.6.tar.bz2<br/>http://oss.linbit.com/csync2/csync2-1.34.tar.gz<br/>http://jaist.dl.sourceforge.net/sourceforge/inotify-tools/inotify-tools-3.13.tar.gz</div></div><br/>2、创建安装脚本并执行<br/><div class="quote"><div class="quote-title">引用</div><div class="quote-content">vi install.sh //以下内容：<br/>wget –i list<br/>cd /usr/local/src/<br/>tar zxvf libgpg-error-1.7.tar.gz<br/>cd libgpg-error-1.7<br/>./configure && make && make install<br/>cd ..<br/>tar zxvf libgcrypt-1.4.4.tar.gz<br/>cd libgcrypt-1.4.4<br/>./configure --with-gpg-error-prefix=/usr/local && make && make install<br/>cd ..<br/>tar zxvf libtasn1-2.1.tar.gz<br/>cd libtasn1-2.1<br/>./configure && make && make install<br/>cd ..<br/>tar zxvf sqlite-2.8.17.tar.gz<br/>cd sqlite-2.8.17<br/>./configure && make && make install<br/>cd ..<br/>tar zxvf librsync-0.9.7.tar.gz<br/>cd librsync-0.9.7<br/>./configure && make && make install<br/>cd ..<br/>tar jxvf gnutls-2.6.6.tar.bz2<br/>cd gnutls-2.6.6<br/>./configure --with-libgcrypt-prefix=/usr/local/ && make && make install<br/>cd ..<br/>echo "/usr/local/lib" >> /etc/ld.so.conf<br/>ldconfig<br/>tar zxvf csync2-1.34.tar.gz<br/>cd csync2-1.34<br/>./configure && make && make install && make cert<br/>cd ..<br/>tar zxvf inotify-tools-3.13.tar.gz<br/>cd inotify-tools-3.13<br/>./configure && make && make install<br/>cd ..<br/>echo "csync2 30865/tcp" >> /etc/services<br/>echo "service csync2<br/>&#123;<br/>disable = no<br/>protocol = tcp<br/>flags = REUSE<br/>socket_type = stream<br/>wait = no<br/>user = root<br/>group = root<br/>server = /usr/local/sbin/csync2<br/>server_args = -i<br/>#only_from = 192.168.10.50 192.168.10.51<br/>&#125;" > /etc/xinetd.d/csync2<br/>chkconfig xinetd on</div></div><br/>3、赋予脚本可执行权限并执行：<br/><div class="quote"><div class="quote-title">引用</div><div class="quote-content">chmod 755 install.sh<br/>./install.sh</div></div><br/><span style="color: #FF0000;">注：需要在每个节点执行脚本</span><br/><br/>4、编辑hosts文件如下内容：<br/><div class="quote"><div class="quote-title">引用</div><div class="quote-content">vi /etc/hosts<br/>192.168.10.50 node1<br/>192.168.10.51 node2</div></div><br/>5、生成密钥并分发到每个节点：<br/><div class="quote"><div class="quote-title">引用</div><div class="quote-content">csync2 -k /etc/test.key<br/>scp /etc/test.key root@node2:/etc/</div></div><br/>6、创建备份目录：<br/><div class="quote"><div class="quote-title">引用</div><div class="quote-content">mkdir /var/csync_backup </div></div><br/><span style="color: #FF0000;">注：用于防止修改错误恢复，如果启用了，分别在其他节点建立相应的目录。</span><br/><br/>7、修改csync2配置文件（所有节点相同）：<br/><div class="quote"><div class="quote-title">引用</div><div class="quote-content">vi /etc/csync2.cfg //以下内容：<br/>group testgroup<br/>&#123;<br/>host node1 node2; //定义组成员<br/>key /etc/test.key; //制定密钥<br/>include /var/www/html; //需要同步的目录<br/>exclude /var/www/html/temp; //需要排除的目录。<br/>exclude /var/www/html/templates_; //需要排除同步的缓存目录。<br/>exclude *~ .*; //排除同步.开头的文件<br/>backup-directory /var/csync_backup; //防错备份目录，根据自己的需求设置。<br/>backup-generations 3;<br/>auto younger; //同步以最新的文件为标准更新.<br/>&#125;</div></div><br/>启动服务：<br/><div class="quote"><div class="quote-title">引用</div><div class="quote-content">/etc/init.d/xinetd start</div></div><br/>注：csync2相关测试命令<br/><div class="quote"><div class="quote-title">引用</div><div class="quote-content"><span style="color: #FF0000;">csync2 –vvv –T&nbsp;&nbsp;测试csync配置是否正确，可以看到相关SQL执行过程<br/>csync2 –xv&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 执行同步命令<br/>csync2 –xvvv&nbsp;&nbsp;&nbsp;&nbsp; 执行同步命令，并显示出详细的信息</span></div></div><br/>8、配置inotify触发同步脚本（所有节点相同）:<br/><div class="quote"><div class="quote-title">引用</div><div class="quote-content">vi /usr/local/sbin/csync2_test.sh //以下内容：<br/>#!/bin/bash<br/>src=/var/www/html<br/>/usr/local/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' &#92;<br/>--exclude "&#92;.swp$" &#92;<br/>-e close_write,modify,delete,create,attrib,move &#92;<br/>$&#123;src&#125; &#92;<br/>&#124; while read file<br/>do<br/>csync2 -x >/dev/null 2>&1<br/>done</div></div><br/>后台执行脚本：<br/><div class="quote"><div class="quote-title">引用</div><div class="quote-content">chmod a+x /usr/local/sbin/csync2_test.sh<br/>csync2_test.sh &</div></div><br/><span style="color: #FF0000;">注：64位OS可能需要做一条软链接</span><br/><div class="quote"><div class="quote-title">引用</div><div class="quote-content">ln -s /usr/local/lib/libinotifytools.so.0 /usr/lib64/libinotifytools.so.0</div></div><br/>9、测试文件同步：<br/>在任一节点上创建，修改，移动，或者删除文件或目录，在其他节点检查是否同步，如果没有同步，请使用csync2的相关测试命令测试同步是否正常。<br/>Tags - <a href="179401.cn/tag.php?tag=csync" rel="tag">csync</a> , <a href="179401.cn/tag.php?tag=inotify" rel="tag">inotify</a> , <a href="179401.cn/tag.php?tag=%25E6%2596%2587%25E4%25BB%25B6%25E5%2590%258C%25E6%25AD%25A5" rel="tag">文件同步</a>
]]>
</description>
</item>
</channel>
</rss>