您的位置:首页 > 其它

磁盘同步rsync+sersync

2015-11-19 14:31 204 查看
一、说明

1. rsync在同步的时候,并不知道具体是哪个文件或者哪个目录发生了变化,每次都是对整个目录进行同步,当数据量很大时,整个目录同步非常耗时(rsync要对整个目录遍历查找对比文件),因此,效率很低。

2. sersync可以记录下被监听目录中发生变化的(包括增加、删除、修改)具体某一个文件或某一个目录的名字;

3. sersync + rsync 可以用sersync监听某一个目录发生变化,然后通知另一台机器的rsync来同步此变化. 所以,sersync安装在主服务器上,用于监控. rsync 安装在备服务上用户复制主服务器上的变化.

二、具体步骤

1. 二台机器 (192.168.1.7 备 , 192.168.1.165 主)

2. 两台机器上都关闭selinux.

3. 安装Rsync服务端软件 (192.168.1.7 安装)

yum install rsync xinetd -y

vim /etc/xinetd.d/rsync

disable = no                              			  # 其实rsync   由 xinetd管理
/etc/init.d/xinetd start                                    # 启动xinetd
chkconfig xinetdon                                       # 设置为开机启动


4. 配置Rsync (192.168.1.7)

a) 配置rsyncd.conf文件

vim /etc/rsyncd.conf

log file =/usr/local/rsync/rsyncd.log #日志文件位置.

pidfile =/usr/local/rsync/rsyncd.pid #pid文件的存放位置

lock file =/usr/local/rsync/rsync.lock #支持max connections参数的锁文件

secrets file= /usr/local/rsync/rsync.pass #用户认证配置文件,里面保存用户名称和密码,后面会创建这个文件

motd file =/usr/local/rsync/rsyncd.Motd #rsync启动时欢迎信息页面文件位置(文件内容自定义)

[rongzhongalex07] #自定义名称 , 用于监控连接的名字

path =/data/web/home/index/ #rsync服务端数据目录路径

comment =rongzhongalex07
#模块名称与[rongzhongalex07]自定义名称相同

uid =rongzhong
#设置rsync运行权限为root

gid =rongzhong
#设置rsync运行权限为root

port=873 #默认端口

use chroot =no #默认为true,修改为no,增加对目录文件软连接的备份

read only =no #读权限

write only =no
#写权限

list = no #不显示rsync服务端资源列表

maxconnections = 200
#最大连接数

timeout =600 #设置超时时间

hosts allow= 192.168.1.165 #允许进行数据同步的客户端IP地址,可以设置多个,用英文状态下逗号隔开

hosts deny =*
#禁止数据同步的客户端IP地址,可以设置多个,用英文状态下逗号隔开

*号表示所有

[rongzhongalex07_2] #
与上面是一样的,这是同时同步2个目录.

path =/data/web/home/page/static

comment =rongzhongalex07_2

uid =rongzhong

gid =rongzhong

port=873

use chroot =no

read only =no

write only =no

list = no

maxconnections = 200

timeout =600

hosts allow= 192.168.1.165

hosts deny =*

如下图:



b). 创建用户认证密码

echo “123456” > /usr/local/rsync/rsync.pass

c). 设置文件权限

chmod 600 /usr/local/rsync/rsync.pass

chmod 600 /etc/rsyncd.conf

d). 重启 xinetd 服务

/etc/init.d/xinetd restart

5. 配置sersync (192.168.1.165)

a). 下载sersync直接解压到 /usr/local/sersync目录中.

b). 添加密码验证文件,跟备机上一样的密文.

echo “123456” > /usr/local/rsync/rsync.pass

chmod 600 /usr/local/rsync/rsync.pass

c). 在目录下面有一个conxml.xml.

修改文件名.

cp conxml.xml alexconf_index.xml

cp conxml.xml alexconf_static.xml

因为这里要同时监控2个目录,所以配置2个配置文件.

vim alexconf_index.xml



vim alexconf_static.xml (这个跟上面的一样的修改方法)



D). 添加脚本,监控sersync是否正常运行

vim check_sersync.sh
# 用于检查alexconf_index.xml配置文件的sersync是否运行正常.

#!/bin/sh
sersync="/usr/local/sersync/sersync2"
confxml="/usr/local/sersync/alexconf_index.xml"
status=$(psaux |grep 'alexconf_index.xml'|grep -v 'grep'|wc -l)
if [$status -eq 0 ];
then
$sersync-d  -n 20 -o $confxml &
else
exit0;
fi


vim check_sersync_2.sh
# 用于检查alexconf_static.xml配置文件的sersync是否运行正常.

#!/bin/sh
sersync="/usr/local/sersync/sersync2"
confxml="/usr/local/sersync/alexconf_static.xml"
status=$(ps aux |grep'alexconf_static.xml'|grep -v 'grep'|wc -l)
if [ $status -eq 0 ];
then
$sersync -d  -n 20 -o $confxml &
else
exit 0;
fi


E). 加入crontab中 . (每10分钟检查一次)

*/10 * * * */usr/local/sersync/check_sersync.sh >> /usr/local/sersync/ser.log

*/10 * * * */usr/local/sersync/check_sersync_2.sh >> /usr/local/sersync/ser.log

F). 启动sersync

/usr/local/sersync/sersync2-d -n 20 -o /usr/local/sersync/alexconf_index.xml

/usr/local/sersync/sersync2-d -n 20 -o /usr/local/sersync/ alexconf_static.xml

# 参数-d:启用守护进程模式

# 参数-n: 指定开启守护线程的数量,默认为10个

# 参数-o:指定配置文件,默认使用confxml.xml文件
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: