rsync简介web
rsync是Linux和UNIX系统下的文件同步和数据传输工具,它采用了“rsync”算法使一个客户机和远程文件服务器之间的文件同步,它不一样于cp和wget命令完整复制的局限性,它支持增量备份,所以文件传输效率高,于是同步时间很短,具体特性以下:算法
一、能够镜像保存整个目录树和文件系统安全
二、能够增量同步数据。文件传输效率高。bash
三、能够保持原有文件的权限、时间等属性。服务器
四、加密传输数据,保证数据的安全性。ssh
五、可使用rcp、ssh等方式来传输文件。异步
搭建远程容灾备份系统ide
Web服务器为了保证数据安全,天天凌晨2点将数据备份到远程容灾服务器上,因为数据量比较大,每一年只能进行增量备份,仅备份当天新增数据,系统环境以下:工具
操做系统 | RHEL5.8 |
内核版本 | 2.6.18-308.el5 |
web服务器地址 | 192.168.1.104 |
远程容灾服务器地址 | 192.168.1.110 |
Web端配置
测试
主配置文件/etc/rsyncd.conf
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
backup:ixdba123
启动rsync守护进程
/usr/local/bin/rsync --daemon
远程容灾端配置
密码文件chmod 600 /etc/server.pass
ixdba123
添加计划任务crontab -e
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
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
web1user:ixdba123
启动rsync守护进程并添加开机自动启动
echo "/usr/local/bin/rsync" >>/etc/rc.local /usr/local/bin/rsync--daemon
Web2端配置
主配置文件/etc/rsyncd.conf
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
web2user:ixdba123
启动rsync守护进程并添加开机自动启动
echo "/usr/local/bin/rsync" >>/etc/rc.local /usr/local/bin/rsync--daemon
Server端配置
安装inotify-tools
tar xf rsync-3.0.7.tar.gz cd rsync-3.0.7 ./configure make && make install
密码文件chmod 600 /etc/server.pass
ixdba123
监控目录变化并同步Web节点脚本
#!/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
指定权限并放入后台运行
chmod 755 /web/wwwroot/inotifyrsync.sh /web/wwwroot/inotifyrsync.sh &
为此脚本添加开机自动启动
echo "/web/wwwroot/inotifyrsync.sh &" >> /etc/rc.local
测试结果
Server端(rsync客户端)建立测试文件,如图:
Web1端查看,如图:
Web2端查看,如图: