rsync:

rsync:集合了cp和scp的功能。比较要复制的目录和目标目录中的文件,将不同的复制过去。

特点:

    1、可以镜像保存整个目录树或文件系统;

    2、较高的数据传输效率;

    3、可以借助于ssh实现安全数据传输;

    4、支持匿名传输;


rsync命令的选项:


-n

测试同步,不真正执行同步过程

-v

详细输出模式

-q

静默模式,不输出信息

-c

checksum,开启校验功能

-r

递归复制

-a

归档,保留文件的原有属性

-p

保留文件的权限

-t

只保留文件的时间戳

-l

保留符号的链接

-g

保留属组

-o

保留属主

-D

保留设备文件

-e ssh

使用ssh作为传输承载

-z

压缩后传输

--progress

显示进度条

--stats

显示如何执行

--delete

删除多余的文件从dest中

--password-file=FILE

从FILE中读取daemon-access密码

注意:rsync命令中,如果源路径是目录,且其路径末尾有/则会复制目录中的内容,如果没有/则会复制目录本身及目录中的文件,目标后有无/无关紧要。


rsync的用法:

Pull: 将数据复制过来

rsync [OPTION...] [USER@]HOST::SRC... [DEST]

rsync [OPTION...] rsync://[USER@]HOST[:PORT]/SRC... [DEST]

Push: 将数据发送过去

rsync [OPTION...] SRC... [USER@]HOST::DEST               

rsync [OPTION...] SRC... rsync://[USER@]HOST[:PORT]/DEST


rsync命令的工作模式:

第一种模式:shell模式,也称作本地模式,类似于copy

功能:将本地指定数据复制到指定路径下,如果指定路径中有于源数据相同的数据,则跳过不复制。

使用格式:rsync /SRC DEST

rsync语法 rsync -i_rsync语法

第二种模式:远程shell模式,可以利用ssh协议承载其远程传输过程。

功能:利用ssh协议将本机的数据发送到目标主机自定义位置

rsync -e ssh /data/createDB.sql 172.16.249.124:/root

rsync语法 rsync -i_运维_02

rsync语法 rsync -i_数据_03

rsync -e ssh --stats 172.16.249.124:/root/createDB.sql  /

rsync语法 rsync -i_shell_04

第三种模式:列表模式,仅列出源中的内容 -nv

模拟复制,但是不真正操作

rsync语法 rsync -i_rsync语法_05

第四种模式:服务模式,此时rsync工作为守护进程,能接受客户端的数据同步请求

服务器模式和samba的功能比较相似

首先需要安装xinetd(c6),再安装rsync

rsync在c6上是监听在xinetd程序下的。

rsync语法 rsync -i_数据_06

chkconfig rsync on

rsync语法 rsync -i_rsync语法_07

设定rsync服务器端

为rsync提供配置文件

   /etc/rsyncd.conf

配置文件分两段:

   全局配置段:1个

   共享配置段:多个

1.默认情况下/etc/rsyncd.conf配置文件是不存在的,我们需要手工的建立。



[root@www /]# vim /etc/rsyncd.conf #Global Setting    uid= nobody     gid= nobody     use chroot = no     max connections = 10     strict modes = yes     pid file = /var/run/rsyncd.pid     log file = /var/log/rsyncd.log #Directory to be synced    [shared]     path = /data     ignore errors = yes     read only = no     write only = no     hosts allow = 172.16.0.0/16     host deny = *     list = false     uid = root     gid = root     auth users = aolens    secrets file = /etc/rsyncd.passwd




创建/data目录

创建 /etc/rsyncd.passwd文件存放用户名,密码,,格式如下username:userpasswd

rsync语法 rsync -i_运维_08

service  xinetd  start

xinetd 监听在873/TCP端口

rsync语法 rsync -i_运维_09

将数据推送往服务端共享的目录shared

rsync语法 rsync -i_数据_10

可以看到成功推送过来一个数据

rsync语法 rsync -i_运维_11

将数据从服务端拉取到根目录下。

在服务器端/data/下touch aaa

client成功拉取过来了。

rsync语法 rsync -i_rsync语法_12

rsync+inotify数据监控同步

inotify:在服务器端,可用于定义监控指定的目录下的所有文件,一旦有文件的元数据发生改变,即会通知客户端来拉取;

DESCRIPTION   INOTIFY

The inotify API provides a mechanism for monitoring filesystem

events.  Inotify can be used to monitor individual files, or to
       monitor directories.  When a directory is monitored, inotify will
       return events for the directory itself, and for files inside the
       directory.

通过Inotify可以监控文件系统中添加、删除,修改、移动等各种细微事件,利用这个内核接口,第三方软件就可以监控文件系统下文件的各种变化情况,而inotify-tools就是这样的一个第三方软件。inotify-tools的支持需要内核版本2.6.13以上,我们使用的是2.6.32内核版本。



[root@wwwdata]# ll /proc/sys/fs/inotify/  

total 0  
-rw-r--r-- 1 root root 0 Aug 7 02:07 max_queued_events  
-rw-r--r-- 1 root root 0 Aug 7 02:07 max_user_instances  
-rw-r--r-- 1 root root 0 Aug 7 02:07 max_user_watches




表示默认支持inotify

安装yum install inotify-tools

[root@www data]# rpm -ql inotify-tools 
/usr/bin/inotifywait 
/usr/bin/inotifywatch 
/usr/lib64/libinotifytools.so.0 
/usr/lib64/libinotifytools.so.0.4.1 
/usr/share/doc/inotify-tools-3.14 
/usr/share/doc/inotify-tools-3.14/AUTHORS 
/usr/share/doc/inotify-tools-3.14/COPYING 
/usr/share/doc/inotify-tools-3.14/ChangeLog 
/usr/share/doc/inotify-tools-3.14/NEWS 
/usr/share/doc/inotify-tools-3.14/README 
/usr/share/man/man1/inotifywait.1.gz 
/usr/share/man/man1/inotifywatch.1.gz

inotifywait:用于等待文件或文件集上的一个特定事件,它可以监控任何文件和目录设置,并且可以递归地监控整个目录树。
inotifywatch:用于收集被监控的文件系统统计数据,包括每个inotify事件发生多少次等信息。


介绍一下inotify的相关参数:


/proc interfaces 

   The following interfaces can be used to limit the amount of kernel memory consumed by inotify:

下面的接口可以用来限制内核内存使用inotify:


/proc/sys/fs/inotify/max_queued_events 

    The value in this file is used when an application calls inotify_init(2) to set an upper limit on the number of events that can be queued to the corresponding inotify instance. Events in excess of this limit are dropped, but an IN_Q_OVERFLOW event is always generated. 

表示调用inotify_init时分配给inotify instance中可排队的event的数目的最大值,超出这个值的事件被丢弃,但会触发IN_Q_OVERFLOW事件。


/proc/sys/fs/inotify/max_user_instances 

    This specifies an upper limit on the number of inotify instances that can be created per real user ID. 

 表示每一个real user ID可创建的inotify instatnces的数量上限。


/proc/sys/fs/inotify/max_user_watches 

    This specifies an upper limit on the number of watches that can be created per real user ID.

表示每个inotify instatnces可监控的最大目录数量。如果监控的文件数目巨大,需要根据情况,适当增加此值的大小,例如:echo 30000000 > /proc/sys/fs/inotify/max_user_watches

inotify的相关参数

inotifywait(1)一般命令手动inotifywait(1)

概要         顶部

inotifywait [ -hcmrq ] [ -e <活动>] [ -t <秒数>] [ --format <格式化>]
       [ --timefmt <格式化>] <文件> [...]

选项       

-h,--help
              输出一些有用的使用信息。
       @ <文件>
              当递归地看着一个目录树,排除从指定的文件被监视。必须在指定的文件以根据相对或绝对路径是否相对或绝对路径,
给出了看目录。如果一个特定的路径,明确双方包括和排除,它会一直关注。
       --fromfile <文件>
              读取的文件名来观看或从文件中排除,1名每行。如果文件名以@他们被排除在外,因为如上所述。如果<文件>是' - ',
文件名是从读标准输入。如果你需要看太多,请使用此选项文件传递在作为命令行参数。
       -m,--monitor
              相反,接受一个单一的事件后退出,实行无限期。默认行为是退出后第一个事件发生。 
       -r,--recursive
              观看传递的任何目录的所有子目录参数。手表将设立递归到无限深度。符号链接不运行。新创建的
              子目录也将被关注。
       -q,--quiet
              如果指定了一次,程序会更简洁。具体地说,它不会状态时,它已经完成了建立所有inotify的手表。
       -e <活动>,--event <活动>
              听仅针对特定事件(S)。它可以是事件听了列在活动部分。此选项可被指定多次。如果省略,所有的事件都听了。

退出状态         顶部

0      该计划成功执行,以及事件发生的在被监听。

       1      在执行程序,或一个事件时发生错误发生这是不被监听。后者一般发生如果有事的强行删除inotify的手表,
如监视的文件被删除或文件系统包含监视的文件被卸载。

       2      将-t选项是用来和事件并没有发生在指定的时间间隔。


实现模拟

rsync语法 rsync -i_rsync语法_13

主机A:作为rsync的服务端,提供数据共享,此位置可以作为数据的发布位置。

主机B:作为一个服务节点,需要同步数据给A,且保持一致。是inotify的服务节点。


查看A的配置文件,确保rsync的配置正确:

/etc/rsync.conf



#Global Setting
uid= nobody
gid= nobody
use chroot = no
max connections = 10
strict modes = yes
pid file = /var/run/rsyncd.pid
log file = /var/log/rsyncd.log
#Directory to be synced
[shared]
path = /data
ignore errors = yes
read only = no
write only = no
hosts allow = 172.16.0.0/16
host deny = *
list = false
uid = root
gid = root
auth users = aolens
secrets file = /etc/rsyncd.passwd



/etc/rsync.passwd



aolens:aolens



chkconfig rsync on

service xinetd start

rsync语法 rsync -i_运维_14

配置主机B,inotify-tools

假如我们需要随时监控/var/www/html/目录下是否有文件生成,同步到服务的/data目录下去。

编写一个脚本inotify.sh



#!/bin/bash
#
host1=172.16.249.138         ##服务器端IP
src=/data/                         ##本地数据存放目录
dst=shared                        ##服务端数据的目录
user=aolens                      ##主机A允许推送拉取数据的用户

/usr/bin/inotifywait -mrq  -e modify,delete,create,attrib $src |while read line ;do 
    /usr/bin/rsync -vzrtopg --delete --progress --password-file=/etc/rsyncd.passwd $src $user@$host::$dst

done




chmod +x inotify.sh

vim /etc/rsyncd.passwd



aolens



给与此文件600权限

chmod 600   /etc/rsyncd.passwd

编辑/etc/rc.local   添加一行,让开机自启动

/data/inotifyrsync.sh &

好了,数据的同步就做好了。。。


转载于:https://blog.51cto.com/aolens/1543851