一,简介:
1. rsync是类unix系统下的数据镜像备份工具——remote sync。一款快速增量备份工具 Remote Sync,远程同步 支持本地复制,或者与其他SSH、rsync主机同步。与传统的cp、tar备份方式相比,rsync具有安全性高、备份迅速、支持增量备份等优点,通过rsync可以解决对实时性要求不高的数据备份需求,例如定期的备份文件服务器数据到远端服务器,对本地磁盘定期做数据镜像等。随着应用系统规模的不断扩大,对数据的安全性和可靠性也提出的更好的要求,rsync在高端业务系统中也逐渐暴露出了很多不足,首先,rsync同步数据时,需要扫描所有文件后进行比对,进行差量传输。如果文件数量达到了百万甚至千万量级,扫描所有文件将是非常耗时的。而且正在发生变化的往往是其中很少的一部分,这是非常低效的方式。其次,rsync不能实时的去监测、同步数据,虽然它可以通过Linux守护进程的方式进行触发同步,但是两次触发动作一定会有时间差,这样就导致了服务端和客户端数据可能出现不一致,无法在应用故障时完全的恢复数据。基于以上原因,rsync+inotify组合出现了!
2.Inotify 是一种强大的、细粒度的、异步的文件系统事件监控机制,linux内核从2.6.13起,加入了Inotify支持,通过Inotify可以监控文件系统中添加、删除,修改、移动等各种细微事件,利用这个内核接口,第三方软件就可以监控文件系统下文件的各种变化情况,而inotify-tools就是这样的一个第三方软件。在上面章节中,我们讲到,rsync可以实现触发式的文件同步,但是通过crontab守护进程方式进行触发,同步的数据和实际数据会有差异,而inotify可以监控文件系统的各种变化,当文件有任何变动时,就触发rsync同步,这样刚好解决了同步数据的实时性问题。
二,环境介绍:
host | status | 系统内核版本 |
192.168.180.4 | client或源端 | CentOS release 6.8(2.6.32-642.3.1.el6.x86_64) |
192.168.180.3 | server或目标端 | CentOS release 6.4(2.6.32-358.el6.x86_64) |
三,具体步骤:
(一),先在目标服务器端安装rsync服务端;
1,关闭SELINUX
[root@GJB-UAT~]#vim/etc/selinux/config #ThisfilecontrolsthestateofSELinuxonthesystem. #SELINUX=cantakeoneofthesethreevalues: #enforcing-SELinuxsecuritypolicyisenforced. #permissive-SELinuxprintswarningsinsteadofenforcing. #disabled-NoSELinuxpolicyisloaded. SELINUX=disabled #SELINUX=enforcing #SELINUXTYPE=cantakeoneofthesetwovalues: #targeted-Targetedprocessesareprotected, #mls-MultiLevelSecurityprotection. #SELINUXTYPE=targeted
[root@Monitorconf]#setenforce0####立即生效 setenforce:SELinuxisdisabled
2,开启防火墙tcp873rsync默认的端口
[root@GJB-UAT~]#vim/etc/sysconfig/iptables -AINPUT-mstate--stateNEW-mtcp-ptcp--dport873-jACCEPT "/etc/sysconfig/iptables"15L,607C已写入 [root@GJB-UAT~]#/etc/init.d/iptablesrestart [root@GJB-UAT~]#iptables-L-n ChainINPUT(policyACCEPT) targetprotoptsourcedestination ACCEPTall--0.0.0.0/00.0.0.0/0stateRELATED,ESTABLISHED ACCEPTicmp--0.0.0.0/00.0.0.0/0 ACCEPTall--0.0.0.0/00.0.0.0/0 ACCEPTtcp--0.0.0.0/00.0.0.0/0stateNEWtcpdpt:22 ACCEPTtcp--0.0.0.0/00.0.0.0/0stateNEWtcpdpt:80 ACCEPTtcp--0.0.0.0/00.0.0.0/0stateNEWtcpdpt:873 REJECTall--0.0.0.0/00.0.0.0/0reject-withicmp-host-prohibited ChainFORWARD(policyACCEPT) targetprotoptsourcedestination REJECTall--0.0.0.0/00.0.0.0/0reject-withicmp-host-prohibited
3,安装rsync服务端软件;
[root@GJB-UAT~]#yuminstallrsyncxinetd-y Loadedplugins:fastestmirror Determiningfastestmirrors *base:mirrors.cn99.com *extras:mirrors.cn99.com *updates:mirrors.cn99.com base|3.7kB00:00 extras|3.4kB00:00 updates|3.4kB00:00 updates/primary_db|4.3MB00:01 SettingupInstallProcess ResolvingDependencies -->Runningtransactioncheck --->Packagersync.x86_640:3.0.6-12.el6willbeinstalled --->Packagexinetd.x86_642:2.3.14-40.el6willbeinstalled -->FinishedDependencyResolution DependenciesResolved ========================================================================================================================== PackageArchVersionRepositorySize ========================================================================================================================== Installing: rsyncx86_643.0.6-12.el6base335k xinetdx86_642:2.3.14-40.el6base122k TransactionSummary ========================================================================================================================== Install2Package(s) Totaldownloadsize:457k Installedsize:942k Isthisok[y/N]:y DownloadingPackages: (1/2):rsync-3.0.6-12.el6.x86_64.rpm|335kB00:00 (2/2):xinetd-2.3.14-40.el6.x86_64.rpm|122kB00:00 -------------------------------------------------------------------------------------------------------------------------- Total2.3MB/s|457kB00:00 Runningrpm_check_debug RunningTransactionTest TransactionTestSucceeded RunningTransaction Installing:2:xinetd-2.3.14-40.el6.x86_641/2 Installing:rsync-3.0.6-12.el6.x86_642/2 Verifying:rsync-3.0.6-12.el6.x86_641/2 Verifying:2:xinetd-2.3.14-40.el6.x86_642/2 Installed: rsync.x86_640:3.0.6-12.el6xinetd.x86_642:2.3.14-40.el6 Complete! [root@GJB-UAT~]#rpm-qa|greprsync rsync-3.0.6-12.el6.x86_64 [root@GJB-UAT~]#vim/etc/xinetd.d/rsync#####设置开机自启动把disable=yes,改成no #default:off #description:Thersyncserverisagoodadditiontoanftpserver,asit\ #allowscrcchecksummingetc. servicersync { disable=no flags=IPv6 socket_type=stream wait=no user=root server=/usr/bin/rsync server_args=--daemon log_on_failure+=USERID }
4,添加新用户和组并赋给相应的权限
[root@GJB-UAT~]#groupaddrsync [root@GJB-UAT~]#useradd-grsync.rsync [root@GJB-UAT~]#useradd-grsyncrsync [root@GJB-UAT~]#greprsync/etc/passwd rsync:x:501:501::/home/rsync:/bin/bash [root@GJB-UAT~]#mkdir/home/rsync/backup/ [root@GJB-UAT~]#ll/home/rsync/ 总用量4 drwxr-xr-x.2rootroot40961月1717:15backup [root@GJB-UAT~]#chown-Rrsync.rsync/home/rsync/backup/ [root@GJB-UAT~]#ll/home/rsync/ 总用量4 drwxr-xr-x.2rsyncrsync40961月1717:15backup
5,创建rsync daemon的配置文件
[root@GJB-UAT~]#vim/etc/rsyncd.conf uid=rsync gid=rsync usechroot=no xconnections=40 timeout=300 pidfile=/var/run/rsyncd.pid lockfile=/var/run/rsyncd.lock logfile=/home/rsync/backup/rsyncd.log [backup] path=/home/rsync/backup/ ignoreerrors readonly=false list=false hostsallow=192.168.180.0/24 authusers=rsync secretsfile=/etc/rsync.password [root@GJB-UAT~]#vim/etc/rsync.password rsync:liqingbiao [root@GJB-UAT~]#chmod600/etc/rsync.password [root@GJB-UAT~]#chmod600/etc/rsyncd.conf
(二)安装rsync客户端(源端)
1,关闭SELINUX
[root@Monitorconf]#vim/etc/selinux/config #ThisfilecontrolsthestateofSELinuxonthesystem. #SELINUX=cantakeoneofthesethreevalues: #enforcing-SELinuxsecuritypolicyisenforced. #permissive-SELinuxprintswarningsinsteadofenforcing. #disabled-NoSELinuxpolicyisloaded. SELINUX=disabled #SELINUXTYPE=cantakeoneofthesetwovalues: #targeted-Targetedprocessesareprotected, #mls-MultiLevelSecurityprotection. #SELINUXTYPE=targeted
2,开启防火墙tcp 873端口
[root@Monitorconf]vim/etc/sysconfig/iptables -AINPUT-mstate--stateNEW-mtcp-ptcp--dport873-jACCEPT "/etc/sysconfig/iptables"15L,607C已写入 [root@Monitorconf]/etc/init.d/iptablesrestart [root@Monitorconf]iptables-L-n ChainINPUT(policyACCEPT) targetprotoptsourcedestination ACCEPTall--0.0.0.0/00.0.0.0/0stateRELATED,ESTABLISHED ACCEPTicmp--0.0.0.0/00.0.0.0/0 ACCEPTall--0.0.0.0/00.0.0.0/0 ACCEPTtcp--0.0.0.0/00.0.0.0/0stateNEWtcpdpt:22 ACCEPTtcp--0.0.0.0/00.0.0.0/0stateNEWtcpdpt:80 ACCEPTtcp--0.0.0.0/00.0.0.0/0stateNEWtcpdpt:873 REJECTall--0.0.0.0/00.0.0.0/0reject-withicmp-host-prohibited ChainFORWARD(policyACCEPT) targetprotoptsourcedestination REJECTall--0.0.0.0/0
3,安装配置rsync客户端软件
[root@Monitorconf]#yuminstallxinetd 已加载插件:fastestmirror 设置安装进程 Determiningfastestmirrors epel/metalink|4.6kB00:00 *epel:mirrors.tuna.tsinghua.edu.cn base|3.7kB00:00 dockerrepo|2.9kB00:00 epel|4.3kB00:00 epel/primary_db|5.9MB00:00 extras|3.4kB00:00 updates|3.4kB00:00 updates/primary_db|4.3MB00:00 解决依赖关系 -->执行事务检查 --->Packagexinetd.x86_642:2.3.14-40.el6willbe安装 -->完成依赖关系计算 依赖关系解决 ========================================================================================================================== 软件包架构版本仓库大小 ========================================================================================================================== 正在安装: xinetdx86_642:2.3.14-40.el6base122k 事务概要 ========================================================================================================================== Install1Package(s) 总下载量:122k Installedsize:259k 确定吗?[y/N]:y 下载软件包: xinetd-2.3.14-40.el6.x86_64.rpm|122kB00:00 运行rpm_check_debug 执行事务测试 事务测试成功 执行事务 正在安装:2:xinetd-2.3.14-40.el6.x86_641/1 Verifying:2:xinetd-2.3.14-40.el6.x86_641/1 已安装: xinetd.x86_642:2.3.14-40.el6 完毕! [root@Monitorconf]#rpm-qa|greprsync rsync-3.0.6-12.el6.x86_64 [root@Monitorlogs]#vim/etc/xinetd.d/rsync #default:off #description:Thersyncserverisagoodadditiontoanftpserver,asit\ #allowscrcchecksummingetc. servicersync { disable=no flags=IPv6 socket_type=stream wait=no user=root server=/usr/bin/rsync server_args=--daemon log_on_failure+=USERID } [root@Monitorconf]#/etc/init.d/xinetdstart 正在启动xinetd:[确定] [root@Monitorlogs]#netstat-lntp|grepxinetd tcp00:::873:::*LISTEN38518/xinetd
4,创建认证文件
[root@Monitorlogs]#vim/etc/rsyncd.passwd liqingbiao [root@Monitorconf]#chmod600/etc/rsyncd.passwd
5,测试源服务client器(192.168.180.4)到目标服务器服务端(192.168.180.3)之间的数据同步
[root@Monitornginx]#rsync-avH--port=873--progress/data/nginx/rsync@192.168.180.3::backup--password-file=/etc/rsyncd.passwd sendingincrementalfilelist ./ access.log 33777280100%39.93MB/s0:00:00(xfer#1,to-check=5/7) error.log 201151100%227.88kB/s0:00:00(xfer#2,to-check=4/7) nginx.access.log 278900324100%66.16MB/s0:00:04(xfer#3,to-check=3/7) log/ log/access.log 0100%0.00kB/s0:00:00(xfer#4,to-check=1/7) log/nginx.access.log 10521574100%66.89MB/s0:00:00(xfer#5,to-check=0/7) sent323439494bytesreceived386bytes71875528.89bytes/sec totalsizeis323400329speedupis1.00 [root@Monitornginx]#
去192.168.180.3上查看backup定义的路径查看同步的文件完成,数据测试完成。
(三)安装Inotify-tools工具,实时出发rsync进行同步。
1,查看服务器的内核是否支持inotify(如果列出文件的目录,说明服务器内核支持inotify)
[root@Monitornginx]#ll/proc/sys/fs/inotify 总用量0 -rw-r--r--1rootroot01月1720:20max_queued_events -rw-r--r--1rootroot01月1720:20max_user_instances -rw-r--r--1rootroot01月1720:20max_user_watches
2,安装编译工具和inotify-tools
[root@Monitornginx]#yuminstallmakegccgcc-c++ 已加载插件:fastestmirror 设置安装进程 Loadingmirrorspeedsfromcachedhostfile *epel:mirrors.tuna.tsinghua.edu.cn 包1:make-3.81-23.el6.x86_64已安装并且是最新版本 包gcc-4.4.7-17.el6.x86_64已安装并且是最新版本 包gcc-c++-4.4.7-17.el6.x86_64已安装并且是最新版本 无须任何处理 [root@Monitornginx]#wgethttp://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz --2017-01-1720:55:41--http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz 正在解析主机github.com...192.30.253.113,192.30.253.112 正在连接github.com|192.30.253.113|:80...已连接。 已发出HTTP请求,正在等待回应...301MovedPermanently 位置:https://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz[跟随至新的URL] --2017-01-1720:55:41--https://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz 正在连接github.com|192.30.253.113|:443...已连接。 已发出HTTP请求,正在等待回应...302Found 位置:https://cloud.github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz[跟随至新的URL] --2017-01-1720:55:43--https://cloud.github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz 正在解析主机cloud.github.com...54.192.127.169,54.192.127.35,54.192.127.201,... 正在连接cloud.github.com|54.192.127.169|:443...已连接。 已发出HTTP请求,正在等待回应...200OK 长度:358772(350K)[null] 正在保存至:“inotify-tools-3.14.tar.gz” 86%[=====================================================================>]310,53291.0K/seta(英国中部时95%[============================================================================>]343,30087.8K/seta(英国中部时100%[================================================================================>]358,77283.0K/seta(英国中部时100%[================================================================================>]358,77283.0K/sin4.2s 2017-01-1720:56:00(83.0KB/s)-已保存“inotify-tools-3.14.tar.gz”[358772/358772]) [root@Monitornginx]#tarxfinotify-tools-3.14.tar.gz [root@Monitornginx]#cdinotify-tools-3.14 [root@Monitorinotify-tools-3.14]#./configure--prefix=/usr/local//inotify [root@Monitorinotify-tools-3.14]#make&&makeinstall
3,设置系统环境变量,添加软连接。
[root@Monitorinotify-tools-3.14]#echo"PATH=/usr/local/inotify/bin:$PATH">>/etc/profile.d/inotify.sh [root@Monitorinotify-tools-3.14]#source/etc/profile.d/inotify.sh [root@Monitorinotify-tools-3.14]#echo"/usr/local/inotify/lib">/etc/ld.so.conf.d/inotify.conf [root@Monitorinotify-tools-3.14]#ln-s/usr/local/inotify/include/usr/include/inotify [root@Monitorinotify-tools-3.14]#sysctl-a|grepmax_queued_events
4,修改inotify默认参数
[root@Monitorinotify-tools-3.14]#sysctl-a|grepmax_queued_events fs.inotify.max_queued_events=99999999 [root@Monitorinotify-tools-3.14]#sysctl-a|grepmax_user_watches fs.inotify.max_user_watches=99999999 fs.epoll.max_user_watches=797306 [root@Monitorinotify-tools-3.14]#sysctl-a|grepmax_user_instances fs.inotify.max_user_instances=65535 [root@Monitorinotify-tools-3.14]#
修改添加如下参数:
[root@Monitorinotify-tools-3.14]#vim/etc/sysctl.conf fs.inotify.max_queued_events=99999999 fs.inotify.max_user_watches=99999999 fs.inotify.max_user_instances=65535
5,创建脚步,实时触发rsync进行同步
[root@Monitorinotify-tools-3.14]#vim/usr/local/inotify/rsync.sh #!/bin/sh #date:2017-01-17 #author:lqb srcdir=/data/nginx dstdir=backup excludedir=/usr/local/inotify/exclude.list rsyncuser=rsync rsyncpassdir=/etc/rsyncd.passwd dstip="192.168.180.3" #foripin$dstip #do rsync-avH--port=873--progress--delete--exclude-from=$excludedir$srcdir$rsyncuser@$ip::$dstdir--password-file=$rsyncpassdir #done /usr/local/inotify/bin/inotifywait-mrq--timefmt'%d/%m/%y%H:%M'--format'%T%w%f%e'-eclose_write,modify,delete,create,attrib,move$srcdir|whilereadfile do foripin$dstip do rsync-avH--port=873--progress--delete--exclude-from=$excludedir$srcdir$rsyncuser@$ip::$dstdir--password-file=$rsyncpassdir echo"${file}wasrsynced">>/tmp/rsync.log2>&1 done done [root@Monitorinotify-tools-3.14]#chmod+x/usr/local/inotify/rsync.sh [root@Monitorinotify-tools-3.14]#/usr/local/inotify/rsync.sh rsync:getaddrinfo:873:Noaddressassociatedwithhostname rsyncerror:errorinsocketIO(code10)atclientserver.c(124)[sender=3.0.6] sendingincrementalfilelist nginx/ nginx/inotify-tools-3.14.tar.gz 358772100%34.54MB/s0:00:00(xfer#1,to-check=54/58) nginx/nginx.access.log 4082457614%38.93MB/s0:00:05
至此,数据进行同步。