Skip to Content

rsync实时同步

实验目的:例如在一个大的集群架构中,下面有N个WEB节点,当站点需要更新操作时,如果一台一台的去更新,会大大降低系统/运维工程师的效率,然而,使用rsync构建所有节点之间的实时同步,其中有一台同步端,当同步端下面的WEB站点内容发生变化时,所有被同步端同时也会得到自动更新,这样就大大降低了系统/运维工程师比较枯燥而又无味的操作。
注:此同步不可用于数据库直接的实时同步。
一、系统环境
1、同步端
[root@host3 ~]# cat /etc/redhat-release
CentOS release 5.5 (Final)
[root@host3 ~]# uname -a
Linux host3.zc.com 2.6.18-194.el5 #1 SMP Fri Apr 2 14:58:14 EDT 2010 x86_64 x86_64 x86_64 GNU/Linux
[root@host3 ~]# ifconfig eth0 | grep "inet addr" | awk '{print $2}' | awk -F: '{print $2}'
192.168.10.3
2、被同步端
[root@host4 ~]# cat /etc/redhat-release
CentOS release 5.5 (Final)
[root@host4 ~]# uname -a
Linux host4.zc.com 2.6.18-194.el5 #1 SMP Fri Apr 2 14:58:14 EDT 2010 x86_64 x86_64 x86_64 GNU/Linux
[root@host4 ~]# ifconfig eth0 | grep "inet addr" | awk '{print $2}' | awk -F: '{print $2}'
192.168.10.4
二、软件环境
rsync
rsync是一个开源快速备份工具,可以在不同主机之间镜像同步整个目录树,支持增量备份、保持链接和权限,且采用优化的同步算法、传输前执行压缩,因此非常适用于异地备份、镜
像服务器等应用。
inotify-tools-3.14.tar.gz
Inotify 是文件系统事件监控机制,计划包含在即将发布的 Linux 内核中作为 dnotify
的有效替代。dnotify是较早内核支持的文件监控机制。Inotify一种强大的、细粒度的、异步的机制,它满足各种各样的文件监控需要,不仅限于安全和性能。
三、安装配置rsync(CentOS5.5系统中默认已经安装rsync,这里我们使用系统默认rpm包安装的rsync,也顺道提供了源码包的安装方法)
1、源码包
tar xzvf rsync-3.0.8.tar.gz
cd rsync-3.0.8
./configure
make
make install
mkdir /etc/rsyncd
vim rsyncd.conf
2、rpm包
同步端:
[root@host3 ~]# yum -y install gcc* xinetd
[root@host3 ~]# rpm -qa | grep rsync
rsync-2.6.8-3.1
[root@host3 ~]# cat /etc/rsyncd.conf #此文件为自己建立的文件
uid = nobody #运行rsync程序的用户
gid = nobody #运行rsync程序的组
use chroot = yes #禁锢在源目录
adress = 192.168.10.3 #监听地址(本机ip)
port 873 #监听端口
log file = /var/log/rsyncd.log #日志文件位置
pid file = /var/run/rsyncd.pid #存放进程ID的文件位置
hosts allow = 192.168.10.0/24 #允许访问的客户机地址
[wwwroot] #
path = /test/ #源目录的实际路径
read only = yes #是否为只读
comment = Document Root of host1.benet.com #描述
dont compress = *.gz *.bz2 *.tgz *.zip *.rar *.z #同时不再压缩的文件类型
被同步端:
[root@host4 ~]# yum -y install xinetd
[root@host4 ~]# rpm -qa | grep rsync
rsync-2.6.8-3.1
[root@host4 ~]# cat /etc/rsyncd.conf
uid = nobody #运行rsync程序的用户
gid = nobody #运行rsync程序的组
use chroot = yes #禁锢在源目录
adress = 192.168.10.4 #监听地址(本机ip)
port 873 #监听端口
log file = /var/log/rsyncd.log #日志文件位置
pid file = /var/run/rsyncd.pid #存放进程ID的文件位置
hosts allow = 192.168.10.0/24 #允许访问的客户机地址
[wwwroot] #
path = /test/ #源目录的实际路径
read only = yes #是否为只读
comment = Document Root of host1.benet.com #描述
dont compress = *.gz *.bz2 *.tgz *.zip *.rar *.z #同时不再压缩的文件类型
[root@host4 ~]# sed -i 's/yes/no/' /etc/xinetd.d/rsync
[root@host4 ~]# /etc/init.d/xinetd restart
[root@host4 ~]# chkconfig xinetd on
[root@host4 ~]# netstat -anpt | grep 873
tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 3749/xinetd
四、在同步端安装inotify软件
1、调整inotify内核参数
在linux内核中,默认的inotify机制提供了三个调控参数:max_queue_envents 监控事件队列
max_user_instances 最多监控实例数
max_user_watches 每个实例最多监控文件数(建议大于监控目标的文件数)
[root@host3 ~]# vi /etc/sysctl.conf #末行添加
fs.inotify.max_queued_events = 16384
fs.inotify.max_user_instances = 1024
fs.inotify.max_user_watches = 1048576
[root@host3 ~]# cd /data/
[root@host3 data]# tar -zxf inotify-tools-3.14.tar.gz
[root@host3 data]# cd inotify-tools-3.14
[root@host3 inotify-tools-3.14]# ./configure
[root@host3 inotify-tools-3.14]# make
[root@host3 inotify-tools-3.14]# make install
2、编写触发式同步脚本
[root@host3 inotify-tools-3.14]# cd /script/
[root@host3 script]# cat inosync.sh
#!/bin/bash
INOTIFY_CMD="inotifywait -mrq -e modify,create,attrib,move,delete /test/"
$INOTIFY_CMD | while read DIRECTORY EVENT FILE
do
if [ $(pgrep rsync | wc -l) -le 0 ];then
rsync -azH --delete /test/ :/test/
rsync -e 'ssh -p 2000' -azH --delete /test/ :/test/ #当ssh端口被修改之后,得添加'ssh -p 2000'指定端口
fi
done
[root@host3 script]# chmod +x inosync.sh
[root@host3 script]# echo '/script/inosync.sh' >> /etc/rc.local
[root@host3 script]# sh inosync.sh &
四创建同步用户
被监控端
[root@host4 /]# useradd rput
[root@host4 /]# passwd rput 密码根据自己的安全要求设置
监控端
[root@host3 ~]# ssh-keygen
[root@host3 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub
设置rput用户对被同步目录的权限
被监控端
[root@host4 ~]# setfacl -R -m u:rput:rwx /test/
5、测试
在同步端的/test目录下创建一个目录,然后再被同步端进行查看
更多精彩:
河南最好的白癜风医院http://www.henanbaidianfeng.com/
白癜风病因http://www.henanbaidianfeng.com/by/
白癜风治疗http://www.henanbaidianfeng.com/zl/
白癜风诊断http://www.henanbaidianfeng.com/bzd/
白癜风症状http://www.henanbaidianfeng.com/bzz/
白癜风常识http://www.henanbaidianfeng.com/bcs/
白癜风饮食http://www.henanbaidianfeng.com/bys/
白癜风用药http://www.henanbaidianfeng.com/bfy/
白癜风遗传http://www.henanbaidianfeng.com/byc/
康复案例http://www.henanbaidianfeng.com/kf/
白癜风危害http://www.henanbaidianfeng.com/bwh/
中药疗法http://www.henanbaidianfeng.com/lf/
专家问答http://www.henanbaidianfeng.com/wd/
儿童白癜风http://www.henanbaidianfeng.com/jbx/
青年白癜风http://www.henanbaidianfeng.com/xtx/
女性白癜风http://www.henanbaidianfeng.com/jdx/
老年白癜风http://www.henanbaidianfeng.com/csx/

发表新评论

  • 你可以在文本中使用BBCode标记语言。 URL会自动被转为链接。

更多关於格式化选项的信息

CAPTCHA
请验证您是否是机器人。
Image CAPTCHA
Enter the characters shown in the image.