rsync+inotify搭建实时同步系统

简介:

rsync简介

   rsync是Linux和UNIX系统下的文件同步和数据传输工具,它采用了“rsync”算法使一个客户机和远程文件服务器之间的文件同步,它不同于cp和wget命令完整复制的局限性,它支持增量备份,因此文件传输效率高,因而同步时间很短,具体特性如下:

   1、可以镜像保存整个目录树和文件系统

   2、可以增量同步数据。文件传输效率高。

   3、可以保持原有文件的权限、时间等属性。

   4、加密传输数据,保证数据的安全性。

   5、可以使用rcp、ssh等方式来传输文件。


搭建远程容灾备份系统

   Web服务器为了保证数据安全,每天凌晨2点将数据备份到远程容灾服务器上,由于数据量比较大,每年只能进行增量备份,仅备份当天新增数据,系统环境如下:

操作系统 RHEL5.8
内核版本 2.6.18-308.el5
web服务器地址 192.168.1.104
远程容灾服务器地址 192.168.1.110

Web端配置

主配置文件/etc/rsyncd.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
uid = nobody
gid = nobody
use chroot = no
max connections = 10
strict modes =  yes
pid  file  /var/run/rsyncd .pid
lock  file  /var/run/rsync .lock
log  file  /var/log/rsyncd .log
[ixdba]
path =  /webdata
commnet = ixdba  file
ignore errors
read  only = no
write only = no
hosts allow = *
hosts deny = 192.168.1.200
list =  false
uid = root
gid = root
auth  users  = backup
secrets  file  /etc/server .pass

密码文件chmod 600 /etc/server.pass 

1
backup:ixdba123

启动rsync守护进程

1
/usr/local/bin/rsync  --daemon

远程容灾端配置

密码文件chmod 600 /etc/server.pass

1
ixdba123

添加计划任务crontab -e

1
1 3 * * *   /usr/local/bin/rsync  -vzrtopg --delete --progress --exclude  "*access*"  --exclude  "debug*"  backup@192.168.1.104::ixdba  /ixdba . dir  --password- file = /etc/server .pass

分析不足

此时一个远程容灾系统已经搭建完成,但这并非是一个完整意义上的容灾方案,由于rsync需要通过触发才能将服务器端数据同步,因此两次触发同步的时间间隔内,服务器和客户端的数据可能不一致,如果在这个时间间隔内,网站系统出现问题,数据必然丢失,Linux 2.6.13以后的内核提供了inotify文件系统监控机制,用过rsync与inotify的组合,完全可以实现rsync服务器端和客户端的实时数据同步。


rsync+inotify实现数据的实时备份

   inotify是一种强大的、细粒度的、异步的文件系统时间监控机制,Linux内核从2.6.13版本起,加入了对inotify的支持。通过inotify可以监控文件系统中添加、删除、修改、移动等各种细微事件,利用这个内核接口,第三方软件可以监控文件系统下文件的各种变化情况,inotify-tools软件正是基于这种需求实现的一个第三方软件。

   内容分发服务器实时同步数据对2个Web服务器上,inotify是用来监控文件系统变化的工具,因此必须安装在内容发布节点上,内容发布节点(Server)充当了rsync客户端的角色,2个Web节点充当了rsync服务器端的角色,整个数据同步过程就是一个从客户端向服务器端发送数据的过程,与前面案例中的逻辑结构刚好相反,系统环境如下:

     节点名称      内核版本        IP地址 网页数据存放路径
Web1 2.6.18-308.el5 192.168.1.110 /web1/wwwroot
Web2 2.6.18-308.el5 192.168.1.160 /web2/wwwroot
Server 2.6.18-308.el5 192.168.1.104 /web/wwwroot

Web1端配置

主配置文件/etc/rsyncd.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
uid = nobody
gid = nobody
use chroot = no
max connections = 10
strict modes =  yes
pid  file  /var/run/rsyncd .pid
lock  file  /var/run/rsync .lock
log  file  /var/log/rsyncd .log
[web1]
path =  /web1/wwwroot
commnet = web1  file
ignore errors
read  only = no
write only = no
hosts allow = 192.168.1.104
hosts deny = *
list =  false
uid = root
gid = root
auth  users  = web1user
secrets  file  /etc/server .pass

密码文件chmod 600 /etc/server.pass

1
web1user:ixdba123

启动rsync守护进程并添加开机自动启动

1
2
echo  "/usr/local/bin/rsync"  >> /etc/rc . local
/usr/local/bin/rsync--daemon

Web2端配置

主配置文件/etc/rsyncd.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
uid = nobody
gid = nobody
use chroot = no
max connections = 10
strict modes =  yes
pid  file  /var/run/rsyncd .pid
lock  file  /var/run/rsync .lock
log  file  /var/log/rsyncd .log
[web2]
path =  /web2/wwwroot
commnet = web2  file
ignore errors
read  only = no
write only = no
hosts allow = 192.168.1.104
hosts deny = *
list =  false
uid = root
gid = root
auth  users  = web2user
secrets  file  /etc/server .pass

密码文件chmod 600 /etc/server.pass

1
web2user:ixdba123

启动rsync守护进程并添加开机自动启动

1
2
echo  "/usr/local/bin/rsync"  >> /etc/rc . local
/usr/local/bin/rsync--daemon

Server端配置

安装inotify-tools

1
2
3
4
tar  xf  rsync -3.0.7. tar .gz
cd  rsync -3.0.7
. /configure
make  &&  make  install

密码文件chmod 600 /etc/server.pass

1
ixdba123

监控目录变化并同步Web节点脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#!/bin/sh
host1=192.168.1.110
host2=192.168.1.160
dir = /web/wwwroot/
dst1=web1
dst2=web2
usr1=web1user
usr2=web2user
/usr/local/bin/inotifywait  -mrq --timefmt  '%d/%m/%y %H:%M'  -- format  '%T %w%f%e'  -e close_write,delete,create,attrib $ dir  \
while  read  files
         do
         /usr/bin/rsync  -vzrtopg --delete --progress --password- file = /etc/server .pass $ dir  $usr1@$host1::$dst1
         /usr/bin/rsync  -vzrtopg --delete --progress --password- file = /etc/server .pass $ dir  $usr2@$host2::$dst2
         echo  "${files} was rsynced"  >>  /tmp/rsync .log 2>&1
         done

指定权限并放入后台运行

1
2
chmod  755  /web/wwwroot/inotifyrsync .sh
/web/wwwroot/inotifyrsync .sh &

为此脚本添加开机自动启动

1
echo  "/web/wwwroot/inotifyrsync.sh &"  >>  /etc/rc . local

测试结果

Server端(rsync客户端)创建测试文件,如图:

213932523.png

Web1端查看,如图:

214125433.png

Web2端查看,如图:

214223457.png




本文转自 ftmoonfans  51CTO博客,原文链接:http://blog.51cto.com/soulboy/1281704

相关文章
|
1天前
|
监控 安全 Unix
Rsync+Inotify实现数据实时同步
Rsync+Inotify实现数据实时同步
|
9月前
|
监控
inotify+rsync实现实时同步数据
inotify+rsync实现实时同步数据
84 0
|
6月前
|
监控 Linux
rsync+inotify实时同步
rsync+inotify实时同步
|
监控 Linux
配置inotify + rsync 实现实时同步
配置inotify + rsync 实现实时同步
213 0
配置inotify + rsync 实现实时同步
|
监控 测试技术 Linux
rsync+inotify实时同步案例
rsync+inotify实时同步案例 随着应用系统规模的不断扩大,对数据的安全性和可靠性也提出的更好的要求,rsync在高端业务系统中也逐渐暴露出了很多不足,首先,rsync同步数据时,需要扫描所有文件后进行比对,进行差量传输。
1224 0
Inotify+rsync远程实时同步
安装inotify yum -y install inotify-tools 写同步脚本 Path=/data IP=172.
907 0
|
监控 安全 Shell
inotify+rsync实现实时同步
1.1 什么是实时同步:如何实现实时同步 要利用监控服务(inotify),监控同步数据服务器目录中信息的变化 发现目录中数据产生变化,就利用rsync服务推送到备份服务器上 1.2 实现实时同步的方法    inotify+rsync 方式实现数据同步    sersync 方式实现实时数据同步 详情参照:http://www.cnblogs.com/clsn/p/7707828.html 1.2.1 实时同步原理介绍   1.3 inotify+rsync 方式实现数据同步 1.3.1 Inotify简介   Inotify是一种强大的,细粒度的。
1820 0
|
数据安全/隐私保护 存储 安全