rsync 具有安全性高, 备份迅速, 支持增量备份等优点, 通过 rsync 可以解决对实时性要求不高的数据备份需求, 例如定期地备份文件服务器数据到远程服务器上, 对本地磁盘定期进行数据镜像等.
Rsync 不足
随着应用系统规模的不断扩大, 对数据的安全性和可靠性提出了更高的要求, rsync 便暴露出了很多不足之处. 首先, rsync 同步数据时, 需要扫描所有文件后进行比对, 然后进行差量传输. 如果文件数量达到了百万量级及以上时, 扫描文件就非常耗费时间, 有时还会出现只是很少一部分数据发生了变化, 因此 rsync 就变得非常低效了. 其次, rsync 不能够实时监测, 同步数据, 虽然它可以通过 Linux 守护进程的方式触发同步, 但是两次触发动作一定会出现时间差, 可能导致服务器端和客户端的数据出现不一致, 后期无法在出现故障时完全恢复数据.
Rsync+inotify 组合
为解决以上问题而出现的, 实现了数据的实时同步
inotify 是什么 ?
inotify 是一种强大的, 细粒度的, 异步的文件系统事件监控机制, Linux 内核从 2.6.13 版本起, 加入了对 inotify 的支持. 通过 inotify 可以监控文件系统中添加, 删除, 修改, 移动等各种事件, 利用这个内核接口, inotify-tools 便可以监控文件系统下文件的各种变化情况了.
检查内核是否支持 inotify:
Rsync+inotify 搭建实时同步系统
实验案例: rsync+inotify 搭建实时同步系统
案例描述: 在前文 Haproxy 搭建高可用 web 集群的网站拓扑架构图基础上, 实现两台 Web 站点数据的实时同步, web1 服务器为内容发布节点, web2 服务器为同步节点 (本文主要说明 rsync+inotify 实时同步数据技术, 其他请阅读前文, 谢谢!).
系统环境
主机 操作系统 IP 地址 web 站点目录 软件包
负载调度服务器: CentOS 7.0 x86_64 192.168.100.25 无 haproxy-1.5.19.tar.gz
web1 节点服务器: CentOS 7.0 x86_64 192.168.100.26 /www/wwwroot nginx-1.12.0.tar.gz,rsync-3.1.3.tar.gz,inotify-tools-3.14.tar.gz
web2 节点服务器: CentOS 7.0 x86_64 192.168.100.27 /web2/wwwroot nginx-1.12.0.tar.gz,rsync-3.1.3.tar.gz
Win 7 客户端: Windows 7 192.168.100.30 无 无
软件包: rsync+inotify 密码: do45
开始部署
inotify-tools 是用来监控文件系统变化的工具, 因此必须安装在内容发布系统上, 服务节点无需安装它, 而 web2 服务器上需要安装 rsync 实现文件传输.
一, web2 服务器 (待同步数据: rsync)
1. 安装 rsync 软件包
- # 解压
- tar zxvf rsync-3.1.3.tar.gz -C /opt
- # 切换目录
- cd /opt/rsync-3.1.3
- # 配置
- ./configure
- # 编译及安装
- make && make install
2. 修改 rsync 配置文件:
- /etc/rsyncd.conf
- #/etc/rsyncd: configuration file for rsync daemon mode
- #See rsyncd.conf man page for more options.
- #configuration example:
- uid = nobody
- gid = nobody
- use chroot = no
- max connections = 10
- strict mode=yes
- pid file = /var/run/rsyncd.pid
- lock file=/var/run/rsync.lock
- log file=/var/log/rsyncd.log
- [web1]
- path = /web1/wwwroot
- comment = web1 file
- ignore errrors
- read only=no
- write only=no
- hosts allow=*
- hosts deny=192.168.100.10
- list=false
- uid=root
- gid=root
- auth users=web1user
- secrets file=/etc/web1.pass
3. 新建密码文件 (文件格式: user:pass)
- /etc/web1.pass
- > web1user:www123
修改密码文件权限
chmod 600 /etc/web1.pass
4. 启动 rsync 守护进程
/usr/local/bin/rsync --daemon
5. 加入系统自启动文件
echo "/usr/local/bin/rsync --daemon">> /etc/rc.local
6. 查看 rsync 进程
ps -ef | grep rsync
Rsync+inotify 搭建实时同步系统
二, web1 服务器 (内发发布节点: rsync+inotify)
1. 安装 rsync 软件包
- # 解压
- tar zxvf rsync-3.1.3.tar.gz -C /opt
- # 切换目录
- cd /opt/rsync-3.1.3
- # 配置
- ./configure
- # 编译及安装
- make && make install
2. 新建密码文件, 需和节点服务器密码一致
- /etc/server.pass
- > www123
3. 修改密码文件权限
chmod 600 /etc/server.pass
4. 安装 inotify-tools
- # 解压
- tar zxvf inotify-tools-3.14.tar.gz -C /opt
- # 切换目录
- cd -C /opt/inotify-tools-3.14
- # 配置
- ./configure
- # 编译及安装
- make && make install
5. 编写 shell 脚本来配置内容发布节点
- vim /web/inotifyrsync.sh
- #!/bin/bash
- host1=192.168.100.26
- src=/web/wwwroot/
- dst1=web1
- user1=web1user
- /usr/local/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f%e' -e close_write,delete,create,attrib $src | while read files
- do
- /usr/bin/rsync -vzrtopg --delete --progress --password-file=/etc/server.pass $src $user1@$host1::$dst1> /dev/null 2>&1
- echo "${files} was rsynced.">> /tmp/rsync.log 2>&1
- done
Rsync+inotify 搭建实时同步系统
6. 为其指定可执行权限, 然后放入后台运行
- chmod 755 /web/inotifyrsync.sh
- /web/inotifyrsync.sh &
7. 将脚本加入系统自启动文件
echo "/web/inotifyrsync.sh &">> /etc/rc.local
测试
在 web1 服务器节点的 / web/wwwroot 目录下添加, 删除, 修改文件, 然后到 web2 服务器节点对应目录去查看文件是否跟随发布节点同步变化.
web1 服务器为内容发布节点:
Rsync+inotify 搭建实时同步系统
web2 服务器进行数据同步:
Rsync+inotify 搭建实时同步系统
来源: http://www.bubuko.com/infodetail-2691536.html