rsync+sersync做实时同步时,用于推送文件的服务器运行sersync服务,用于接受文件的服务器则运行rsync守护进程

sersync会利用rsync命令将文件推送到rsync服务器, sersync坐位住服务器,rsync坐位景象u服务器,实现数据同步备份,web镜像等功能
rsync   服务器IP:192.168.1.137
sersync服务器IP:192,168.1.138
使用sersync之前,必须先配置好rsync服务器
配置rsync 接收端
1. 安装rsync
yum install rsync -y
yum install xinetd -y
2. 启动rsync依赖服务并加入到开机启动项
service xinetd start
chkconfig xinetd on
3.配置:
uid = root
gid = root
use chroot = no
max connections = 10
strict modes = yes
port = 873
address = 192.168.1.137
[data] # rsync模块名,后面配置sersync会用到
path = /data  # 该同步目录只要uid所指定的用户有写权限即可
comment = mirror for leonis
ignore errors
read only = on
list = no
auth users = user
secrets file = /etc/rsync.pas # 密码认证文件,必须为600权限,否则rsync传输会报错
host allow = *
#host deny = 0.0.0.0/0
pid file = /var/run/rsync.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsync.log
4.创建同步目录
mkdir /data
5.配置认证文件
echo “user:123456” > /etc/rsync.pas
chmod 666 /etc/rsync.pas
rsync –daemon –config=/etc/rsyncd.conf
6.重启xinetd使其配置生肖
service xinetd restart
7.设置开机启动
echo ”rsync —daemon —config=/etc/rsyncd.conf” >>/etc/rc.local
配置sersync 推送端
安装rsync
vi /etc/xinetd.d/rsync #编辑配置文件,设置开机启动rsync
disable = no #修改为no
创建认证密码文件
touch /etc/rsync.pas
echi “123456” >> /etc/rsync.pas
chomod 600 /etc/rsync.pas
mkdir /data/leonis  #在源服务器上创建测试文件夹,然后在源服务器运行下面2行命令
rsync -avH –port=873 –progress –delete/data/root@192.168.21.127::data –password-file=/etc/rsync.pas
运行完成后,查看接收端服务器,192,168.1.137, 如果在data目录下leonis文件夹存在,说明数据同步正常
安装sersync工具,实时触发rsync进行同步
1.下载sersync源码包
2.创建sersync目录结构
mkdir /usr/local/sersync
mkdir /usr/local/sersync/conf
mkdir /usr/local/sersync/bin
mkdir /usr/local/sersync/log
tar zxvf sersync2.5_64bit_binary_stable_final.tar.gz
cd GNU-Linux-x64
cp confxml.xml /usr/local/sersync/conf
cp sersync2 /usr/local/sersync/bin
3.配置confxml.xml

# cd /usr/local/sersync/conf

# vi confxml.xml

按照注释进行修改

—————————–

<?xml version=”1.0″ encoding=”ISO-8859-1″?>

<head version=”2.5″>

# 设置本地IP和端口

<host hostip=”localhost” port=”8008″></host>

# 开启DUBUG模式

<debug start=”false”/>

# 开启xfs文件系统

<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=”true”/>

<closeWrite start=”true”/>

<moveFrom start=”true”/>

<moveTo start=”true”/>

<attrib start=”true”/>

<modify start=”true”/>

</inotify>

<sersync>

# 本地同步的目录路径

<localpath watch=”/data”>

# 远程IP和rsync模块名

<remote ip=”192.168.100.29″ name=”data”/>

<!–<remote ip=”192.168.8.39″ name=”tongbu”/>–>

<!–<remote ip=”192.168.8.40″ name=”tongbu”/>–>

</localpath>

<rsync>

# rsync指令参数

<commonParams params=”-auvzP”/>

# rsync同步认证

<auth start=”true” users=”user” passwordfile=”/etc/rsync.pas”/>

# 设置rsync远程服务端口,远程非默认端口则需打开自定义

<userDefinedPort start=”false” port=”874″/><!– port=874 –>

# 设置超时时间

<timeout start=”true” time=”100″/><!– timeout=100 –>

# 设置rsync+ssh加密传输模式,默认关闭,开启需设置SSH加密证书

<ssh start=”false”/>

</rsync>

# sersync传输失败日志脚本路径,每隔60会重新执行该脚本,执行完毕会自动清空。

<failLog path=”/usr/local/sersync/log/rsync_fail_log.sh” timeToExecute=”60″/><!–default every 60mins execute once–>

# 设置rsync+crontab定时传输,默认关闭

<crontab start=”false” schedule=”600″><!–600mins–>

<crontabfilter start=”false”>

<exclude expression=”*.php”></exclude>

<exclude expression=”info/*”></exclude>

</crontabfilter>

</crontab>

# 设置sersync传输后调用name指定的插件脚本,默认关闭

<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>

4.创建推送端sersync同步目录
# mkdir /data
5.设置环境变量:

# echo “export PATH=$PATH:/usr/local/sersync/bin/” >> /etc/profile

# source /etc/profile

6.启动sersync

# sersync2 -r -d -o /usr/local/sersync/conf/confxml.xml

注:重启操作如下:

# killall sersync2 && sersync2 -r -d -o /usr/local/sersync/conf/confxml.xml

7.设置开机启动

# echo “sersync2 -r -d -o /usr/local/sersync/conf/confxml.xml” >> /etc/rc.local