sersync2 实时同步配置
自动同步文件或者是目录
实现的方式:
在同步服务器上开启sersync,将监控路径中的文件同步到目标服务器,因此需要在同步服务器配置sersync,在同步目标服务器配置rsync
实验机器的地址分配:
Rsync 服务器: 192.168.1.112
Sersync2 服务器:192.168.1.110
系统环境
[root@liyao home]# uname -a
Linux liyao 2.6.18-194.el5 #1 SMP Fri Apr 2 14:58:35 EDT 2010 i686 i686 i386 GNU/Linux [root@liyao home]#一、 rsync 服务器的配置
1. 先查看下是否安装了 rsync 软件包
[root@liyao ~]# rpm -qa | grep rsync
rsync-2.6.8-3.1
[root@liyao ~]#
2. 如果系统默认安装了 rsync 软件包,我们可以使用 yum 命令将其卸载
[root@liyao ~]# yum remove -y rsync
Loaded plugins: fastestmirror
Setting up Remove Process
Resolving Dependencies
--> Running transaction check
---> Package rsync.i386 0:2.6.8-3.1 set to be erased
--> Finished Dependency Resolution
Dependencies Resolved
================================================================================
Package Arch Version Repository Size
================================================================================
Removing:
rsync i386 2.6.8-3.1 installed 392 k
Transaction Summary
================================================================================
Remove 1 Package(s)
Reinstall 0 Package(s)
Downgrade 0 Package(s)
Downloading Packages:
Running rpm_check_debug
Running Transaction Test
Finished Transaction Test
Transaction Test Succeeded
Running Transaction
Erasing : rsync 1/1
Removed:
rsync.i386 0:2.6.8-3.1
Complete!
[root@liyao ~]#
3. 下载 rsync 3.0 的源码包
[root@liyao~]#wget-chttp://rsync.samba.org/ftp/rsync/rsync-3.0.8.tar.gz
--2011-08-20 08:05:05-- http://rsync.samba.org/ftp/rsync/rsync-3.0.8.tar.gz
Resolving rsync.samba.org... 216.83.154.106, 2001:470:1f05:1a07::1
Connecting to rsync.samba.org|216.83.154.106|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 790722 (772K) [application/x-gzip]
Saving to: `rsync-3.0.8.tar.gz'
100%[======================================>] 790,722 153K/s in 5.7s
2011-08-20 08:05:11 (135 KB/s) - `rsync-3.0.8.tar.gz' saved [790722/790722]
4. 将该软件包解压缩,并且进行编译安装
[root@liyao ~]# tar zxf rsync-3.0.8.tar.gz
[root@liyao ~]# cd rsync-3.0.8
[root@liyao rsync-3.0.8]# ./configure --prefix=/usr/ && make && make install
5. 建立 rsync 服务器的配置文件
uid = nobody
gid = nobody
use chroot = no
strict modes = yes
max connections = 200
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
[test]
path = /home/
comment = test
read only = no
write only = no
ignore errors = yes
list = yes
uid = root
gid = root
6. 启动 rsync 的服务
[root@liyao ~]# rsync --daemon
[root@liyao ~]# netstat -npl | grep 873
tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 7731/rsync
tcp 0 0 :::873 :::* LISTEN 7731/rsync
二、 配置Sersync2 服务器
二进制直接可运行
wget
1、将该软件包下载下来之后解压缩,并进行安装
[root@liyao ~]# tar zxf sersync2.5_32bit_binary_stable_final.tar.gz
[root@liyao ~]# ls
anaconda-ks.cfg install.log.syslog
GNU-Linux-x86 sersync2.5_32bit_binary_stable_final.tar.gz
install.log
[root@liyao ~]# cd GNU-Linux-x86/
[root@liyao GNU-Linux-x86]# ls
confxml.xml sersync2
[root@liyao GNU-Linux-x86]# mkdir /etc/sersync2
[root@liyao GNU-Linux-x86]# cp confxml.xml /etc/sersync2/
[root@liyao GNU-Linux-x86]# cp sersync2 /usr/bin/
# 安装该软件只要把软件中的文件拷贝到相应的目录即可
2. 修改 Sersync2 的配置文件
[root@liyao ~]# cat /etc/sersync2/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="false"/>
<modify start="false"/>
</inotify>
<sersync>
<localpath watch="/home">
<remote ip="192.168.1.112" name="test"/>
# 指定 rsyncd 服务器的地址和备份的模块名称
<!--<remote ip="192.168.8.39" name="tongbu"/>-->
<!--<remote ip="192.168.8.40" name="tongbu"/>-->
</localpath>
<rsync>
<commonParams params="-artuz"/>
<auth start="false" users="root" passwordfile="/etc/rsync.pas"/>
<userDefinedPort start="false" port="874"/><!-- port=874 -->
<timeout start="false" time="100"/><!-- timeout=100 -->
<ssh start="false"/>
</rsync>
<failLog path="/var/log/rsync_fail.log" 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>
[root@liyao ~]#
3. 我们来运行如下的备份命令
在运行备份命令之前我们先看下 /home 这个目录下面有文件或者是目录
[root@liyao home]# ls -l
total 4
drwxr-xr-x 2 root root 4096 Aug 20 08:51 liyao
[root@liyao home]#
[root@liyao ~]# sersync2 -r -d -o /etc/sersync2/confxml.xml
set the system param
execute:echo 50000000 > /proc/sys/fs/inotify/max_user_watches
execute:echo 327679 > /proc/sys/fs/inotify/max_queued_events
parse the command param
option: -r rsync all the local files to the remote servers before the sersync work
option: -d run as a daemon
option: -o config xml name: /etc/sersync2/confxml.xml
daemon thread num: 10
parse xml config file
host ip : localhost host port: 8008
daemon start,sersync run behind the console
config xml parse success
please set /etc/rsyncd.conf max connections=0 Manually
sersync working thread 12 = 1(primary thread) + 1(fail retry thread) + 10(daemon sub threads)
Max threads numbers is: 22 = 12(Thread pool nums) + 10(Sub threads)
please according your cpu ,use -n param to adjust the cpu rate
------------------------------------------
rsync the directory recursivly to the remote servers once
working please wait...
execute command: cd /home && rsync -artuz -R --delete ./ 192.168.1.112::test >/dev/null 2>&1
run the sersync:
watch path is: /home
4. 我们到 rsyncd 服务器(1.112)的 /home 目录下看看是否有我在 1.110 /home/liyao 这个目录
[root@liyao home]# ls
liyao
[root@liyao home]#
那我们在从 1.110 的 /home 目录下创建 abc 目录,不用执行任何命令他会自动同步到 1.112 上
[root@liyao home]# mkdir abc
[root@liyao home]# ls
abc liyao
[root@liyao home]#
我们在去 1.112 上去看下,abc 目录已经同步过来了
[root@liyao home]# ll
total 8
drwxr-xr-x 2 root root 4096 Aug 21 04:12 abc
drwxr-xr-x 2 root root 4096 Aug 21 04:05 liyao
[root@liyao home]#
OK ,这样我们就大功告成了。