sersync2 文件的实时同步备份
|——需求:
监控192.168.9.5【主】 下的 /data/vmeipai 目录 –> 同步到 192.168.12.8 【备】 下的 /data/vmeipai 目录
|——网络拓扑:
|——准备软件
1,Rsync
2,Sersync
rsync可以从【wget http://rsync.samba.org/ftp/rsync/rsync-3.0.9.tar.gz 】获取,不过我这里使用了【yum install rsync -y 】
sersync 详细:【https://code.google.com/p/sersync/】解压之后直接使用无需安装
3,查看本机系统环境:64位的机子我们就下载64的软件包
|——【备环境软件安装】
机器上已经安装过了
没有的话使用下面安装命令:
yum install rsync -y
.csharpcode, .csharpcode pre { font-size: small; color: rgba(0, 0, 0, 1); font-family: consolas, “Courier New”, courier, monospace; background-color: rgba(255, 255, 255, 1) }
.csharpcode pre { margin: 0 }
.csharpcode .rem { color: rgba(0, 128, 0, 1) }
.csharpcode .kwrd { color: rgba(0, 0, 255, 1) }
.csharpcode .str { color: rgba(0, 96, 128, 1) }
.csharpcode .op { color: rgba(0, 0, 192, 1) }
.csharpcode .preproc { color: rgba(204, 102, 51, 1) }
.csharpcode .asp { background-color: rgba(255, 255, 0, 1) }
.csharpcode .html { color: rgba(128, 0, 0, 1) }
.csharpcode .attr { color: rgba(255, 0, 0, 1) }
.csharpcode .alt { background-color: rgba(244, 244, 244, 1); width: 100%; margin: 0 }
.csharpcode .lnum { color: rgba(96, 96, 96, 1) }
—配置[备]上的【rsync】
安装好之后 更改 /etc/xinetd.d/rsync 文件 将disable 由【yes 改成 no ,双重否定即为肯定】
—然后创建 /etc/rsyncd.conf 文件 [说明:该文件原来是没有的,这里是新建]
vim /etc/rsyncd.conf
###############################################################
##以下是本机配置
###############################################################
uid = nobody
gid = nobody
use chroot = no
hosts allow = 192.168.9.0/255 #允许的ip
strict modes = yes
max connections = 200
###以下指定具体文件的地址也可以默认
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
#####以上为全局配置,下面为具体模块
[backup] ##模块名
path = /data/vmeipai/ ##指定文件目录,必须
comment = test
read only = no
write only = no
ignore errors = yes ## 忽略IO错误
list = yes
uid = root
gid = root
###以下是同步是验证用的账号,没有则是匿名同步
auth users = rsync
secrets file = /etc/rsync.passwd ##指定密码文件
.csharpcode, .csharpcode pre { font-size: small; color: rgba(0, 0, 0, 1); font-family: consolas, “Courier New”, courier, monospace; background-color: rgba(255, 255, 255, 1) }
.csharpcode pre { margin: 0 }
.csharpcode .rem { color: rgba(0, 128, 0, 1) }
.csharpcode .kwrd { color: rgba(0, 0, 255, 1) }
.csharpcode .str { color: rgba(0, 96, 128, 1) }
.csharpcode .op { color: rgba(0, 0, 192, 1) }
.csharpcode .preproc { color: rgba(204, 102, 51, 1) }
.csharpcode .asp { background-color: rgba(255, 255, 0, 1) }
.csharpcode .html { color: rgba(128, 0, 0, 1) }
.csharpcode .attr { color: rgba(255, 0, 0, 1) }
.csharpcode .alt { background-color: rgba(244, 244, 244, 1); width: 100%; margin: 0 }
.csharpcode .lnum { color: rgba(96, 96, 96, 1) }
—创建密码认证文件:[文件格式-> 用户名:密码]
1,
vim /etc/rsync.passwd
rsync:logonmy ##用户名:密码。这个不是系统用户,只是rsync使用认证,所以不需要创建系统用户useradd 【必须与auth users 保持一致】
2,
chmod 600 /etc/rsync.passwd ## [这一步必须]
3,
如果在配置文件中指定了欢迎信息,
vim /etc/rsync.motd
welcome the rsync services!
——开启iptables响应端口[ 不然【主】服务器推过来的文件都被挡在门外进不来了]
iptables -i INPUT -p tcp –dport 873 -j ACCEPT
iptables -L
结果如下
Chain INPUT (policy ACCEPT)
target prot opt source destination
ACCEPT tcp — anywhere anywhere tcp dpt:rsync
——启动rsync:
# service xinetd start
或者
/usr/bin/rsync –daemon
加入自启动:[很多时候加入自启动能减少很多排错时间]
echo "/usr/bin/rsync --daemon" >> /etc/rc.local
.csharpcode, .csharpcode pre { font-size: small; color: rgba(0, 0, 0, 1); font-family: consolas, “Courier New”, courier, monospace; background-color: rgba(255, 255, 255, 1) }
.csharpcode pre { margin: 0 }
.csharpcode .rem { color: rgba(0, 128, 0, 1) }
.csharpcode .kwrd { color: rgba(0, 0, 255, 1) }
.csharpcode .str { color: rgba(0, 96, 128, 1) }
.csharpcode .op { color: rgba(0, 0, 192, 1) }
.csharpcode .preproc { color: rgba(204, 102, 51, 1) }
.csharpcode .asp { background-color: rgba(255, 255, 0, 1) }
.csharpcode .html { color: rgba(128, 0, 0, 1) }
.csharpcode .attr { color: rgba(255, 0, 0, 1) }
.csharpcode .alt { background-color: rgba(244, 244, 244, 1); width: 100%; margin: 0 }
.csharpcode .lnum { color: rgba(96, 96, 96, 1) }
——检查运行情况:
|_______到这里 【备】服务器配置完成并生效
|———【主环境安装】
主环境需要 sersync+rsync
特别说明:主环境上 rsync 不需要配置 ,只要将rsync服务跑起来就可以. 不用配置同步模块等等,但是全局模块还是要指定一下.
——首先安装 sersync
1,解压到 /usr/local/ 下
tar -zxf sersync2.5.4_64bit_binary_stable_final.tar.gz -C /usr/local/
.csharpcode, .csharpcode pre { font-size: small; color: rgba(0, 0, 0, 1); font-family: consolas, “Courier New”, courier, monospace; background-color: rgba(255, 255, 255, 1) }
.csharpcode pre { margin: 0 }
.csharpcode .rem { color: rgba(0, 128, 0, 1) }
.csharpcode .kwrd { color: rgba(0, 0, 255, 1) }
.csharpcode .str { color: rgba(0, 96, 128, 1) }
.csharpcode .op { color: rgba(0, 0, 192, 1) }
.csharpcode .preproc { color: rgba(204, 102, 51, 1) }
.csharpcode .asp { background-color: rgba(255, 255, 0, 1) }
.csharpcode .html { color: rgba(128, 0, 0, 1) }
.csharpcode .attr { color: rgba(255, 0, 0, 1) }
.csharpcode .alt { background-color: rgba(244, 244, 244, 1); width: 100%; margin: 0 }
.csharpcode .lnum { color: rgba(96, 96, 96, 1) }
2,重命名 为sersync
# mv GNU-Linux-x86 sersync 这样就好了 ,不需要安装解压就能用。
——配置sersync [配置要注意自己的实际情况]
主要的修改部分在这[改成备服务器上的rsync配置好的模块]
<sersync> <localpath watch="/data/vmeipai"> <remote ip="192.168.12.8" name="backup"/> </localpath> </sersync>
意思是将 监控目录 【localpath watch】 /data/vmeipai 下的任何 增删改事件推到 备服务上的rsync [backup]模块
.csharpcode, .csharpcode pre { font-size: small; color: rgba(0, 0, 0, 1); font-family: consolas, “Courier New”, courier, monospace; background-color: rgba(255, 255, 255, 1) }
.csharpcode pre { margin: 0 }
.csharpcode .rem { color: rgba(0, 128, 0, 1) }
.csharpcode .kwrd { color: rgba(0, 0, 255, 1) }
.csharpcode .str { color: rgba(0, 96, 128, 1) }
.csharpcode .op { color: rgba(0, 0, 192, 1) }
.csharpcode .preproc { color: rgba(204, 102, 51, 1) }
.csharpcode .asp { background-color: rgba(255, 255, 0, 1) }
.csharpcode .html { color: rgba(128, 0, 0, 1) }
.csharpcode .attr { color: rgba(255, 0, 0, 1) }
.csharpcode .alt { background-color: rgba(244, 244, 244, 1); width: 100%; margin: 0 }
.csharpcode .lnum { color: rgba(96, 96, 96, 1) }
——尔后安装rsync 这里在备服务器上安装过了 配置的 话 省掉模块的配置即可
|——启动【主】上的sersync,最好写成服务,并且随机启动
首先看下帮助信息
1,
cd /usr/local/sersync
./sersync2 –d
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
daemon thread num: 10
parse xml config file
host ip : localhost host port: 8008
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)
please according your cpu ,use -n param to adjust the cpu rate
run the sersync:
watch path is: /data/vmeipai
.csharpcode, .csharpcode pre { font-size: small; color: rgba(0, 0, 0, 1); font-family: consolas, “Courier New”, courier, monospace; background-color: rgba(255, 255, 255, 1) }
.csharpcode pre { margin: 0 }
.csharpcode .rem { color: rgba(0, 128, 0, 1) }
.csharpcode .kwrd { color: rgba(0, 0, 255, 1) }
.csharpcode .str { color: rgba(0, 96, 128, 1) }
.csharpcode .op { color: rgba(0, 0, 192, 1) }
.csharpcode .preproc { color: rgba(204, 102, 51, 1) }
.csharpcode .asp { background-color: rgba(255, 255, 0, 1) }
.csharpcode .html { color: rgba(128, 0, 0, 1) }
.csharpcode .attr { color: rgba(255, 0, 0, 1) }
.csharpcode .alt { background-color: rgba(244, 244, 244, 1); width: 100%; margin: 0 }
.csharpcode .lnum { color: rgba(96, 96, 96, 1) }
现在sersync已经开启
2,开启实时事件触发的同步,同步之前对【主服务器上的监控目录】做一次整体的同步动作
./sersync –r
3, 最后将同步进程做成守护进程 一直在后台运行 [做到目标目录的任何触发事件都被捕捉到并被推送]
./sersync –r –d
|——【进一步】 和rsync 写入自启动一样,将sersync 写成自启动服务
#cd /etc/init.d/
#vim sersyncd
#! /bin/bash
#chkconfig: 35 10 90
#description: 监控目标目录[事件触发]同步到备份机
#secript: /etc/init.d/sersyncd and chkconfig --level 35 sersyncd on
#with service rsync [echo "/usr/bin/rsync --daemon" >> /etc/rc.local]
#sersyncd
#
. /etc/rc.d/init.d/functions
case "$1" in
start)
cd /usr/local/sersync
./sersync2 -r -d
if [ $? -eq 0 ]
then
echo -e "Staring sersyncd [ OK ]"
exit 0
fi
;;
stop)
kill \'ps aux | grep sersync2 | grep -v grep | awk \'{print $2}\'\'
if [ $? -eq 0 ]
then
echo -e "Stopping sersyncd [ OK ]"
exit 0
fi
;;
status)
ps aux | grep sersync2 | grep -v grep
;;
esac
.csharpcode, .csharpcode pre { font-size: small; color: rgba(0, 0, 0, 1); font-family: consolas, “Courier New”, courier, monospace; background-color: rgba(255, 255, 255, 1) }
.csharpcode pre { margin: 0 }
.csharpcode .rem { color: rgba(0, 128, 0, 1) }
.csharpcode .kwrd { color: rgba(0, 0, 255, 1) }
.csharpcode .str { color: rgba(0, 96, 128, 1) }
.csharpcode .op { color: rgba(0, 0, 192, 1) }
.csharpcode .preproc { color: rgba(204, 102, 51, 1) }
.csharpcode .asp { background-color: rgba(255, 255, 0, 1) }
.csharpcode .html { color: rgba(128, 0, 0, 1) }
.csharpcode .attr { color: rgba(255, 0, 0, 1) }
.csharpcode .alt { background-color: rgba(244, 244, 244, 1); width: 100%; margin: 0 }
.csharpcode .lnum { color: rgba(96, 96, 96, 1) }
|____然后 执行 chkconfig –level 35 sersyncd on
这样整个过程就完整了。完成了我们实时同步的需求!
参考:
http://fenglingcorp.iteye.com/blog/1218401 服务器同步(sersync2 完全安装配置说明(一)—-基本功能使用)
http://www.kankanews.com/ICkengine/archives/92550.shtml sersync2+rsync目录文件实时同步备份
http://blog.sina.com.cn/s/blog_5eda2dda01015fcs.html rsync安装、配置、实例