天天看点

Linux运维 第三阶段 (十四)rsync

一、相关概念:

rsync(remote synchronize,rsync.samba.org):远程数据同步工具,通过网络快速同步多台主机间的文件,也可使用rsync同步本地硬盘中不同目录中的数据,利用其自身算法(rsync算法)只传送两个文件的不同部分,通过ssh方式来传输文件,因此保密性好;与cp、tar备份方式相比,rsync具有安全性高、备份迅速、支持增量备份的优点,通过rsync可以解决对实时性要求不高的数据备份需求,如定期备份服务器数据到指定的服务器、对本地磁盘定期做数据镜像、集群中负载均衡后端real server的网页数据改动时同步到其它real server等场景

随着应用系统规模的不断扩大,对数据的安全性和可靠性也提出更高的要求,rsync在高端业务系统中暴露出了很多不足:rsync同步数据时,需要扫描所有文件后进行比对,进行差量传输,若文件数量达到了百万甚至千万量级,这将非常消耗系统性能而且低效,有时变化的数据往往只是很少的一部分;rsync不能实时的去监测、同步数据,虽然可以通过计划任务方式触发同步,但两次触发动作一定有时间差,这将导致服务端和客户端数据可能会不一致,通过inotify可解决此问题

inotify(是一种强大的、细粒度的、异步的FS事件监控机制,文件内容发生改变是内核管理的,内核将这种监控文件本身是否发生改变的功能输出给用户空间,用户空间才有可能知道,linux内核从2.6.13起支持inotify机制,通过inotify可以监控FS中添加、删除、修改、移动等各种细微事件,利用这个内核接口,通过第三方软件(inotify-tools)就可监控FS下文件的各种变化)

sersync(基于inotify开发,功能类似inotify,C++编写,多线程同步,sersync可以记录下被监听目录中发生变化的具体哪一个文件或哪个目录,然后使用rsync同步的时候,只同步发生变化的)

rsync+inotify与rsync-sersync两种架构的区别?

rsync+inotify(inotify监控FS的各种变化,一旦有变动时就触发rsync同步,解决了数据同步实时性的问题,inotify仅记录被监控的目录发生了变化,并没记录哪个目录或哪些文件发生了变化,所以rsync每次都要在被监控的所有目录和其下的文件与目标目录进行比对,再传输有差异的文件(要遍历目录下所有文件找出有差异的才进行同步),若数据量很大这将非常耗时,因此效率很低)

rsync+sersync(sersync可以记录被监控的目录中具体哪个目录哪些文件发生了变化,rsyc在同步时只同步发生变化的目录或文件,因此速度很快(节约时间)、节约带宽、效率很高)

注:若数据量不大建议使用rsync+inotify,若数据量很大建议使用rsync+sersync

二、操作:

1、rsync+inotify的实现

举例:实现两个web服务器的网页文件保持同步(一台为server,另一台为client,仅在server-side更新网页文件,查看client-side是否有同步,数据流向是server-->client)

准备软件:

rsync--3.0.9.tar.gz(https://rsync.samba.org/ftp/rsync/src/rsync-3.0.0.tar.gz)

inotify-tools-3.14.tar.gz(https://cloud.github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz)

注:server-side安装rsync和inotify,client-side仅安装rsync

#ll /proc/sys/fs/inotify/(若此目录下有max_queued_events、max_user_instances、max_user_watches这三项则此内核支持inotify机制,内核2.6.13之后才支持此功能)

server-side(192.168.41.131):

#tar xf  rsync-3.0.9.tar.gz

#cd rsync-3.0.9

#./configure  --prefix=/usr/local/rsync

#make

#make install

#vim /etc/profile.d/rsync.sh

export  PATH=$PATH:/usr/local/rsync/bin

#. !$

#vim /etc/man.config

MANPATH  /usr/local/rsync/share/man

#man rsync

#rsync options  SRC  DET(rsync命令格式)

options:-vzrtopg

-v(--verbose)

-z(--compress)

-r(--recursive)

-t(--times)

-o(--owner)

-p(--perms)

-g(--group)

-a(--archive)

-A(--acls)

-H(--hard-links)

-l(--links)

-D(--devices  --specials)

--delete(delete extraneousfiles from dest dirs,删除目标位置有而原始位置没有的文件)

--progress(show progress duringtransfer显示传输过程)

--passwd-file=FILE(readdaemon-access password from FILE,指定密钥文件)

#echo  "redhat"  >  /usr/local/rsync/rsync.passwd(建立认证文件,密码为redhat,此处不需要用户名)

#chmod 600  !$(为安全,将密码文件权限改为600)

#tar xf  inotify-tools-3.14.tar.gz

#cd inotify-tools-3.14

#./configure  --prefix=/usr/lcoal/inotify

#vim /etc/profile.d/inotify.sh

export PATH=$PATH:/usr/local/inotify/bin

MANPATH /usr/local/inotify/share/man

#man inotifywatch(gather filesystem access statistics using inotify,用于短期监控,任务完成后再出结果)

#man inotifywait(wait for changes to files using inotify,用于持续监控,实时输出结果)

#inotifywait  -mrq -e  EVENT  --timefmt FMT  --format  FMT FILE(inotifywait命令用法)

-m(--monitor)

-q(--quiet)

-e(--event,Listen forspecific event(s) only)

--timefmt

--format(Output  in a  user-specified  format)

EVENT有:

modify(A watched file or afile within a watched directory was written to)

delete(A file or directorywithin a watched directory was deleted)

create(A file or directorywas created within a watched directory)

attrib(The metadata of awatched file or a file within a watched directory was modified)

move(A file or directorywas moved from or to a watched directory)

#vim /root/rsync.sh

#!/bin/bash

#

host=192.168.41.132

src=/var/www/html/

des=test

user=testuser

/usr/local/inotify/bin/inotifywait  -mrq --timefmt  ‘%d/%m/%y %H:%M’  --format ‘%T %w%f%e’  -e  modify,delete,create,attrib  $src |  while  read files;do

/usr/bin/rsync  -vzrtopg --delete  --progress  --passwd-file=/usr/local/rsync/rsync.passwd  $src $user@$host::$des

echo “${files} was rsynced” >>  /var/log/rsync.log  2>&1

done

#chmod 764  /root/rsync.sh

#sh /root/rsync.sh  &(注意!待client-side启动rsync服务后再运行此脚本)

client-side(192.168.41.132):

#echo  "testuser:redhat"  >  /usr/local/rsync/rsync.passwd(建立认证文件)

#vim /usr/local/rsync/rsync.conf

uid = root

gid = root

use chroot = no(默认为true,增加对目录文件软链接的备份)

max connections = 10

timeout = 600

strict modes = yes

pid file = /var/run/rsyncd.pid

lock file = /var/log/rsyncd.lock

log file = /var/log/rsyncd.log

motd file = /etc/rsyncd.motd(可在此文件中编辑启动时的欢迎信息)

[test](自定义名称)

path = /var/www/html/(数据同步到本地的目录路径)

comment = test(描述信息)

ignore errors = yes

read only = no(设置rsync服务端文件为读写权限)

hosts allow = 192.168.41.131(允许进行数据同步的主机地址,多个用逗号隔开)

hosts deny = *(禁止数据同步的主机地址)

list = false(不显示rsync服务端资源列表)

uid = root(设置rsync运行权限)

port = 873(默认端口)

auth users = testuser(执行数据同步的用户名,可以设置多个用逗号隔开)

secrets file =/user/local/rsync/rsync.passwd(用户认证配置文件,里面包含用户名和密码)

#rsync --daemon --config=/usr/local/rsync/rsync.conf(启动rsync)

测试:

server-side(/var/www/html下):

<a href="http://s2.51cto.com/wyfs02/M02/77/10/wKiom1ZiMsnS4l-qAABrmV9sixw732.jpg" target="_blank"></a>

client-side(/var/www/html下):

<a href="http://s2.51cto.com/wyfs02/M02/77/0F/wKioL1ZiM0XgifzPAAAb2Kb_Lvo556.jpg" target="_blank"></a>

2、rsync+sersync的实现:

server-side:

参照上例,安装rsync、制作认证文件并改权限为600

#mkdir /usr/local/sersync/

# tar  -xf  sersync_64bit_binary_stable_final.tar.gz

#cd  GNU-Linux-x86

#mv  confxml.xml  sersync2 /usr/local/sersync/

#cd /usr/local/sersync

#cp  confxml.xml confxml.xml.bak

#vim confxml.xml

……

&lt;sersync&gt;

   &lt;localpath watch="/var/www/html"&gt;

       &lt;remote ip="192.168.41.132" name="test"/&gt;

       &lt;!--&lt;remote ip="192.168.8.39" name="tongbu"/&gt;--&gt;

       &lt;!--&lt;remote ip="192.168.8.40" name="tongbu"/&gt;--&gt;

   &lt;/localpath&gt;

   &lt;rsync&gt;

       &lt;commonParams params="-vzrtopg"/&gt;

       &lt;auth start="true" users="testuser" passwordfile="/usr/local/rsync/rsync.passwd"/&gt;

       &lt;userDefinedPort start="false"port="874"/&gt;&lt;!-- port=874 --&gt;

       &lt;timeout start="false" time="100"/&gt;&lt;!--timeout=100 --&gt;

       &lt;ssh start="false"/&gt;

   &lt;/rsync&gt;

   &lt;failLog path="/tmp/rsync_fail_log.sh" timeToExecute="60"/&gt;&lt;!--default every 60mins execute once--&gt;

   &lt;crontab start="true"schedule="600"&gt;&lt;!--600mins--&gt;

       &lt;crontabfilter start="false"&gt;

       &lt;exclude expression="*.php"&gt;&lt;/exclude&gt;

       &lt;exclude expression="info/*"&gt;&lt;/exclude&gt;

       &lt;/crontabfilter&gt;

   &lt;/crontab&gt;

   &lt;plugin start="false" name="command"/&gt;

&lt;/sersync&gt;

#vim /root/check_sersync.sh

SERSYNC="/usr/local/sersync/sersync2"

CONF_FILE="/usr/local/sersync/confxml.xml"

STATUS=`ps aux | grep 'sersync2' | grep -v'grep' | wc -l`

if [ $STATUS -eq 0 ];then

 $SERSYNC -d -r -o $CONF_FILE &amp;

else

exit 0;

fi

#crontab -e(为防止sersync2意外中断,每5分钟使用此脚本检测,若sersync2未运行,则会自动开启,进行实时同步)

*/5 * * * *  bash  /root/check_sersync.sh&amp;&gt; /dev/null

#/usr/local/sersync/sersync2  -d -r  -o  /usr/local/sersync/confxml.xml(开启sersync服务)

-d(run as a daemon)

-r(rsync all the localfiles to the remote servers before the sersync work)

-o(config xml name:  ./confxml.xml)

client-side:

参照上例,安装rsync,配置rsync.conf文件,配置认证文件,并开启rsync服务

<a href="http://s4.51cto.com/wyfs02/M02/77/10/wKiom1ZiMvyAkYQbAABeKZMPEE4022.jpg" target="_blank"></a>

<a href="http://s2.51cto.com/wyfs02/M01/77/0F/wKioL1ZiM3iA804-AAA3yYJJMzY516.jpg" target="_blank"></a>

<a href="http://s4.51cto.com/wyfs02/M00/77/10/wKiom1ZiMyGSuvUTAAAqecFM-T8276.jpg" target="_blank"></a>

以上内容是自找资料整理。

本文转自 chaijowin 51CTO博客,原文链接:http://blog.51cto.com/jowin/1719774,如需转载请自行联系原作者

继续阅读