☞实时数据同步方案{ rsync; inotify; }

实时数据同步方案{ rsync; inotify; }



rsync 传输数据

Rsync uses a reliable algorithm to bring remote and host files into sync very quickly. Rsync is fast because it just sends the differences in the files over the network instead of sending the complete files. Rsync is often used as a very powerfulmirroring process or just as a more capable replacement for the rcp command. A technical report which describes the rsync algorithm is included in this package.

rsync is a file transfer program capable of efficient remote update via a fast differencing algorithm.

特点概括如下:

  1. 仅传输发生变化的文件,传输效率高

  2. 传输速度快,安全,可靠

  3. 能镜像备份完整的文件系统

  4. 配置简单,使用简单

安装 rsyncd 服务

CentOS 6

  • 安装 
    rsync非独立服务代理于xinetd守护进程,因此必须安装并启动xinetd守护进程,在启动rsync服务;

yum install rsync xinetd        #base repo提供
rpm -ql rsync
  • 主要文件 
    程序文件:/usr/bin/rsync 
    文档文件:/usr/share/doc/rsync-3.0.6 
    托管配置:/etc/xinetd.d/rsync 
    服务配置(手动创建):/etc/rsyncd.conf

CentOS 7

  • 安装 
    yum -y install rsync

  • 主要文件 
    程序文件:/usr/bin/rsync 
    服务配置:/etc/rsyncd.conf 
    Unit File/usr/lib/systemd/system/rsyncd.service

配置 rsyncd 服务

  • 配置文件简介 
    分为一个公共配置段与多个同步共享段(module name)

# configuration example:

# uid = nobody                      #运行rsync用户
# gid = nobody
# use chroot = yes                  #关闭chroot
# max connections = 4               #最大并发连接数
# pid file = /var/run/rsyncd.pid
# exclude = lost+found/             #排除的目录
# transfer logging = yes            #记录传输日志
# timeout = 900                     #超时时间
# ignore nonreadable = yes          #忽略不可读
# dont compress   = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2  #压缩

# [ftp]
#        path = /home/ftp           #同步目录
#        comment = ftp export area  

配置 rsync 手动同步示例

  • rsyncd 服务端配置

#Configure
uid = nobody                      
gid = nobody                    
use chroot = no                  
max connections = 10              
strict modes = yes                #开启严格模式
pid file = /var/run/rsyncd.pid    #pid文件位置
log file = /var/log/rsyncd.log    #日志文件位置

#module name
[share]
path = /data/rsync
ignore errors = yes              
read only = no                    
write only = no                  
hosts allow = 10.1.253.0/24       #白名单
hosts deny = *                    #黑名单
list = false                      #不允许查看文件列表
uid = root                        #进程以root用户执行,覆盖默认配置
gid = root                        
auth users = alice                #被被允许登录的用户
secrets file = /etc/rsync.passwd  #该用户的认证文件

  • 服务端生成授权用户的认证文件

echo alice:alicepass > /etc/rsync.passwd
chmod 600 /etc/rsync.passwd
  • 客户端为了自动验证创建的密码文件

echo alicepass > /etc/rsync_login.passwd
chmod 600 /etc/rsync.passwd

启动 rsyncd 服务

CentOS 6

service xinetd start
chkconfig rsync on          #启动rsync服务,chkconfig --list查看xinetd代理的服务
ss -tnl | grep ':873\>'

CentOS 7

systemctl start rsyncd.service
ss -tnl | grep ':873\>'

rsync 客户端命令

支持两种传输方式: 
一种是两个shell之间无需rsyncd服务的数据传输,使用 :指明SRC文件系统路径,登录用户为系统用户; 
另一种是C/S模型的rsyncd服务端与rsync客户端之间的数据传输,使用::指明同步段名(module name),登录用户为rsyncd配置文件中授权的用户;

Usage: rsync [OPTION]... SRC [SRC]... DEST
 or   rsync [OPTION]... SRC [SRC]... [USER@]HOST:DEST
 or   rsync [OPTION]... SRC [SRC]... [USER@]HOST::DEST
 or   rsync [OPTION]... SRC [SRC]... rsync://[USER@]HOST[:PORT]/DEST
 or   rsync [OPTION]... [USER@]HOST:SRC [DEST]
 or   rsync [OPTION]... [USER@]HOST::SRC [DEST]
 or   rsync [OPTION]... rsync://[USER@]HOST[:PORT]/SRC [DEST]
The ':' usages connect via remote shell, while '::' & 'rsync://' usages connect to an rsync daemon, and require SRC or DEST to start with a module name.

如:
rsync -avz user@host::modName /path/rsync
rsync -avz /path/rsync rsync://user@host/modName

rsync 数据传输功能

基于 ssh 协议完成数据传输(无rsyncd服务)

该用法类似于scp命令,可实现PUSH本地文件至远程,或PULL远程文件到本地,只需主机安装rsync程序即可;也就是说没有rsync服务端一说;

  • PUSH 
    rsync -avz /data/rsync/ root@10.1.253.69:/data/rsync/

  • PULL 
    rsync -avz root@10.1.253.29:/data/rsync/* /data/rsync/

基于 rsync 协议完成数据传输(需rsyncd服务)

基于C/S模型的rsyncd服务端与rsync客户端命令完成单向数据传输,数据传输使用rsync协议;

  1. 同步时无论是PUSHPULL都在客户端完成,即客户端同步对方到本机或同步本机到对方

  2. 不能指定传输的文件,必须以共享目录下的所有文件为单位进行传输;

  3. 必须使用-a-r递归选项,否则无法传输任何数据;

  4. 认证的用户为rsyncd.conf文件中auth users指定的用户和secrets file指定的mode为600的明文密码文件。

    • Client PUSH 
      rsync -av /data/rsync/ alice@10.1.253.29::share 
       
      rsync -av /data/rsync/ rsync://alice@10.1.253.29/share

    • Client PULL 
      rsync -vr alice@10.1.253.29::share /data/rsync/ 
       
      rsync -vr rsync://alice@10.1.253.29/share /data/rsync/

rsync 数据同步功能

基于 rsync 协议完成数据同步(需rsyncd服务)

基于C/S模型的rsyncd服务端与rsync客户端命令实现数据同步,数据传输使用rsync协议;

  1. 为了实现数据同步:与上文数据传输的区别是能够--delete删除服务端多出的文件,以及同步所有文件的元数据;

  2. 为了实现实时数据同步:即无需每次手动输入密码,通常把认证用户的密码文件写在文件中;

  3. 同步时无论是PUSHPULL都在客户端完成,即客户端同步对方到本机或同步本机到对方

  4. 同步时以共享目录下的所有文件为单位进行传输;

  5. 必须使用-a-r递归选项,否则无法传输任何数据;

  6. 认证的用户为rsyncd.conf文件中auth users指定的用户和secrets file指定的权限为600的明文账户密码文件。

  7. 客户端登录时使用的明文密码存放在文件中,权限为600,用户名则在user@host指定,username和password与服务端想对应即可。

    • Client PULL 
      rsync -vzrtopg --delete --password-file=/etc/rsync_login.passwd rsync://alice@10.1.253.29/share /data/rsync/ 
       
      rsync -avz --delete --password-file=/etc/rsync_login.passwd alice@10.1.253.29::share /data/rsync/

    • Client PUSH 
      rsync -vzrtogp --delete --password-file=/etc/rsync_login.passwd /data/rsync/ rsync://alice@10.1.253.29/share/ 
       
      rsync -vzrtogp --delete --progress --password-file=/etc/rsync_login.passwd /data/rsync/ alice@10.1.253.29::share 
       
      rsync -avz --delete ……因为-a可实现递归与归档的效果,同-rltogpD

  8. 同步参数说明: 
    -v, –verbose:详细输出同步的文件 
    –progress:显示备份过程,进度条 
    -u, –update:仅更新旧文件,判断mtime,不同步相同的文件 
    -z, –compress:压缩传输 
    -r, –recursive:递归子目录 
    -a, –archive:归档且递归,保留所有属性,相当于-rltogpD 
    -l, –links:保留软链接 
    -H, –hard-links:保留硬链结 
    -t, –times:保留时间戳 
    -o, –owner:保留属主 
    -g, –group:保留属组 
    -p, –perms:保留权限 
    -D, –devices:保留设备文件属性 
    –delete:删除多余的文件 
    –delete-after:传输完成后再删除多余的文件 
    –force:强制删除目录,即使不为空 
    –password-file=login.passwd:从文件读取密码,代替手动输入 
    –ignore-errors:忽略IO错误进行删除 
    –partial:保留未完全传输的文件,恢复后断点续传 
    –compare-dest=DIR 比较DIR中的文件来决定是否需要同步 
    -c, –checksum:对传输完成的文件进行校验 
    -b, –backup:不覆盖原有文件,为其重命名name~ 
    –exclude=PATTERN:指定排除的文件或目录,可模式匹配 
    –include=PATTERN:指定不排除的文件或目录 
    –bwlimit=number:限制传输带宽,KBytes per second

显示rysncd服务端的目录列表

  • 仅列出rsyncd服务端同步目录下的所有文件,验证用户为服务端已经定义好的用户; 
    rsync -v --password-file=/etc/rsync_login.passwd rsync://alice@10.1.253.29/share 
    或 
    rsync -v alice@10.1.253.29::share需手动输入口令;

inotify监测文件

inotify-tools is a set of command-line programs for Linux providing a simple interface to inotify. These programs can be used to monitor and act upon filesystem events.

使用inotifywait进程实时监控指定目录下的所有文件,一旦有文件发生变化,通过管道触发处于循环等待的rsync同步本地数据到远程,借此实现了实时的数据同步;

安装 inotify-tools 工具

  • yum安装要求配置好Fedora Epel仓库

yum install inotify-tools           #fedora-epel repo
rpm -ql inotify-tools
  • 主要文件 
    程序文件:/usr/bin/inotifywait/usr/bin/inotifywatch

inotifywait 命令

inotifywait [ options ] file1 [ file2 ] [ file3 ] [ ... ]
   -m|--monitor:保持对指定目录下文件的监控状态,检测到变化依然不退出
   -r|--recursive:递归子目录
   -s|--syslog:输出到日志文件
   -q|--quiet:静默模式,只输出events
   -e|--event <event1> [ -e|--event <event2> ... ]:指定监控的events(access,modify,attrib,close,move,delete,……)
   --timefmt <fmt>:指定时间格式,用 %T 代替输出
   --format:指定输出文件events变化的信息格式,如'%T %w%f %e'为:16-2-2 09:00 /data/rsync/xxATTRIB

如:
inotifywait -rmq --timefmt '%y-%m-%d %H:%M' --format '%T %w%f%e' -e modify,delete,create,attrib /data/rsync/

inotifywait+rsync 实时同步数据示例

inotifywait进程将持续监控指定路径的events发生,如果发现修改或增加了文件,就会立即按照指定格式输出相应的events;再结合rsync快速同步文件的功能,通过管道触发rsync执行同步操作并循环接收inotifywait的输出,即可实现数据的实时同步; 
建议在实现此步骤之前,必须先搞清楚rsync同步或传输数据的过程,以及在手动实现了数据同步的基础上再尝试与inotify相结合的实时数据同步; 
以下命令可使用&运行在后台,也可写成后台执行的shell脚本,以实现将本地文件同步给多台rsync服务器的效果;

inotifywait -rmq --timefmt '%y-%m-%d %H:%M' --format '%T %w%f%e' -e modify,delete,create,attrib /data/rsync/|while read line;do rsync -avz --delete --password-file=/etc/rsync_login.passwd /data/rsync/ alice@10.1.253.29::share; done

总结

rsync具有高效传输文件的特点,inotify能够实时监控文件变化,相结合就能够实时地将本地文件同步至服务端,尤其解决了小型站点的多台Web服务器实时同步数据的问题。但是此方案并非真正的实时数据同步,毕竟inotify监控文件events只能在文件描述符close之后才有相应的输出。但是,即便大型站点普遍采用分布式文件存储的方案实现数据共享与同步,也不能否认rsync+inotify基本能够实现数据的实时同步。

原创文章,作者:helloc,如若转载,请注明出处:http://www.178linux.com/56465

(0)
hellochelloc
上一篇 2016-10-29 16:51
下一篇 2016-10-29 17:21

相关推荐

  • 马哥教育网络班22期+第3周课程练习

    1、列出当前系统上所有已经登录的用户的用户名,注意:同一个用户登录多次,则只显示一次即可。    [test2@localhost ~]$ who |cut -d " " -f1|sort -u   &nbsp…

    Linux干货 2016-08-30
  • 如何在 Linux 下大量屏蔽恶意 IP 地址

    很多情况下,你可能需要在Linux下屏蔽IP地址。比如,作为一个终端用户,你可能想要免受间谍软件或者IP追踪的困扰。或者当你在运行P2P软件时。你可能想要过滤反P2P活动的网络链接。如果你是一名系统管理员,你可能想要禁止垃圾IP地址访问你们的公司邮件服务器。或者你因一些原因想要禁止某些国家访问你的web服务。在许多情况下,然而,你的IP地址屏蔽列表可能会很快…

    Linux干货 2015-02-26
  • dns配置

    dns

    Linux干货 2018-01-22
  • bash编程初体验(三)

    bash编程初体验之for for while until 概述 本文将介绍以for为代表的循环语句在shell 脚本中的应用,常见的循环语句有for, while,until,作为循环语句,顾名思义,它就是重复地做一件事,直到满足某一条件而退出;另外,还有两个循环控制语句continue与break来配合循环语句,以实现临时中断或跳出循环的功能;以下为fo…

    Linux干货 2016-08-24
  • lvs-dr

            通过为请求报文重新封装一个MAC首部进行转发,源MAC是DIP所在的接口的MAC,目标MAC是某挑选出的RS的RIP所在接口的MAC地址;源IP/PORT,以及目标IP/PORT均保持不变;     VIP通常配置在lo:0…

    2017-06-29