|——需求:

监控192.168.9.5【主】  下的 /data/vmeipai 目录  –> 同步到 192.168.12.8 【备】 下的 /data/vmeipai 目录

|——网络拓扑:

image

|——准备软件

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的软件包

image

|——【备环境软件安装】

机器上已经安装过了

image

没有的话使用下面安装命令:

  1. 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 ,双重否定即为肯定】

image

—然后创建 /etc/rsyncd.conf 文件  [说明:该文件原来是没有的,这里是新建]

vim /etc/rsyncd.conf

  1. ###############################################################
  1. ##以下是本机配置
  1. ###############################################################
  1. uid = nobody
  1.  
  1. gid = nobody
  1.  
  1. use chroot = no
  1.  
  1. hosts allow = 192.168.9.0/255 #允许的ip
  1.  
  1. strict modes = yes
  1.  
  1. max connections = 200
  1.  
  1. ###以下指定具体文件的地址也可以默认
  1.  
  1. pid file = /var/run/rsyncd.pid
  1.  
  1. lock file = /var/run/rsync.lock
  1.  
  1. log file = /var/log/rsyncd.log
  1.  
  1.  
  1. #####以上为全局配置,下面为具体模块
  1.  
  1. [backup] ##模块名
  1.  
  1. path = /data/vmeipai/ ##指定文件目录,必须
  1.  
  1. comment = test
  1.  
  1. read only = no
  1.  
  1. write only = no
  1.  
  1. ignore errors = yes ## 忽略IO错误
  1.  
  1. list = yes
  1.  
  1. uid = root
  1.  
  1. gid = root
  1.  
  1. ###以下是同步是验证用的账号,没有则是匿名同步
  1.  
  1. auth users = rsync
  1.  
  1. 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

加入自启动:[很多时候加入自启动能减少很多排错时间]

  1. 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) }

——检查运行情况:

image

|_______到这里 【备】服务器配置完成并生效

 

|———【主环境安装】

主环境需要 sersync+rsync

特别说明:主环境上 rsync 不需要配置 ,只要将rsync服务跑起来就可以. 不用配置同步模块等等,但是全局模块还是要指定一下.

——首先安装 sersync

1,解压到 /usr/local/ 下

  1. 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配置好的模块]

  1. <sersync>
  2. <localpath watch="/data/vmeipai">
  3. <remote ip="192.168.12.8" name="backup"/>
  4. </localpath>
  5. </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,最好写成服务,并且随机启动

首先看下帮助信息

image

1,

cd /usr/local/sersync

./sersync2 –d

  1. set the system param
  1. executeecho 50000000 > /proc/sys/fs/inotify/max_user_watches
  1. executeecho 327679 > /proc/sys/fs/inotify/max_queued_events
  1. parse the command param
  1. daemon thread num: 10
  1. parse xml config file
  1. host ip : localhost host port: 8008
  1. config xml parse success
  1. please set /etc/rsyncd.conf max connections=0 Manually
  1. sersync working thread 12 = 1(primary thread) + 1(fail retry thread) + 10(daemon sub threads)
  1. please according your cpu use -n param to adjust the cpu rate
  1. run the sersync:
  1. 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

  1. #! /bin/bash
  1. #chkconfig: 35 10 90
  1. #description: 监控目标目录[事件触发]同步到备份机
  1. #secript: /etc/init.d/sersyncd and chkconfig --level 35 sersyncd on
  1. #with service rsync [echo "/usr/bin/rsync --daemon" >> /etc/rc.local]
  1. #sersyncd
  1. #
  1. . /etc/rc.d/init.d/functions
  1. case "$1" in
  1. start)
  1. cd /usr/local/sersync
  1. ./sersync2 -r -d
  1. if [ $? -eq 0 ]
  1. then
  1. echo -e "Staring sersyncd [ OK ]"
  1. exit 0
  1. fi
  1. ;;
  1. stop)
  1. kill \'ps aux | grep sersync2 | grep -v grep | awk \'{print $2}\'\'
  1. if [ $? -eq 0 ]
  1. then
  1. echo -e "Stopping sersyncd [ OK ]"
  1. exit 0
  1. fi
  1. ;;
  1. status)
  1. ps aux | grep sersync2 | grep -v grep
  1. ;;
  1. 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安装、配置、实例

http://www.nginx.cn/tag/rsync  rsync备份同步文件教程

版权声明:本文为logon原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/logon/p/3628366.html