为什么写这个
对于服务器之间的文件,如果想做同步,可以采用sersync+rsync+inotify组合的方式来做到主从方式的自动同步。最近恰好也是用到这个,所以在此简单记录一下。
模型
服务器:数据从83—>84 和85
10.10.10.84(从)
10.10.10.85(从)
10.10.10.83(主)
安装与配置sersync和rsync(从)
在Ubuntu server上,rsync和inotify默认都是安装了的。配置以xinetd方式运行。
(1)安装命令
apt-get install rsync xinetd
(2)创建存放文件目录,命令如下:
mkdir /home/tfxiaozi/webapps
(3)修改rsync启动方式
vim /etc/default/rsync
(4)修改下面这行
RSYNC_ENABLE=inetd
(5)rsync启动方式的配置文件
vim /etc/xinetd.d/rsync,加入下面内容
service rsync
{
disable = no
wait = no
socket_type = stream
user = root
server = /usr/bin/rsync
server_args = --daemon
log_on_failure += USERID
}
(6)rsync的配置文件
vim /etc/rsyncd.conf加入下面内容
max connections = 65535
log file = /var/log/rsync.log
timeout = 300
[webapps]
path = /home/tfxiaozi/webapps
read only = no
list = yes
uid = root
gid = root
auth users = test
secrets file = /etc/rsyncd.secrets
用户组最好设置为root,不然文件权限同步过去的时候会变化。
(7)创建rsync传输过程中的认证文件
vim /etc/rsyncd.secrets,内容如下,格式:用户名:密码
test:123xcvz
然后,将认证文件权限修改成
chmod 600 /etc/rsyncd.secrets
(8)重启xinetd
/etc/init.d/xinetd restart
查看rsyncserver是否启动
netstat -ntpl|grep 873
tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 24651/xinetd
(9)测试:在83机器上执行
root@tfxiaozi-server:~# rsync test@10.10.10.85::webapps
pssword:
drwxrwxr-x 4,096 2016/06/14 09:45:57 .
【注】84和85两台机器的配置相同。
配置主服务器(源)
10.10.10.83服务器
sersync下载地址
https://code.google.com/archive/p/sersync/downloads
http://sersync.sourceforge.net/
解压:
tar -zxvf sersync2.5.4_64bit_binary_stable_final.tar.gz
二进制文件直接放在/usr/local/目录下即可。
root@tfxiaozi-server:/usr/local/sersync2.5.4# ll
total 1784
drwxr-xr-x 2 root root 4096 Jun 14 10:02 ./
drwxr-xr-x 13 root root 4096 May 26 20:45 ../
-rwx------ 1 root root 2505 Jun 14 10:02 confxml.xml*
-rwxrwxrwx 1 root root 0 Jun 14 13:02 rsync_fail_log.sh*
-rw-r--r-- 1 root root 1 May 27 04:11 rsync.log
-rwx------ 1 root root 1810128 May 26 20:25 sersync2*
修改sersync的配置文件
vi 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"/>
<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="false"/>
<closeWrite start="true"/>
<moveFrom start="true"/>
<moveTo start="true"/>
<attrib start="true"/>
<modify start="true"/>
</inotify>
<sersync>
<localpath watch="/home/tfxiaozi/webapps">
<remote ip="10.10.10.84" name="webapps"/>
<remote ip="10.10.10.85" name="webapps"/>
</localpath>
<rsync>
<commonParams params="-auvPz"/>
***<auth start="true" users="test" passwordfile="/etc/password.rsync"/>***
<userDefinedPort start="false" port="874"/><!-- port=874 -->
<timeout start="true" time="100"/><!-- timeout=100 -->
<ssh start="false"/>
</rsync>
<failLog path="/usr/local/sersync2.5.4/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>
创建密码文件:
vi /etc/password.rsync,输入123xcvz。这个密码和上面第(7)步是一样的,但是不加用户名,密码文件权限要设置为600。
到这里就配置完毕了。
测试
/usr/local/sersync2.5.4/sersync2 -r -d -o /usr/local/sersync2.5.4/confxml.xml
在10.10.10.83的/home/tfxiaozi/webapps目录下新建个文件1.txt,则在10.10.10.84和10.10.10.85的/home/tfxiaozi/webapps下会自动同步刚才的1.txt到本地。