linux应用之文件实时同步(sersync)

在网上看到了很多关于用rsync实时同步文件的文章,可是有些文章写的不是很详细,所有菜鸟就写了一篇文章,我使用是sersync这个开源的软件,是由金山才子周洋所写,非常感谢他的贡献。具体项目详情请访问:http://code.google.com/p/sersync/  qq群:37499077 欢迎讨论
 
linux下文件实时同步
一、Rsync Remote端的配置

1、首先需要确定你的系统版本是否是2.6(支持inotify)
[root@hxqm tongbu]# uname -a
Linux hxqm 2.6.18-128.el5 #1 SMP Wed Jan 21 10:44:23 EST 2009 i686 i686 i386 GNU/Linux
2、查看是否装了rsync包
[root@hxqm tongbu]# rpm -q rsync
rsync-2.6.8-3.1
如果没有安装就用rpm 或yum装上。
3、新建配置文件/etc/rsyncd.conf,然后编辑/etc/rsyncd.conf,添加一下内容到文件里:
motd file = /etc/rsyncd.motd
uid=root
gid=root
max connections=36000
use chroot=no
log file=/var/log/rsyncd.log
log format =  %t %a %m %f %b
pid file=/var/run/rsyncd.pid
lock file=/var/run/rsyncd.lock
timeout = 300
 
 
[tongbu]
path=/opt/tongbu
list=yes
comment = xiao song tongbu wen jian
ignore errors = yes
read only = no
hosts allow = 192.168.81.0/24
hosts deny = *
auth users test
secrets file = /etc/rsyncd.secrets
具体参数请google一下,这里就不多讲解了。
说明几点:在server配置了一个名为"[tongbu]"的模块,需要同步的目录为/opt/tongbu 访问用的用户名为test
密码存放文件为rsyncd.secrets,值得注意的地方是修改文件的属性为"600",chmod 600 /etc/rsyncd.secrets
4、新建/etc/rsyncd.secrets,文件里的内容格式是:
test:test
注意:第一个字段是用户名,第二个字段是密码,他们之间用":"分开。
5、开启rsync daemon格式
rsync --daemon --config=/etc/rsyncd.conf
6、把rsync启动加到启动项里
echo "rsync --daemon --config=/etc/rsyncd.conf" >> /etc/rc.local
二、Rsync client端配置:
1、首先需要下载sersync最新版本2.5
[root@localhost ~]# wget http://sersync.googlecode.com/files/sersync2.5beta1_32bit_binary.tar.gz
2、解压并编辑confxml.conf文件,confxml.conf是sersync的配置文件,用于指定需要同步的目录及是否刷新cdn接口等,(我只是测试cdn这个没有用到,这个你要是用在生产环境中,可以根据自己的需要来定)
[root@hxqm-tomcat sersysnc2]# cat confxml.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<head version="2.2">
    <host hostip="localhost" port="8008"></host>
    <filter start="false">
        <exclude expression="(.*)\.gz"></exclude>
        <exclude expression="^info/*"></exclude>
    </filter>
    <sersync>
        <localpath watch="/opt/tongbu" debug="false">
            <remote ip="192.168.81.128" name="tongbu"/>
            <!--<remote ip="192.168.8.39" name="tongbu"/>-->
        </localpath>
        <rsyncauth start="flase" users="test" passwordfile="/etc/rsync.password"/>
        <crontab start="false" schedule="600">
            <crontabfilter start="false">
                <exclude expression="*.php"></exclude>
                <exclude expression="info/*"></exclude>
            </crontabfilter>
        </crontab>
        <plugin start="false" name="refreshCDN"/>
    </sersync>
    <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>
    <plugin name="socket">
        <localpath watch="/opt/tongbu">
            <deshost ip="192.168.138.20" port="8009"/>
        </localpath>
    </plugin>
</head>
当前最新版中已经增加了同步过滤,密码验证以及--delete功能,但仍然不支持多目录同步到多目录的操作。
这是confxml.conf文件的内容,解释一下:我使用的用户名是test,密码存放在/etc/rsync.password里。将本机的/opt/tongbu实时目录同步到局域网内IP为192.168.81.128端,定义的模块为"tongbu",并且开启了--delete功能。
4、密码文件的格式介绍
[root@hxqm-tomcat ~]# cat /etc/rsync.password
test#
解释:"test"代表的是Rsync server端的密码,并且要以"#"做结尾。如果有多个密码,增加多行就行。
5、启动实时同步功能(完整同步一次并开启驻留进程)
[root@hxqm-tomcat sersysnc2]#  ./sersync2 -d -r
如果想要了解更多的参数 请使用sersync --help获取帮助信息

这样linux之间的实时文件同步到这里就ok了
 
赶紧测试吧,需要注意一下:
同步是把Rsync client端的文件同步到Rsync Remote端,所以你要是测试的话就在Rsync client 端建立或修改文件,在Rsync server端看效果。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章