本文共 4266 字,大约阅读时间需要 14 分钟。
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客户端)创建测试文件,如图:
Web1端查看,如图:
Web2端查看,如图:
本文转自 ftmoonfans 51CTO博客,原文链接:http://blog.51cto.com/soulboy/1281704
转载地址:http://fxujl.baihongyu.com/