在網上看到了很多關於用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
[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
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
說明幾點:在server配置了一個名爲"[tongbu]"的模塊,需要同步的目錄爲/opt/tongbu 訪問用的用戶名爲test
密碼存放文件爲rsyncd.secrets,值得注意的地方是修改文件的屬性爲"600",chmod 600 /etc/rsyncd.secrets
4、新建/etc/rsyncd.secrets,文件裏的內容格式是:
test:test
注意:第一個字段是用戶名,第二個字段是密碼,他們之間用":"分開。
test:test
注意:第一個字段是用戶名,第二個字段是密碼,他們之間用":"分開。
5、開啓rsync daemon格式
rsync --daemon --config=/etc/rsyncd.conf
rsync --daemon --config=/etc/rsyncd.conf
6、把rsync啓動加到啓動項裏
echo "rsync --daemon --config=/etc/rsyncd.conf" >> /etc/rc.local
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
[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>
[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端的密碼,並且要以"#"做結尾。如果有多個密碼,增加多行就行。
test#
解釋:"test"代表的是Rsync server端的密碼,並且要以"#"做結尾。如果有多個密碼,增加多行就行。
5、啓動實時同步功能(完整同步一次並開啓駐留進程)
[root@hxqm-tomcat sersysnc2]# ./sersync2 -d -r
[root@hxqm-tomcat sersysnc2]# ./sersync2 -d -r
如果想要了解更多的參數 請使用sersync --help獲取幫助信息
這樣linux之間的實時文件同步到這裏就ok了
趕緊測試吧,需要注意一下:
同步是把Rsync client端的文件同步到Rsync Remote端,所以你要是測試的話就在Rsync client 端建立或修改文件,在Rsync server端看效果。