sersync的安装流程:
一、首先实现rsync,增量同步
实验环境:
客户端:server1 :10.2.22.46 (需要备份的服务器)
服务端:server2:10.2.22.229 (需要存储的备份服务器)
1.服务端安装:
1)安装 rsync
yum install -y rsync
2)编辑配置文件
# /etc/rsyncd: configuration file for rsync daemon mode
# See rsyncd.conf man page for more options.
# configuration example:
# uid = nobody
# gid = nobody
# use chroot = yes
# max connections = 4
# pid file = /var/run/rsyncd.pid
# exclude = lost+found/
# transfer logging = yes
# timeout = 900
# ignore nonreadable = yes
# dont compress = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2
# [ftp]
# path = /home/ftp
# comment = ftp export area
uid = root 用于同步的用户
gid = root 用于同步的用户
use chroot = no 是否切换到root用户再执行
max connections = 200 并发同步的线程数
timeout = 300 超时时间,多长时间不进行备份就自动断开
pid file = /var/run/rsyncd.pid 进程对应进程号文件(存放服务运行时进程id号)
lock file = /var/run/rsync.lock 锁文件
log file = /var/log/rsyncd.log 日志文件
ignore errors
read only = false
list = false 是否可以列表
hosts allow = 10.2.22.0/24 准许访问rsync服务器客户范围(白名单)
hosts deny = 0.0.0.0/32 禁止访问rsync服务器客户范围(黑名单)
auth users = rsync_backup 虚拟认证用户,任意名称都可以,在/etc/passwd文件中无法找 到
secrets file = /etc/rsync.password 密码文件
fake super = yes 它不需要daemon以root运行,就可以存储文件的完整属性。
[backup] 备份的模块
comment = "backup dir by clsn"
path = /backup 备份存储的路径
3)创建同步的用户和备份的目录并授权
此块使用的是root用户,因此创建用户步骤省略
mkdir /backup/
4)创建认证用户密码文件
echo "123456" >>/etc/rsync.password
chmod 600 /etc/rsync.password
5)启动服务
rsync --daemon
6)查看服务是否启动
root 27575 1 0 11:22 ? 00:00:00 rsync --daemon
2)客户端安装:
1)安装 rsync
yum install -y rsync
2)创建密码文件,客户端的认证文件只需要有密码即可
echo "123456" >>/etc/rsync.password
chmod 600 /etc/rsync.password
3)数据传输测试:
rsync -avz /etc/hosts rsync_backup@10.2.22.229::backup --password-file=/etc/rsync.password
二:配置实时同步,使用的软件为开源的sersync
1.下载地址
cd /usr/local/src/
wget https://storage.googleapis.com/google-code-archive-downloads/v2/code.google.com/sersync/sersync2.5.4_64bit_binary_stable_final.tar.gz
2.解压软件包
tar zxvf sersync2.5.4_64bit_binary_stable_final.tar.gz
3.创建目录结构
mkdir /usr/local/sersync
mkdir /usr/local/sersync/conf
mkdir /usr/local/sersync/logs
mkdir /usr/local/sersync/bin
4.移动文件
mv GNU-Linux-x86/sersync2 /usr/local/sersync/bin/
mv GNU-Linux-x86/confxml.xml /usr/local/sersync/conf
5.配置环境变量
vim /etc/profile.d/sersync.sh
echo "PATH=$PATH:/usr/local/sersync" > /etc/profile.d/sersync.sh
source /etc/profile.d/sersync.sh
6.配置
cd /usr/local/sersync/conf
cp confxml.xml confxml.xml.bak
*** 配置文件的说明****
======================================================================================================================================================================
vim confxml.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<head version="2.5">
<host hostip="localhost" port="8008"></host> # 设置本地IP和端口
<debug start="false"/> # 是否开启调试模式,下面所有出现false和true的地方都分别表示关闭和开启的开关
<fileSystem xfs="false"/> # 监控的是否是xfs文件系统
<filter start="false"> # 是否启用监控的筛选功能,筛选的文件将不被监控,默认关闭
<exclude expression="(.*).svn"></exclude>
<exclude expression="(.*).gz"></exclude>
<exclude expression="^info/*"></exclude>
<exclude expression="^static/*"></exclude>
</filter>
<inotify> # 设置监控的事件,默认监控的是delete/close_write/moved_from/moved_to/create folder
<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> # rsync命令的配置段
<localpath watch="/data/sp"> # 本地监控同步的目录或文件,同inotify+rsync一样,建议同步目录
<remote ip="192.168.66.153" name="sp"/> # 目标地址和rsync daemon的模块名,所以远端要以daemon模式先运行好rsync
<!--remote ip="IPADDR" name="module"--> # 除非下面开启了ssh start,此时name为远程shell方式运行时的目标目录
</localpath>
<rsync> # 指定rsync选项
<commonParams params="-auvrtzopgP"/>
<auth start="true" users="rsync" passwordfile="/etc/rsync_client.pwd"/> #rsync同步认证
<userDefinedPort start="false" port="874"/><!-- port=874 --> # 设置rsync远程服务端口,非默认端口需要打开自定义(若开启rsync+ssh, 则这里需定义SSH端口)
<timeout start="true" time="100"/><!-- timeout=100 --> # 设置超时时间
<ssh start="false"/> # 是否使用远程shell模式而非rsync daemon运行rsync命令
</rsync>
<failLog path="/tmp/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once--> # 错误重传。sersync传输失败日志脚本路径,每隔60会重新执行该脚本,执行完毕会自动清空。
<crontab start="false" schedule="600"><!--600mins--> # 是否开启crontab功能,默认关闭
<crontabfilter start="false"> # crontab定时传输的筛选功能
<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@im conf]# cat 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="/application/tomcat/bin/c:">
<remote ip="10.2.22.229" name="backup"/>
<!--<remote ip="192.168.8.39" name="tongbu"/>-->
<!--<remote ip="192.168.8.40" name="tongbu"/>-->
</localpath>
<rsync>
<commonParams params="-artuz"/>
<auth start="true" users="rsync_backup" passwordfile="/etc/rsync.password"/>
<userDefinedPort start="false" port="874"/><!-- port=874 -->
<timeout start="false" time="100"/><!-- timeout=100 -->
<ssh start="false"/>
</rsync>
<failLog path="/tmp/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>
7.启动
/usr/local/sersync/bin/sersync2 -d -r -o /usr/local/sersync/conf/confxml.xml
说明:还有些其他参数,使用“-h”选项查看。
--------------------------------------------------
参数-d:启用守护进程模式,让sersync2运行在后台
参数-r:在监控前,将监控目录与远程主机用rsync命令推送一遍,
:即首先让远端目录和本地一致,以后再同步则通过监控实现增量同步
参数-n:指定开启守护线程的数量,默认为10个
参数-o:指定配置文件,默认使用confxml.xml文件
参数-m:单独启用其他模块,使用 -m refreshCDN 开启刷新CDN模块
参数-m:单独启用其他模块,使用 -m socket 开启socket模块
参数-m:单独启用其他模块,使用 -m http 开启http模块
不加-m参数,则默认执行同步程序