Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1162002
  • 博文数量: 220
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1769
  • 用 户 组: 普通用户
  • 注册时间: 2015-03-13 16:19
个人简介

努力, 努力, 再努力

文章分类

全部博文(220)

文章存档

2018年(8)

2017年(46)

2016年(75)

2015年(92)

我的朋友

分类: 系统运维

2015-08-19 18:56:01


 

 

一.    环境描述

1.     系统版本: CentOS release 6.3 (Final)

2.     内核版本: 2.6.32-279.el6.x86_64

3.     软件版本:

rsync-3.0.6-12.el6.x86_64

sersync2.5.4_64bit_binary_stable_final

4.     host: 192.168.1.10

5.     目标host:192.168.1.20

6.     同步方向:源(10) --- --->目标(20)

二.    配置Rsync服务端(主机)

1.     关闭selinux

SELINUX= enforcing  改为SELINUX=disabled

然后重启

2.     Iptables开放Rsync端口873

-A INPUT -m state --state NEW -m tcp -p tcp --dport 873 -j ACCEPT

3.     安装Rsync软件: yum install rsync xinetd -y

4.     配置xinetd服务,并重启

vim /etc/xinetd.d/rsync

disable = yes  改为 disable = no

flags = IPv6  改为 flags = IPv4

/etc/init.d/xinetd restart

5.     创建rsyncd.conf配置文件

vim /etc/rsyncd.conf

[root@localhost ~]# cat /etc/rsyncd.conf

log file = /var/log/rsyncd.log

transfer logging = yes

log format = %a %u %o %m %f %l %t %c %b

pidfile = /var/run/rsyncd.pid

lock file = /var/run/rsync.lock

secrets file = /etc/rsync.pass

motd file = /etc/rsyncd.Motd

[webapps]

path = /home/apache-tomcat-7.0.57/webapps

comment =

uid = root

gid = root

port=873

use chroot = no

read only = no

list = no

max connections = 2000

timeout = 600

auth users = chinanet

hosts allow = 192.168.1.10

[www]

path = /home/www

comment =

uid = root

gid = root

port=873

use chroot = no

read only = no

list = no

max connections = 2000

timeout = 600

auth users = chinanet

hosts allow = 192.168.1.10

[conf]

path = /home/apache-tomcat-7.0.57/conf

comment =

uid = root

gid = root

port=873

use chroot = no

read only = no

list = no

max connections = 2000

timeout = 600

auth users = chinanet

hosts allow = 192.168.1.10

[ceshi]

path = /home/apache-tomcat-7.0.57/ceshi

comment =

uid = root

gid = root

port=873

use chroot = no

read only = no

list = no

max connections = 2000

timeout = 600

auth users = chinanet

hosts allow = 192.168.1.10

:wq保存退出

 

6.     创建rsync用户认证文件

vim /etc/rsync.pass

chinanet:123456

:wq 保存退出

7.     设置文件权限

chmod 600 /etc/rsyncd.conf

chmod 600 /etc/rsync.pass

8.     启动Rsyncd

service xinetd restart

或者

/etc/init.d/xinetd restart

三.    配置Rsync客户端(主机)

1.     关闭selinux

2.     Iptables开放Rsync端口873

3.     安装Rsync客户端软件

yum install rsync xinetd -y

4.     配置Rsync客户端

vim /etc/xinetd.d/rsync

disable = yes 改为disable = no

flags = IPv6 改为 flags = IPv4

:wq 保存退出

5.     重启xinetd服务

/etc/init.d/xinetd restart

6.     创建密码文件(对应主服务器上的用户名 chinanet)

vim /etc/rsync.passwd

123456

:wq 保存退出

7.     设置文件的读写权限

chmod 600 /etc/rsync.passwd

四.    测试Rsync同步:从源主机同步数据到目标主机

1.     在源主机的同步目录创建ceshe文件夹

 mkdir -pv /home/apache-tomcat-7.0.57/ceshi

2.     master执行命令:

注意:下面的命令rsync不是3行,而是一行

rsync -avH --port=873 --progress --delete /home/apache-tomcat-7.0.57/ceshi/

chinanet@192.168.1.10::ceshi --password-file=/etc/passwd.txt

注意1:上面的命令中ceshi ceshi/ 效果不一样

ceshi表示把ceshi目录和其下的文件同步到服务端的ceshi模块指定的目录中

ceshi/ 表示把ceshi目录下的文件同步到服务端的ceshi模块指定的目录中

注意2:用rsync同步数据完成后

然后用du –sb 源目录, du –sb 目标目录

有时看到两边大小不一致, 目标目录总是比源目录大4096字节,或者是4096字节的2倍或者3

多出的4096字节是目录本身的大小,换句话说,同步完成后,目标目录比源目录大4096字节,或者8192字节,而数据大小是一致的。

另外du –sb 命令计算的是目录和文件占用的块数,不是目录和文件的实际大小。一个块可能是1024字节或者4096字节

五.    安装sersync工具,实时触发rsync进行同步()

1. 查看服务器内核是否支持inotify

如果列出如下内容,说明服务器内核支持inotify

[root@localhost ~]# ll /proc/sys/fs/inotify/

总用量 0

-rw-r--r-- 1 root root 0 5   5 16:42 max_queued_events

-rw-r--r-- 1 root root 0 5   5 16:42 max_user_instances

-rw-r--r-- 1 root root 0 5   5 16:42 max_user_watches

 

支持inotify功能的内核是从2.6.13开始,uname –r可以查看内核

[root@localhost ~]# uname -r

2.6.32-279.el6.x86_64

2. 修改内核,使inotify的参数永久生效

vim /etc/sysctl.conf

fs.inotify.max_queued_events = 99999999

fs.inotify.max_user_watches = 99999999

fs.inotify.max_user_instances = 65535

加载内核参数: sysctl –p

参数解释:

a)     fs.inotify.max_queued_events

inotify队列最大长度,如果值太小,会出现"** Event Queue Overflow **"错误,导致监控文件不准确

b)    fs.inotify.max_user_watches

要同步的文件包含多少目录,可以用:

find /home/apache-tomcat-7.0.57/ |wc –l

统计,必须保证max_user_watches值大于统计结果(这里/home/ apache-tomcat-7.0.57/为同步文件目录)

 

c)     fs.inotify.max_user_instances = 65535

每个用户创建inotify实例最大值

3. 安装sersync

a)下载: style="font-size:15.0pt;">

b) root目录下执行

tar zxvf sersync2.5.4_64bit_binary_stable_final.tar.gz

注意:该软件无须再编译

c) 将解压的目录移动到 /usr/local下并改名为sersync

mv GNU-Linux-x86/ /usr/local/sersync

六.    配置sersync(同步3个目录对应3个配置文件)

1.     备份配置文件: cp /usr/local/sersync/confxml.xml  ~

2.     配置文件的解释:

<filter start="true"> 启用过滤器

<exclude expression="ROOT/upload/*"></exclude>

<exclude expression="(.*)\.php"></exclude>

</filter>

要同步的目录:/home/apache-tomcat-7.0.57/webapps

要排除的目录:ROOT/upload/*

要排除的文件类型: (.*)\.php 

<localpath watch="/home/apache-tomcat-7.0.57">

#源服务器同步目录
<remote ip="192.168.1.10" name="chinanet"/>

remote ip 指目标服务器

name 目标服务器上的目录模块名称

<auth start="true" users="chinanet" passwordfile="/etc/rsync.passwd"/>

 

start=true 表示启动

users是目标服务器上的用户名

Passwordfile 是本地存储的密码文件

<failLog path="/tmp/rsync_fail_log.sh" timeToExecute="60"/>

failLog path 指脚本运行失败的日志

<crontab start="false" schedule="600">

#如果设置为true,每隔600分钟执行一次全盘同步

3.     设置开机自动运行,自动同步3个目录

vim /etc/rc.d/rc.local

/usr/local/sersync/sersync2 -d -r -o /usr/local/sersync/confxml_webapps.xml

/usr/local/sersync/sersync2 -d -r -o /usr/local/sersync/confxml_conf.xml

 

4.     增加监测脚本:防止sersync进程失效

[root@localhost sersync]# cat chk_sersync2.sh

#!/bin/sh

sersync="/usr/local/sersync/sersync2"

webapps_conf=/usr/local/sersync/confxml_webapps.xml

www_conf=/usr/local/sersync/confxml_

config_conf=/usr/local/sersync/confxml_conf.xml

 

webapps_st=$(ps aux |grep 'confxml_webapps'|grep -v 'grep'|wc -l)

if [ $webapps_st -eq 0 ];

then

$sersync -d -r -o $webapps_conf &

fi

 

#www_st=$(ps aux |grep 'confxml_www'|grep -v 'grep'|wc -l)

#if [ $www_st -eq 0 ];

#then

#$sersync -d -r -o $www_conf &

#fi

 

config_st=$(ps aux |grep 'confxml_conf'|grep -v 'grep'|wc -l)

if [ $config_st -eq 0 ];

then

$sersync -d -r -o $config_conf &

fi

5.     通过计划任务每隔5分钟检查一次sersync2进程

[root@localhost sersync]# crontab -l

*/5 * * * * /usr/local/sersync/chk_sersync2.sh  > /dev/null 2>&1

 

重启crontab服务:/etc/init.d/crond restart

七.    Rsync 同步原理及相关内容

1.     根据Rsync的算法得知,Rsync同步时,仅同步两个文件中不相同的数据块,可以通过rsync.log验证

2.     当文件未发生变化时,Rsync不同步数据块,仅对比file list

3.     当文件发生变化时,Rsync同步file list(文件的数据块列表), 传输差异数据块

4.     对于同步较大文件时,如1G以上,比较慢,主要时间消在Rsync对文件的分块和计算弱校验强校验上。因此,Rsync适合普通尺寸的文件,如网站更新的代码,图片,HTML页面,对于特大文件的同步对比计算比较慢

5.     特别注意:Rsync第一次部署,要做一次全面的数据同步,这个过程较慢,主要工作是对源数据所有文件进行分块,计算校验码,但这个过程是必须的,当Rsync具备了这些信息后,才能实现完整的同步功能。

6.     如果想实现本地源----->服务器:触发式同步多个目录

需要启动多个sersync2 并指定多个xml配置文件

比如有两个不同的目录, a b ,需要相应的两个 sersync 的配置文件,启动两个 sersync 就可以实现

[root@tc]# ./sersync -d -r -o a.xml
[root@tc]# ./sersync -d -r -o b.xml

7.  Sersync 实时同步是基于rsync

Sersync发现被监视的目录有文件发生变化,它通过rsync将该文件同步到目的主机

 


阅读(1712) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~