Rsync服务

rsync概述

rsync是类unix系统下的数据镜像备份工具——remote sync。一款支持快速增量备份的工具,支持本地复制,远程同步等,rsync 命令来同步系统文件之前要先登录remote 主机认证,认证过程中用到的协议有2种:ssh 协议和rsync协议。

rsync特性

  1. 能更新整个目录和树和文件系统
  2. 有选择性的保持符号链接、硬链接、文件属性、权限、设备以及时间等
  3. 对于安装来说,无任何特殊权限要求
  4. 对于多个文件来说,内部流水线减少文件等待的延时
  5. 能用rsh、ssh或直接端口作为传输入口端口
  6. 支持匿名rsync同步文件,是理想的镜像工具

同步源

rsync(服务器):

指备份操作的远程服务器,也称为备份源

主要包括两种:rsync源、ssh源

构建ssh同步源

实验环境准备:一台服务器,一台客户端

在服务器和客户端上创建单独的测试目录(/server/ssh、/client/ssh)

Rsync 命令常用法
基本格式: rsync 【选项】 原始位置 目标位置
常用选项:
-a::归档模式,,递归保留对象属性,,等同于 -rlptgoD
-v::显示同步过程的详细信息(verbose)信息
-z::在传输文件是进行压缩((compress)
-H::保留硬链接文件
-A:保留 ACL 属性信息
--delete::删除目标位置有而原始位置没有的文件
-r::递归模式,,包含目录和子目录中所有文件
-l::对于符号连接文件仍然复制为符号连接文件
-p::保留文件的权限标记
-t::保留文件的时间标记
-g::保留文件的属主标记(仅超级用户使用)
-o::保留文件属主标记(仅超级用户使用)
-D::保留设备文件及其他特殊文件
 
 

inotify+rsync实现实时同步

1.1 什么是实时同步:如何实现实时同步

A. 要利用监控服务(inotify),监控同步数据服务器目录中信息的变化

B. 发现目录中数据产生变化,就利用rsync服务推送到备份服务器上

1.2.1 实时同步原理介绍

 

1.3 inotify+rsync 方式实现数据同步                                            

 Inotify是一种强大的,细粒度的。异步的文件系统事件监控机制,linux内核从2.6.13起,加入了 Inotify支持,通过Inotify可以监控文件系统中添加、删除,修改、移动等各种事件,利用这个内核接口,第三方软件就可以监控文件系统下文件的各种变化情况,而 inotify-tools 正是实施这样监控的软件。国人周洋在金山公司也开发了类似的实时同步软件sersync。

提示信息:

sersync软件实际上就是在 inotify软件基础上进行开发的,功能要更加强大些 ,多了定时重传机制,过滤机制了提供接口做 CDN,支持多线程橾作。

 Inotify实际是一种事件驱动机制,它为应用程序监控文件系统事件提供了实时响应事件的机制,而无须通过诸如cron等的轮询机制来获取事件。cron等机制不仅无法做到实时性,而且消耗大量系统资源。相比之下,inotify基于事件驱动,可以做到对事件处理的实时响应,也没有轮询造成的系统资源消耗,是非常自然的事件通知接口,也与自然世界事件机制相符合。

inotify的实现有几款软件: 

inotify-tools,sersync,lrsyncd

1.3.2 inotify+rsync使用方式

inotify 对同步数据目录信息的监控

rsync  完成对数据信息的实时同步

三个重要文件的说明

 

1.4.2 【服务优化】可以将三个文件的数值调大,监听更大的范围

 [root@localhost ~]# cat /etc/sysctl.conf

fs.inotify.max_queued_events = 16384
fs.inotify.max_user_instances = 1024
fs.inotify.max_user_watches = 1048567

1.5 inotify软件介绍及参数说明             

 [root@localhost ~]# yum install -y inotify-tools

 注:    YUM 安装需要有epel源

1.5.2 inotify主要安装的两个软件

inotifywait: (主要)

 在被监控的文件或目录上等待特定文件系统事件(open close delete等)发生,执行后处于阻塞状态,适合在shell脚本中使用

inotifywatch:

收集被监控的文件系统使用的统计数据,指文件系统事件发生的次数统计。

 说明:在实时实时同步的时候,主要是利用inotifywait对目录进行监控

1.5.3 inotifywait命令参数说明

参数   含义

-m, --monitor

(重要参数)

Keep listening for events forever.  Without this option, inotifywait will exit after one event is received.

始终保持事件监听

-d, --daemon         111

-r, --recursive

(重要参数)

Watch all subdirectories of any directories passed as arguments.

递归监控目录数据信息变化

-o, --outfile <file>

Print events to <file> rather than stdout.

打印事件到文件中,相当于标准正确输出

-s, --syslog

Output errors to syslog(3) system log module rather than stderr.

发送错误到syslog相当于标准错误输出

-q, --quiet

(重要参数)

If specified once, the program will be less verbose.  Specifically, it will not state  when  it  has  completed establishing all inotify watches.

输出信息少(只打印事件信息)

--exclude <pattern>

Exclude all events on files matching the extended regular expression <pattern>.

排除文件或目录

--excludei <pattern>

Like --exclude but case insensitive.

排除文件或目录时,不区分大小写

--timefmt <fmt>

(重要参数)

Print using a specified printf-like format string; read the man page for more details.

指定时间输出格式

--format <fmt>

(重要参数)

Print using a specified printf-like formatstring; read the man page for more details.

打印使用指定的输出类似格式字符串;即实际监控输出内容

-e

(重要参数)

Listen for specific event(s).  If omitted, all events are listened for.

指定监听指定的事件,如果省略,表示所有事件都进行监听

以上的信息可以通过 inotifywait --help  获得

1.5.4 -e[参数]  可以指定的事件类型

事件名称 事件说明
access

file or directory contents were read

文件或目录内容被读取

modify

ile or directory contents were writterv

文件或目录内容被写入

attrib

ile or directory attributes changed

文件或目录属性改变

close_write

(重要参数)

file or directory closed, after being opened in writeable mode.

文件或目录关闭,在写入模式打开之后关闭的。

close_nowrite

file or directory closed, after being opened in read-only mode.

文件或目录关闭,在只读模式打开之后关闭的

close

file or directory closed, regardless of read/write mode

文件或目录关闭,不管读或是写模式

open

ile or directory opened

文件或目录被打开

moved_to

file or directory moved to watched directory

文件或目录被移动到监控的目录中

moved_from

file or directory moved from watched directory

文件或目录被移动从监控的目录中

move

(重要参数)

file or directory moved to or from watched directory

文件或目录不管移动到或是移出监控目录都触发事件

create

(重要参数)

file or directory created within watched directory

文件或目录创建在监控目录中

delete

(重要参数)

ile or directory deleted within watched directory

文件或目录被删除在监控目录中

delete_self

file or directory was deleted

文件或目录被删除,目录本身被删除

unmount file system containing file or directory unmounted
以上的信息可以通过 inotifywait --help  获得

 1、创建事件

[root@nfs01 data]# touch test2.txt
[root@nfs01 ~]# inotifywait -mrq  /data --timefmt "%d-%m-%y %H:%M" --format "%T %w%f 事件信息: %e" -e create
17-10-17 11:19 /data/test2.txt 事件信息: CREATE

2、删除事件

[root@nfs01 data]# 
m -f test1.txt
[root@nfs01 ~]# inotifywait -mrq  /data --timefmt "%d-%m-%y %H:%M" --format "%T %w%f 事件信息: %e" -e delete
17-10-17 11:28 /data/test1.txt 事件信息: DELETE

3、修改事件

[root@nfs01 data]# echo "132" > test.txt
[root@nfs01 ~]# inotifywait -mrq  /data --timefmt "%d-%m-%y %H:%M" --format "%T %w%f 事件信息: %e" -e close_write
17-10-17 11:30 /data/test.txt 事件信息: CLOSE_WRITE,CLOSE

4、移动事件 moved_to

[root@nfs01 data]# mv /etc/hosts .
[root@nfs01 ~]# inotifywait -mrq  /data --timefmt "%d-%m-%y %H:%M" --format "%T %w%f 事件信息: %e" -e moved_to
17-10-17 11:33 /data/hosts 事件信息: MOVED_TO

移动事件 moved_from

[root@nfs01 data]# mv ./hosts  /tmp/
[root@nfs01 ~]# inotifywait -mrq  /data --timefmt "%d-%m-%y %H:%M" --format "%T %w%f 事件信息: %e" -e moved_from
17-10-17 11:34 /data/hosts 事件信息: MOVED_FROM

1.5.5 inotifywait 参数 --format <fmt>格式定义参数

命令参数 参数说明
%w(重要参数) 事件出现时,监控文件或目录的名称信息
%f(重要参数) 事件出现时,将显示监控目录下触发事件的文件或目录信息,否则为空
%e(重要参数) 显示发生的事件信息,不同的事件信息用逗号进行分隔
%Xe 显示发生的事件信息,不同的事件信息有x进行分隔,可以修改X为指定分隔符
%T(重要参数)

输出时间格式中定义的时间格式信息,通过 --timefmt option 语法格式指定时间信息

这个格式是通过strftime函数进行匹配时间格式信息的

以上的信息可以通过 inotifywait --help  获得

1.5.6 inotifywait 参数--timefmt <fmt>时间格式参数

命令参数 参数说明
%d(重要参数)

The day of the month as a decimal number(range 01 to 31)

每月的第几天,显示倍息为十进制数(范围是 01-31 )

%m(重要参数)

The month as a decimal number (range 01 to 12).

显示月份,显示信息为十进制(范围 01-12 )

%M

The minute as a decimal number (range 00 to 59).

显示分钟,显示信息为十进制(范围 00-59 )

%y(重要参数)

The year as a decimal number without a century (range 00 to 99).

年份信息,显示信息为十进制,并且没有世纪信息

%Y

The year as a decimal number including the century.

年份信息,显示信息为十进制,并且包含世纪信息

%H

The hour as a decimal number using a 24-hour clock (range 00 to 23).

小时信息,显示信息为十进制,使用 24小时制(范围 00-23 )

以上的信息可以通过 inotifywait --help  获得

1.5.6.1 修改输出的日期格式

[root@nfs01 ~]# inotifywait -mrq  /data --timefmt "%d/%m/%y %H:%M" --format "%T %w%f"
17/10/17 11:12 /data/test1.txt

1.5.7 -e[参数] 重要监控事件参数汇总表:

重要事件 包含事件 备注说明
close

close_write  

close_nowrite

文件或目录关闭,不管读或是写模式

即包含写关闭与读关闭

close_write create 包含文件创建事件,但不包含目录创建事件
move

moved_to  

moved_from

文件或目录不管移动到或是移动出监控目录都触发事件

即包含信息移入或移出监控目录事件

重要参数汇总:根据以上说明,在实际使用时,只要监控以下事件即可

create 创建、 delete 删除、 movedjto 移入、 close_write 修 改

inotifywait -mrq /data --format "%w%f"  -e create,delete,moved_to,close_write

 

原文地址:https://www.cnblogs.com/liujunjun/p/11846078.html