inotify+rsync实现数据实时同步

第1章 数据实时同步介绍

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

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

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

1.2 实现实时同步的方法

inotify+rsync 方式实现数据同步

sersync 方式实现实时数据同步

1.2.1 实时同步原理介绍

未分类

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

1.3.1 Inotify简介

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 部署inotify软件的前提

需要2.6.13以后内核版本才能支持inotify软件。2.6.13内核之后版本,在没有安装inotify软件之前,应该有这三个文件。

[root@backup ~]# ll /proc/sys/fs/inotify/

total 0

-rw-r--r-- 1 root root 0 Oct 17 10:12 max_queued_events

-rw-r--r-- 1 root root 0 Oct 17 10:12 max_user_instances

-rw-r--r-- 1 root root 0 Oct 17 10:12 max_user_watches

1.4.1 三个重要文件的说明

未分类

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

1.4.3 【官方说明】三个重要文件

[root@nfs01 ~]# man proc

/proc/sys/fs/inotify (since Linux 2.6.13)

      This  directory  contains    files    max_queued_events,

      max_user_instances, and max_user_watches, that can be used

      to limit the amount of kernel memory consumed by the  inotify interface. 

for further details, see inotify(7).

通过man手册的第7级别中查到 inotify的默认文件的详细说明。

[root@nfs01 ~]# man 7 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.



/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.



/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.

1.5 inotify软件介绍及参数说明

1.5.1 两种安装方式

1) yum install -y inotify-tools

2) 手工编译安装

注:

YUM 安装需要有epel源

http://mirrors.aliyun.com

手工编译安装方式需要到github上进行下载软件包

inotify软件的参考资料链接:

https://github.com/rvoicilas/inotify-tools/wiki

1.5.2 inotify主要安装的两个软件

inotifywait: (主要)

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

inotifywatch:

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

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

1.5.3 inotifywait命令参数说明

未分类

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

未分类

1.5.4.1 【实例】inotifywait监控中的事件测试

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]# rm -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 格式定义参数

未分类

1.5.6 inotifywait 参数–timefmt 时间格式参数

未分类

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[参数] 重要监控事件参数汇总表:

未分类

1.6 对inotifywait命令的测试

对inotifywait命令测试的说明:

需要打开两个连接窗口

  • 窗口运行inotifywait

  • 窗口对文件夹进行操作,可在一窗口中查看出inotifywait的监控记录

1.6.1 创建文件的逻辑↓

[root@nfs01 ~]# inotifywait /data

Setting up watches.

Watches established.

/data/ CREATE test1.txt

/data/ OPEN test1.txt

/data/ ATTRIB test1.txt

/data/ CLOSE_WRITE,CLOSE test1.txt

创建文件,inotifywait显示创建文件的过程↑

[root@nfs01 data]# touch test1.txt

1.6.2 创建目录逻辑↓

[root@nfs01 data]# mkdir testdir

[root@nfs01 ~]#

/data/ CREATE,ISDIR testdir

1.6.3 监控子目录下的文件↓

[root@nfs01 data]# touch  testdir/test01.txt

[root@nfs01 ~]# inotifywait -mrq  /data

/data/testdir/ OPEN test01.txt

/data/testdir/ ATTRIB test01.txt

/data/testdir/ CLOSE_WRITE,CLOSE test01.txt

1.6.4 sed命令修改逻辑

[root@nfs01 data]# sed 's#132#123#g' test.txt -i



[root@nfs01 ~]# inotifywait -mrq  /data --timefmt "%d-%m-%y %H:%M" --format "%T %w%f 事件信息: %e" -e moved_from

 /data/test.txt 事件信息: OPEN

 /data/sedDh5R8v 事件信息: CREATE

 /data/sedDh5R8v 事件信息: OPEN

 /data/test.txt 事件信息: ACCESS

 /data/sedDh5R8v 事件信息: MODIFY

 /data/sedDh5R8v 事件信息: ATTRIB

 /data/sedDh5R8v 事件信息: ATTRIB

 /data/test.txt 事件信息: CLOSE_NOWRITE,CLOSE

 /data/sedDh5R8v 事件信息: CLOSE_WRITE,CLOSE

 /data/sedDh5R8v 事件信息: MOVED_FROM

 /data/test.txt 事件信息: MOVED_TO

sed命令替换逻辑 :

  • 创建临时文件

  • 将原文件内容放置到临时文件中,修改替换临时文件中的内容,原有文件不做改动

  • 重命名临时文件,覆盖原文件

1.6.5 inotifywait监控中 -e 的参数使用

inotifywait -mrq /data --timefmt "%d/%m/%y %H:%M" --format "%T %w%f 事件信息: %e" -e create

说明:表示只监听create事件

inotifywait -mrq /data --timefmt "%d/%m/%y %H:%M" --format "%T %w%f 事件信息: %e"

说明:不指定-e参数,表示监听所有事件

  • 删除事件delete
    # inotifywait -mrq /data --timefmt "%F %H:%M" --format "%T %w%f 事件信息: %@e" -e delete

    2017-10-17 11:28 /data/02.txt 事件信息: DELETE

    2017-10-17 11:28 /data/03.txt 事件信息: DELETE

    2017-10-17 11:28 /data/04.txt 事件信息: DELETE
  • 修改事件close_write
    # inotifywait -mrq /data --timefmt "%F %H:%M" --format "%T %w%f 事件信息: %@e" -e delete,close_write

    2017-10-17 11:30 /data/oldgirl.txt 事件信息: CLOSE_WRITE@CLOSE

    2017-10-17 11:30 /data/.oldgirl.txt.swx 事件信息: CLOSE_WRITE@CLOSE

    2017-10-17 11:30 /data/.oldgirl.txt.swx 事件信息: DELETE

    2017-10-17 11:30 /data/.oldgirl.txt.swp 事件信息: CLOSE_WRITE@CLOSE

    2017-10-17 11:30 /data/.oldgirl.txt.swp 事件信息: DELETE

    2017-10-17 11:30 /data/.oldgirl.txt.swp 事件信息: CLOSE_WRITE@CLOSE

    2017-10-17 11:30 /data/.oldgirl.txt.swp 事件信息: DELETE
  • 移动事件moved_to
    inotifywait -mrq /data --timefmt "%F %H:%M" --format "%T %w%f 事件信息: %@e" -e delete,close_write,moved_to

    2017-10-17 11:34 /data/hosts 事件信息: MOVED_TO

1.7 实时同步命令参数示意图

未分类

第2章 inotify+rsync实时同步服务部署

2.1 第一个里程碑:部署rsync服务

2.1.1 rsync服务端部署

1)软件是否存在

[root@backup ~]# rpm -qa |grep rsync

rsync-3.0.6-12.el6.x86_64

需求:查询到某个命令非常有用。但是不知道属于哪个软件包

    yum  provides  rysnc

    provides  Find what package provides the given value

2)进行软件服务配置

[root@backup ~]# vim /etc/rsyncd.conf

uid = rsync

gid = rsync

use chroot = no

max connections = 200

timeout = 300

pid file = /var/run/rsyncd.pid

lock file = /var/run/rsync.lock

log file = /var/log/rsyncd.log

ignore errors

read only = false

list = false

hosts allow = 172.16.1.0/24

auth users = rsync_backup

secrets file = /etc/rsync.password

[backup]

comment = "backup dir by oldboy"

path = /backup

[nfsbackup]

comment = "nfsbackup dir by hzs"

path = /nfsbackup

3)创建rsync管理用户

[root@backup ~]# useradd -s /sbin/nologin -M rsync

4)创建数据备份储存目录,目录修改属主

[root@backup ~]# mkdir /nfsbackup/

[root@backup ~]# chown -R rsync.rsync /nfsbackup/

5)创建认证用户密码文件并进行授权600

echo "rsync_backup:oldboy123" >>/etc/rsync.password

chmod 600 /etc/rsync.password

6)启动rsync服务

rsync --daemon

至此服务端配置完成

[root@backup ~]# ps -ef |grep rsync

root      2076      1  0 17:05 ?        00:00:00 rsync --daemon

root      2163  1817  0 17:38 pts/1    00:00:00 grep --color=auto rsync

2.1.2 rsync客户端配置

1)软件是否存在

[root@backup ~]# rpm -qa |grep rsync

rsync-3.0.6-12.el6.x86_64

2)创建安全认证文件,并进行修改权限600

echo "oldboy123" >>/etc/rsync.password

chmod 600 /etc/rsync.password

3) 测试数据传输

[root@nfs01 sersync]# rsync -avz /data  [email protected]::nfsbackup  --password-file=/etc/rsync.password

sending incremental file list

data/

data/.hzs

data/.tar.gz

data/.txt

2.2 第二个里程碑:部署inotify服务

首先先确认是否有epel源用来安装inotify-tools软件

[root@nfs01 ~]# yum repolist

Loaded plugins: fastestmirror, security

Loading mirror speeds from cached hostfile

 * base: mirrors.aliyun.com

 * epel: mirrors.aliyun.com

 * extras: mirrors.aliyun.com

 * updates: mirrors.aliyun.com

repo id  repo name                                      status

base    CentOS-6 - Base - mirrors.aliyun.com            6,706

epel    Extra Packages for Enterprise Linux 6 - x86_64  12,401

extras  CentOS-6 - Extras - mirrors.aliyun.com              46

updates  CentOS-6 - Updates - mirrors.aliyun.com            722

repolist: 19,875

2.2.1 安装inotify软件

两种安装方式

1) yum install -y inotify-tools

2) 手工编译安装

注:

手工编译安装方式需要到github上进行下载软件包

inotify软件的参考资料链接:

https://github.com/rvoicilas/inotify-tools/wiki

2.2.2 查看inotify安装上的两个命令(inotifywait,inotifywatch)

[root@nfs01 ~]# rpm -ql inotify-tools

/usr/bin/inotifywait      #主要

/usr/bin/inotifywatch

2.2.2.1 inotifywait和inotifywatch的作用:

一共安装了2个工具(命令),即inotifywait和inotifywatch

inotifywait : 在被监控的文件或目录上等待特定文件系统事件(open close delete等)发生,

执行后处于阻塞状态,适合在shell脚本中使用

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

说明:yum安装后可以直接使用,如果编译安装需要进入到相应软件目录的bin目录下使用

#命令 man手册说明

# man inotifywait

inotifywait - wait for changes to files using inotify

使用inotify进行监控,等待产生变化的文件信息

# man inotifywatch

inotifywatch - gather filesystem access statistics using inotify

使用inotify进行监控,收集文件系统访问统计佶息

2.3 第三个里程碑:编写脚本,实现rsync+inotify软件功能结合

2.3.1 rsync服务命令

rsync -avz --delete /data/ [email protected]::nfsbackup --password-file=/etc/rsync.password

2.3.2 inotify服务命令:

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

2.3.3 编写脚本

[root@nfs01 sersync]# vim /server/scripts/inotify.sh
#!/bin/bash
inotifywait -mrq /data --format "%w%f" -e create,delete,moved_to,close_write|
while read line
do
        rsync -az --delete /data/ [email protected]::nfsbackup --password-
file=/etc/rsync.password
done

脚本说明:

for循环会定义一个条件,当条件不满足时停止循环

while循环:只要条件满足就一直循环下去

2.3.4 对脚本进行优化

#!/bin/bash

Path=/data
backup_Server=172.16.1.41


/usr/bin/inotifywait -mrq --format '%w%f' -e create,close_write,delete /data  | while read line  
do
    if [ -f $line ];then
        rsync -az $line --delete rsync_backup@$backup_Server::nfsbackup --password-file=/etc/rsync.password
    else
        cd $Path &&
        rsync -az ./ --delete rsync_backup@$backup_Server::nfsbackup --password-file=/etc/rsync.password
    fi

done

2.4 第四个里程碑:测试编写的脚本

2.4.1 让脚本在后台运行

在/data 目录先创建6个文件

[root@nfs01 data]# sh  /server/scripts/inotify.sh &

[root@nfs01 data]# touch {1..6}.txt

在backup服务器上,已经时候同步过去了6个文件。

[root@backup ~]# ll /nfsbackup/

total 8

-rw-r--r-- 1 rsync rsync 0 Oct 17 12:06 1.txt

-rw-r--r-- 1 rsync rsync 0 Oct 17 12:06 2.txt

-rw-r--r-- 1 rsync rsync 0 Oct 17 12:06 3.txt

-rw-r--r-- 1 rsync rsync 0 Oct 17 12:06 4.txt

-rw-r--r-- 1 rsync rsync 0 Oct 17 12:06 5.txt

-rw-r--r-- 1 rsync rsync 0 Oct 17 12:06 6.txt

2.5 利用while循环语句编写的脚本停止方法(kill)

  1. ctrl+z暂停程序运行,kill -9杀死

  2. 不要暂停程序,直接利用杀手三剑客进行杀进程

说明:kill三个杀手不是万能的,在进程暂停时,无法杀死;kill -9 (危险)

2.5.1 查看后台都要哪些程序在运行

[root@nfs01 data]# jobs

[1]+  Running                sh /server/scripts/inotify.sh &

2.5.2 fg将后台的程序调到前台来

[root@nfs01 data]# fg 1

sh /server/scripts/inotify.sh

2.6 进程的前台和后台运行方法:

    fg    -- 前台

    bg    -- 后台

2.6.1 脚本后台运行方法

    01. sh inotify.sh &

    02. nohup sh inotify.sh &

    03. screen实现脚本程序后台运行
sh /server/scripts/inotify.sh &

nohup

nohup sh inotify.sh &

2.7 screen实现脚本程序后台运行

2.7.1 经过yum查找发现screen命令属于screen包

[root@test ~]# yum provides screen

Loaded plugins: fastestmirror, security

Loading mirror speeds from cached hostfile

 * base: mirrors.aliyun.com

 * epel: mirrors.aliyun.com

 * extras: mirrors.aliyun.com

 * updates: mirrors.aliyun.com

base                                                      | 3.7 kB    00:00   

epel                                                      | 4.3 kB    00:00   

extras                                                    | 3.4 kB    00:00   

updates                                                  | 3.4 kB    00:00   

screen-4.0.3-19.el6.x86_64 : A screen manager that supports multiple logins on

                          : one terminal

Repo        : base

Matched from:

2.7.2 安装screen软件

[root@test ~]# yum install -y  screen

2.7.3 screen命令的参数

在shell中输入 screen即可进入screen 视图

[root@test ~]# screen

Screen实现后台运行程序的简单步骤:

  screen -ls :可看screen会话

  screen -r ID :指定进入哪个screen会话

Screen命令中用到的快捷键

  Ctrl+a c :创建窗口

  Ctrl+a w :窗口列表

  Ctrl+a n :下一个窗口

  Ctrl+a p :上一个窗口

  Ctrl+a 0-9 :在第0个窗口和第9个窗口之间切换

  Ctrl+a K(大写) :关闭当前窗口,并且切换到下一个窗口 ,

(当退出最后一个窗口时,该终端自动终止,并且退回到原始shell状态)

  exit :关闭当前窗口,并且切换到下一个窗口

(当退出最后一个窗口时,该终端自动终止,并且退回到原始shell状态)

  Ctrl+a d :退出当前终端,返回加载screen前的shell命令状态

  Ctrl+a " : 窗口列表不同于w

2.8 sersync软件实现实时同步

http://www.linuxidc.com/Linux/2017-10/147899.htm

本博文中所使用的系统版本为: CentOS release 6.9 (Final) 内核版本为: 2.6.32-696.10.1.el6.x86_64 望读者注意!

Linux下rsync数据同步工具简易配置

rsync 服务端配置步骤

创建配置文件:

文件本身不存在需要手动创建

vi /etc/rsyncd.conf
#Rsync server 
#created by kendall 2017.10.18
##rsyncd.conf start##
uid = rsync                     #客户端连过来具有什么权限
gid = rsync
use chroot = no                 #安全相关,程序出bug开启有好处
max connections = 2000          #最大客户端连接数
timeout = 300                   #超时断开时间
pid file = /var/run/rsyncd.pid  #daemon进程号记录
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log  #日志文件位置
ignore errors                   #忽略错误
read only = false               #只读 假的(可读写)
list = false                    #不可以查看服务端列表
hosts allow = 172.16.1.0/24     #允许IP段
#hosts deny = 0.0.0.0/32        #拒绝
auth users = rsync_backup       #远程连接的用户(纯虚拟用户,不是系统用户)
secrets file = /etc/rsync.password #存放用户密码的文件位置
[backup]                        #第一个模块
path = /backup                  #共享的目录
[oldboy]                        #第二个模块
path = /data                    #共享的目录

创建用户,及共享目录

useradd rsync -s /sbin/nologin -M
id rsync
mkdir /backup /data
chown -R rsync.rsync /backup/ /data/

创建密码文件

echo "rsync_backup:654321" >/etc/rsync.password
chmod 600 /etc/rsync.password

启动rsync

rsync --daemon
netstat -lntup|grep rsync
ps -ef|grep rsync|grep -v grep

加入开机自启动

echo "rsync --daemon" >>/etc/rc.local
cat /etc/rc.local

rsync 客户端配置步骤

创建密码文件

echo "654321" >/etc/rsync.password
chmod 600 /etc/rsync.password
ll /etc/rsync.password
cat /etc/rsync.password

推送文件测试

rsync -avz /tmp/ rsync_backup@server_ip::backup --password-file=/etc/rsync.password
rsync -avz /tmp/ rsync://rsync_backup@servr_ip/backup/tmp/  --password-file=/etc/rsync.password

相关其他内容

rsync服务端配置修改后,需要重启rsync

pkill rsync
rsync --daemon
lsof -i:873

推送时,排除特定文件

rsync -avz --exlude={a,b} /backup/ rsync_backup@server_ip::backup --password-file=/etc/rsync.password
rsync -avz --exlude={a..g} /backup/ rsync_backup@server_ip::backup --password-file=/etc/rsync.password
rsync -avz --exlude-from=paichu.log /backup/ rsync_backup@server_ip::backup --password-file=/etc/rsync.password

无差异同步,增量同步千万别用

rsync -avz --delete /backup/ rsync_backup@server_ip::backup --password-file=/etc/rsync.password

限速推送

dd if=/dev/zero of=test1 bs=1M count=128
rsync -avz --bwlimit=100 /tmp/ rsync_backup@server_ip::backup --password-file=/etc/rsync.password

rsync的三种工作模式

本地   cp
通道   rsync -avzP -e 'ssh -p22' /etc root@server_ip:/tmp/
daemon 内网明文传输

题外:操作文件前的备份方法:

cp /etc/rsyncd.conf{,.bak}

使用rsync搭建centos的镜像站

简介

自己一直以来相搭建一个开源镜像站,一方面可以了解搭建镜像站的知识,一方面可以同步那些国内没有的linux发行版软件源,但是最主要的原因只是为了好玩

注意点

我这个教程不是专业教程,但是要注意的是镜像站是一个对I/O要求很高,网络带宽要求很高,磁盘占用量的站点,不然没人用

步骤

  • 安装需要的软件(nginx,rsync)
  • 配置nginx
  • 编写同步脚本

编译安装nginx

  • 安装PCRE库

下载解压

wget https://ftp.pcre.org/pub/pcre/pcre-8.41.tar.gz
tar -zxvf pcre-8.41.tar.gz
cd pcre-8.41

编译安装

./configure --prefix=/usr/local/pcre
make
make install
  • 安装zlib

下载解压

wget http://zlib.net/zlib-1.2.11.tar.gz
tar -zxvf zlib-1.2.11.tar.gz
cd zlib-1.2.11

编译安装

./configure --prefix=/usr/local/zlib
make
make install
  • 安装openssl库
wget https://www.openssl.org/source/openssl-1.0.2l.tar.gz
tar -zxvf openssl-1.0.2l.tar.gz
cd openssl-1.0.2l

编译安装

./config --prefix=/usr/local/openssl
make
make install
  • 安装nginx

下载解压

wget http://mirrors.sohu.com/nginx/nginx-1.12.1.tar.gz
tar -zxvf nginx-1.12.1.tar.gz
cd nginx-1.12.1

编译安装

./configure --prefix=/usr/local/nginx --with-pcre=/usr/local/pcre/ --with-zlib=/usr/local/zlib/ --with-openssl=/usr/local/openssl/
make

报错

[root@bboysoul nginx-1.12.1]# make
make -f objs/Makefile
make[1]: Entering directory `/root/nginx-1.12.1'
cd /usr/local/pcre/ 
&& if [ -f Makefile ]; then make distclean; fi 
&& CC="cc" CFLAGS="-O2 -fomit-frame-pointer -pipe " 
./configure --disable-shared 
/bin/sh: line 2: ./configure: No such file or directory
make[1]: *** [/usr/local/pcre//Makefile] Error 127
make[1]: Leaving directory `/root/nginx-1.12.1'
make: *** [build] Error 2

百度了一下,原来–with-pcre要指定的不是安装目录而是源码目录

./configure --prefix=/usr/local/nginx --with-pcre=/root/pcre-8.41 --with-zlib=/usr/local/zlib/ --with-openssl=/usr/local/openssl/
make

又报错

src/http/modules/ngx_http_log_module.c:13:18: fatal error: zlib.h: No such file or directory
 #include <zlib.h>
                  ^
compilation terminated.
make[1]: *** [objs/src/http/modules/ngx_http_log_module.o] Error 1
make[1]: Leaving directory `/root/nginx-1.12.1'
make: *** [build] Error 2

直接指定zlib的源码目录好了

./configure --prefix=/usr/local/nginx --with-pcre=/root/pcre-8.41 --with-zlib=/root/zlib-1.2.11 --with-openssl=/usr/local/openssl/
make
make install

成功

  • 测试一下

如果你这个时候还是访问不了你的nginx,出现ERR_ADDRESS_UNREACHABLE这个错误,你要检查一下你的防火墙,可以配置也可以选择关闭

systemctl stop firewalld.service

其实到这一步的时候我已经忘记我在干什么了,看了一下标题,哦!原来我在做一个镜像站,,,,,

安装rsync

yum install rsync

配置

之后,配置一下站点

在home文件夹下创建一个站点文件来存放同步过来的资源

mkdir /home/mirror

之后修改nginx.conf的server段为

    server {
        listen       80;
        server_name  localhost;
        root    /home/mirror;
        location / {    
                autoindex on;
                autoindex_exact_size off;
                autoindex_localtime on;
        }

    }

重启一下服务器

[root@bboysoul sbin]# pidof nginx
31632 31631
[root@bboysoul sbin]# kill 31632 31631
[root@bboysoul sbin]# pidof nginx
[root@bboysoul sbin]# ./nginx
[root@bboysoul sbin]# 

接着你去访问站点,看到的就会是下面这样,因为打开了nginx的目录浏览功能autoindex

Index of /
../

安装createrepo

yum install createrepo

因为centos的软件包目录有点多,而且大,所以为了方便我就只同步epel软件源了

mkdir epel
cd epel
mkdir 7

之后编写一个同步脚本

rsync -avz rsync://mirrors.tuna.tsinghua.edu.cn/epel/7/SRPMS/ /home/mirror/epel/7/SRPMS/
createrepo /home/mirror/epel/7/SRPMS/

rsync -avz rsync://mirrors.tuna.tsinghua.edu.cn/epel/7/aarch64/ /home/mirror/epel/7/aarch64/
createrepo /home/mirror/epel/7/aarch64/

rsync -avz rsync://mirrors.tuna.tsinghua.edu.cn/epel/7/ppc64/ /home/mirror/epel/7/ppc64/
createrepo /home/mirror/epel/7/ppc64/

rsync -avz rsync://mirrors.tuna.tsinghua.edu.cn/epel/7/ppc64le/ /home/mirror/epel/7/ppc64le/
createrepo /home/mirror/epel/7/ppc64le/

rsync -avz rsync://mirrors.tuna.tsinghua.edu.cn/epel/7/x86_64/ /home/mirror/epel/7/x86_64/
createrepo /home/mirror/epel/7/x86_64/

echo "bboysoul done"

赋予执行权限

sudo chmod +x rsync.sh

安装screen

yum install screen

建立一个新的会话

screen -S rsync

执行脚本

./rsync.sh

ctrl+a+d退出

你可以安装bwm-ng来监控网速,我这边的状态是这样的

同步时间很久的,注意磁盘有没有被占满

同步时间肯定很久。

rsync工具远程数据同步备份

基本信息:

  • A:192.168.1.10 源服务器(源数据服务器)
  • B:192.168.1.20 目的服务器(备份存放服务器)

两台服务器系统均为CentOS

1、在A服务器配置rsync服务端

安装rsync

#yum install rsync

编辑配置文件

#vim /etc/rsyncd.conf
[global]
uid=nobody
gid=nobody
use chroot = yes
max connections = 4
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsyncd.lock
log file = /var/log/rsyncd.log
Timeout = 600
Log format = %t %a %m %f %b

[backup]
path=/tmp/data_bak
ignore errors
read only = yes
list = no
auth users = backupuser
secrets file = /etc/rsyncd/rsyncd.secrets
hosts allow = 192.168.1.20
hosts deny = 0.0.0.0/0

创建密码文件,采用这种方式不能使用系统用户对客户端进行认证,所以需要创建一个密码文件,其格式为“username:password”,用户名可以和密码可以随便定义,不要和系统帐户重名,同时要把创建的密码文件权限设置为600,一行一个账号密码,账号与密码用:号隔开

#echo "back:abc123" > /etc/rsyncd.secrets
#chmod 600 /etc/rsyncd.secrets

设置备份数据目录权限为755

#chmod -R 755 /tmp/data_bak

开启防火墙并重启

#iptables -A INPUT -p tcp -m state --state NEW -m tcp --dport 873 -j ACCEPT
#service iptables save
#service iptables restart

启动rsyncd服务

#/usr/bin/rsync --daemon --config=/etc/rsyncd.conf

2、在B服务器配置rsync客户端

这里很简单,只要安装和配置连接密码即可

#yum install rsync
#echo "abc123" > /etc/rsyncd.secrets

执行备份命令

#rsync -vrtopg --password-file=/etc/rsyncd.secrets [email protected]::backup /home/data_bak

rsync 命令的选项含义参考:

http://man.linuxde.net/rsync

这里执行这个命令可能会报一些错误,解决办法如下:

  • 问题1:
@ERROR: chdir failed
rsync error: error starting client-server protocol (code 5) at main.c(1503) [receiver=3.0.6]

原因:服务器端配置文件/etc/rsyncd.conf中use chroot = yes是否配置

  • 问题2:
@ERROR: chroot failed
rsync error: error starting client-server protocol (code 5) at main.c(1503) [receiver=3.0.6]

原因:
服务器端的目录不存在或无权限,创建目录并修正权限可解决问题。注意查看/etc/rsyncd.conf中的path是否配置正确。

  • 问题3:
rsync: opendir "data/2017-08-29" (in backup) failed: Permission denied (13)

原因:
注意查看服务器端同步的目录权限是否为755。

将备份命令加入任务计划

#crontab -e
* * */1 * * rsync -vrtopg --password-file=/etc/rsyncd.secrets [email protected]::backup /home/data_bak

rsync通过exclude排除多文件/目录实例

说明:

使用rsync -av –exclude=upload /home/mysql/backup /home/mysql/backup2/ 只能排除upload文件/目录。但如果要排除多个文件/目录,就需要新建个exclude.list,然后rsync -av –exclude-from=”exclude.list”指定不需要同步的文件/目录

实现:

# rsync -av --exclude-from=/root/exclude.list /home/mysql/backup /home/mysql/backup2/
//将/home/mysql/backup目录拷贝到/home/mysql/backup2目录下,/root/exclude.list中指定文件不拷贝。

注意exclude.list里面填写要排除的文件/目录,一行一个,直接写文件名即可。这里,可以把/home/mysql/backup看成根目录,所以如果要排除a,b.1,b.2,tmp/g,那么exclude.list里就应该写

a
b.*
tmp/g

而不是填写以下完整url。不然这样还是会同步a,b.1,b.2,tmp/g

/home/mysql/backup/a
/home/mysql/backup/b.*
/home/mysql/backup/tmp/g

当然以下这种写法也是会同步a,b.1,b.2,tmp/g文件的,也是有问题

./home/mysql/backup/a
./home/mysql/backup/b.*
./home/mysql/backup/tmp/g

CentOS6.8使用rsync sersync实现数据实时同步

Sersync简介

Sersync利用inotify与rsync对服务器进行实时同步,其中inotify用于监控文件系统事件,rsync是目前广泛使用的同步算法,其优点是只对文件不同的部分进行操作,所以其优势大大超过使用挂接文件系统的方式进行镜像同步。由金山的周洋开发完成,是目前使用较多的文件同步工具之一。该工具和其他的工具相比有如下优点:

  • sersync是使用c++编写,由于只同步发生更改的文件,因此比其他同步工具更节约时间、带宽;

  • 安装方便、配置简单;

  • 使用多线程进行同步,能够保证多个服务器实时保持同步状态;

  • 自带出错处理机制,通过失败队列对出错的文件重新出错,如果仍旧失败,则每10个小时对同步失败的文件重新同步;

  • 自带crontab功能,只需在xml配置文件中开启,即可按您的要求,隔一段时间整体同步一次;
    自带socket与http协议扩展,你可以方便的进行二次开发;

rsync+sersync与rsync+Inotify-tools区别

Inotify-tools只能记录下被监听的目录发生了变化(包括增加、删除、修改),并没有把具体是哪个文件或者哪个目录发生了变化记录下来;

rsync在同步的时候,并不知道具体是哪个文件或者哪个目录发生了变化,每次都是对整个目录进行同步,当数据量很大时,整个目录同步非常耗时(rsync要对整个目录遍历查找对比文件),因此,效率很低。

sersync是基于Inotify开发的,类似于Inotify-tools的工具;

sersync可以记录下被监听目录中发生变化的(包括增加、删除、修改)具体某一个文件或某一个目录的名字;

rsync在同步的时候,只同步发生变化的这个文件或者这个目录(每次发生变化的数据相对整个同步目录数据来说是很小的,rsync在遍历查找比对文件时,速度很快),因此,效率很高。

所以,当同步的目录数据量不大时,建议使用Rsync+Inotify-tools;当数据量很大(几百G甚至1T以上)、文件很多时,建议使用Rsync+sersync。

地址:http://code.google.com/p/sersync/,要翻墙才能下载

http://download.csdn.net/detail/hellopengyl/9918625

安装、配置

和Inotify-tools一样,只需要在数据源端安装

1、查看服务器内核是否支持inotify

[root@localhost src]# ll /proc/sys/fs/inotify    #查看服务器内核是否支持inotify,出现下面的内容,说明服务器内核支持inotify
total 0
-rw-r--r-- 1 root root 0 Jul 27 10:32 max_queued_events
-rw-r--r-- 1 root root 0 Jul 27 10:32 max_user_instances
-rw-r--r-- 1 root root 0 Jul 27 10:32 max_user_watches
[root@localhost src]# uname -r        #Linux下支持inotify的内核最小为2.6.13
2.6.32-642.el6.x86_64
[root@localhost src]# sysctl -a|egrep -i "max_queued_events|max_user_watches|max_user_instances"    #修改inotify默认参数(inotify默认内核参数值太小)
fs.inotify.max_user_instances = 128
fs.inotify.max_user_watches = 8192
fs.inotify.max_queued_events = 16384
fs.epoll.max_user_watches = 201420
[root@localhost src]# vim /etc/sysctl.conf
fs.inotify.max_user_instances = 65535
fs.inotify.max_user_watches = 99999999
fs.inotify.max_queued_events = 99999999    
[root@localhost src]# cat /proc/sys/fs/inotify/{max_user_instances,max_user_watches,max_queued_events}
65535
99999999
99999999
[root@localhost src]#
  • max_queued_events inotify队列最大长度,如果值太小,会出现”** Event Queue Overflow **”错误,导致监控文件不准确

  • max_user_watches 要同步的文件包含多少目录,可以用:find /home/www.osyunwei.com -type d | wc -l统计,必须保证max_user_watches值大于统计结果(这里/home/www.osyunwei.com为同步文件目录)

  • max_user_instances 每个用户创建inotify实例最大值

2、安装、配置sersync

[root@localhost src]# tar zxvf sersync2.5.4_64bit_binary_stable_final.tar.gz
[root@localhost src]# mv GNU-Linux-x86 /app/sersync
[root@localhost src]# cd /app/sersync/
[root@localhost sersync]# ls
confxml.xml  sersync2
[root@localhost sersync]# cp confxml.xml{,.default}
[root@localhost sersync]# vim confxml.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<head version="2.5">
    <host hostip="localhost" port="8008"></host>    #针对插件的保留字段,保留默认即可。
    <debug start="true"/>    #在sersync正在运行的控制台,打印inotify,rsync同步命令
    <fileSystem xfs="false"/>    #对于xfs文件系统用户,需要将这个选项开启才正常工作
    <filter start="false">    #过滤系统的临时文件,被过滤的文件不会被监控提高,默认过滤系统的临时文件(以“.”开头,以“~”结尾)
    <exclude expression="(.*).svn"></exclude>
    <exclude expression="(.*).gz"></exclude>
    <exclude expression="^info/*"></exclude>
    <exclude expression="^static/*"></exclude>
    </filter>
    <inotify>    #inotify监控文件模块
    <delete start="true"/>    #如果本地文件删除,不需要删除远程段的文件可以设置成false
    <createFolder start="true"/> #如果将createFolder设为false,则不会对产生的目录进行监控,该目录下的子文件与子目录也不会被监控;
    <createFile start="false"/>  #把createFile(监控文件事件选项)设置为false来提高性能,减少rsync通讯;因为拷贝文件到监控目录会产生create事件与close_write事件,所以如果关闭create事件,只监控文件拷贝结束时的时间close_write,同样可以实现文件完整同步;
    <closeWrite start="true"/>
    <moveFrom start="true"/>
    <moveTo start="true"/>
    <attrib start="false"/>
    <modify start="false"/>
    </inotify>

    <sersync>    #进行数据同步的模块
    <localpath watch="/app/rsync_client">    #定义本地要同步的目录
        <remote ip="10.15.43.100" name="app_rsync_server"/>#远程接受同步的IP和rsync模块名
        <!--<remote ip="192.168.8.39" name="tongbu"/>-->
        <!--<remote ip="192.168.8.40" name="tongbu"/>-->
    </localpath>
    <rsync>
        <commonParams params="-artuz"/>    #自定义rsync参数,默认是-artuz
        <auth start="true" users="rsync" passwordfile="/etc/rsyncd.secret"/>    #开启用户认证,定义用户名密码
        <userDefinedPort start="false" port="874"/><!-- port=874 -->
        <timeout start="false" time="100"/><!-- timeout=100 -->
        <ssh start="false"/>    #开启会使用rsync -e ssh的方式进行传输
    </rsync>
    <failLog path="/tmp/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once-->  #对于失败的传输,会进行重新传送,再次同步失败后日志记录到/tmp/rsync_fail_log.sh,并且每60分钟对失败的log进行重新同步
    <crontab start="true" schedule="600"><!--600mins-->    #每隔600s会做一次完全同步
        <crontabfilter start="false">    #如果开启了filter文件过滤功能,那么crontab整体同步也需要设置过滤,否则虽然实时同步的时候文件被过滤了,但crontab整体同步的时候 如果不单独设置crontabfilter,还会将需过滤的文件同步到远程,
        <exclude expression="*.php"></exclude> #crontab的过滤正则与filter过滤的不同,果同时开启了filter与crontab,则要开启crontab的crontabfilter,并按示例设置使其与filter的过滤一一对应。
        <exclude expression="info/*"></exclude>
        </crontabfilter>
    </crontab>
        <plugin start="false" name="command"/>    #当设置为true的时候,将文件同步到远程服务器后会调用name参数指定的插件。
    </sersync>

    <plugin name="command">    #name指定的插件
    #当文件同步完成后,会调用command插件,例如同步文件是file.txt,file.txt文件在改动之后,调用rsync同步到远程服务器后,调用command插件,执行/bin/sh file.txt suffix >/dev/null 2>&1
    #如果suffix 设置了,则会放在inotify事件file.txt之后,如果ignoreError为true,则会添加>/dev/null 2>&1
    <param prefix="/bin/sh" suffix="" ignoreError="true"/> <!--prefix /opt/tongbu/mmm.sh suffix-->
    <filter start="false">
        <include expression="(.*).php"/>    #当filter为ture,include可以只对正则匹配到的文件,调用command。
        <include expression="(.*).sh"/>
    </filter>
    </plugin>
        #http插件,可以向指定域名的主机post,inotify监控的事件。
        #socket插件,开启该模块,则向指定ip与端口发送inotify所产生的文件路径信息
    <plugin name="socket">    
    <localpath watch="/opt/tongbu">
        <deshost ip="192.168.138.20" port="8009"/>
    </localpath>
    </plugin>
    #在同步过程中将文件发送到目的服务器后刷新cdn接口。如果不想使用,则设置<plugin start="false" name="refreshCDN"/>
    #该模块根据chinaCDN的协议,进行设计,当有文件产生的时候,就向cdn接口发送需要刷新的路径位置
    <plugin name="refreshCDN">    
    <localpath watch="/data0/htdocs/cms.xoyo.com/site/">  #需要监控的目录
            #cdnifo标签制定了cdn接口的域名,端口号,以及用户名与密码。
        <cdninfo domainname="ccms.chinacache.com" port="80" username="xxxx" passwd="xxxx"/>
        #sendurl标签是需要刷新的url的前缀
        <sendurl base="
        #regexurl标签中的,regex属性为true时候,使用match属性的正则语句匹配inotify返回的路径信息,并将正则匹配到的部分作为url一部分,
        <regexurl regex="false" match="cms.xoyo.com/site([/a-zA-Z0-9]*).xoyo.com/images"/>
            #如果产生文件事件为:/data0/htdoc/cms.xoyo.com/site/jx3.xoyo.com/image/a/123.txt
            #经过上面的match正则匹配后,最后刷新的路径是:http://pic.xoyo.com/cms/jx3/a/123.txt;
            #如果regex属性为false,最后刷新的路径是http://pic.xoyo.com/cms/jx3.xoyo.com/images/a/123.txt;
    </localpath>
    </plugin>
</head>
[root@localhost sersync]# /app/sersync/sersync2 -d -r -n 8 -o /app/sersync/confxml.xml
set the system param
execute:echo 50000000 > /proc/sys/fs/inotify/max_user_watches
execute:echo 327679 > /proc/sys/fs/inotify/max_queued_events
parse the command param
option: -d    run as a daemon
option: -r    rsync all the local files to the remote servers before the sersync work
option: -n    thread num is:  8
option: -o    config xml name:  /app/sersync/confxml.xml
parse xml config file
host ip : localhost  host port: 8008
Open debug, you will see debug infomation 
daemon start,sersync run behind the console 
Start the crontab    Every 600 minutes rsync all the files to the remote servers entirely
use rsync password-file :
user is    rsync
passwordfile is   /etc/rsyncd.secret
config xml parse success
please set /etc/rsyncd.conf max connections=0 Manually
sersync working thread 10  = 1(primary thread) + 1(fail retry thread) + 8(daemon sub threads) 
Max threads numbers is: 18 = 10(Thread pool nums) + 8(Sub threads)
please according your cpu ,use -n param to adjust the cpu rate
------------------------------------------
rsync the directory recursivly to the remote servers once
working please wait...
execute command: cd /app/rsync_client && rsync -artuz -R --delete ./ [email protected]::app_rsync_server --password-file=/etc/rsyncd.secret
crontab command:cd /app/rsync_client && rsync -artuz -R --delete ./ [email protected]::app_rsync_server --password-file=/etc/rsyncd.secret
run the sersync: 
watch path is: /app/rsync_client
add watch: /app/rsync_client return wd is: 1
add watch: /app/rsync_client/test return wd is: 2
[root@localhost sersync]#
  • -d 后台启动

  • -r 同步前将已存在的文件全部同步过去,如果设置了过滤器,即在xml文件中,filter为true,则暂时不能使用-r参数进行整体同步;

  • -n 开启的线程总数默认10

  • -o 指定配置文件,指定 -o 参数可以指定多个不同的配置文件,从而实现sersync多进程多实例的数据同步

  • -m 不进行同步,只运行插件 ./sersync -m pluginName 例如:./sersync -m command,则在监控到事件后,不对远程目标服务器进行同步,而是直接运行command插件

[root@localhost sersync]# cat monitor_sersync.sh #监控Sersync运行状态的脚本,如果服务停止了就重启服务
#!/bin/bash
server_file="/app/sersync/sersync2"
conf_file="/app/sersync/confxml.xml"
options="-d -r -n 8 -o"
proc_num=$(ps -ef|grep -i sersync2|grep -v "grep"|wc -l)
if [ $proc_num -lt 1 ];then
    cd $(dirname $server_file)
    nohup $server_file $options $conf_file &
else
   exit 0;
fi
[root@localhost sersync]# chmod +x monitor_sersync.sh 
[root@localhost sersync]# crontab -l
*/5 * * * *  /app/sersync/monitor_sersync.sh
[root@localhost sersync]# /etc/init.d/crond restart
[root@localhost sersync]# vim /etc/rc.d/rc.local    #设置开机自动运行脚本
/app/sersync/sersync2 -d -r -n 8 -o /app/sersync/confxml.xml
[root@localhost sersync]#

测试

在客户端监控的目录/app/rsync_client创建文件,然后查看服务器端app_rsync_server模块对应的目录是否同步更新

[root@localhost rsync_client]# touch file{1..9}
inotify wd:1   name:file1  mask:256
inotify wd:1   name:file1  mask:8
inotify wd:1   name:file2  mask:256
inotify wd:1   name:file2  mask:8
inotify wd:1   name:file3  mask:256
inotify wd:1   name:file3  mask:8
inotify wd:1   name:file4  mask:256
inotify wd:1   name:file4  mask:8
inotify wd:1   name:file5  mask:256
[root@localhost rsync_client]# cd /app/rsync_client && rsync -artuz -R "./file2" [email protected]::app_rsync_server --password-file=/etc/rsyncd.secret
inotify wd:1   name:file5  mask:8
inotify wd:1   name:file6  mask:256
inotify wd:1   name:file6  mask:8
inotify wd:1   name:file7  mask:256
inotify wd:1   name:file7  mask:8
inotify wd:1   name:file8  mask:256
inotify wd:1   name:file8  mask:8
inotify wd:1   name:file9  mask:256
inotify wd:1   name:file9  mask:8
cd /app/rsync_client && rsync -artuz -R "./file1" [email protected]::app_rsync_server --password-file=/etc/rsyncd.secret
cd /app/rsync_client && rsync -artuz -R "./file3" [email protected]::app_rsync_server --password-file=/etc/rsyncd.secret
cd /app/rsync_client && rsync -artuz -R "./file4" [email protected]::app_rsync_server --password-file=/etc/rsyncd.secret
cd /app/rsync_client && rsync -artuz -R "./file5" [email protected]::app_rsync_server --password-file=/etc/rsyncd.secret
cd /app/rsync_client && rsync -artuz -R "./file6" [email protected]::app_rsync_server --password-file=/etc/rsyncd.secret
cd /app/rsync_client && rsync -artuz -R "./file7" [email protected]::app_rsync_server --password-file=/etc/rsyncd.secret
cd /app/rsync_client && rsync -artuz -R "./file8" [email protected]::app_rsync_server --password-file=/etc/rsyncd.secret
cd /app/rsync_client && rsync -artuz -R "./file9" [email protected]::app_rsync_server --password-file=/etc/rsyncd.secret
[root@localhost rsync_client]# touch file1
inotify wd:1   name:file1  mask:8
[root@localhost rsync_client]# cd /app/rsync_client && rsync -artuz -R "./file1" [email protected]::app_rsync_server --password-file=/etc/rsyncd.secret

[root@localhost sersync]#

压测

写入10K个文件批量同步测试结果:

[root@localhost rsync_client]# for n in {1..10000};do dd if=/dev/zero of=/app/rsync_client/"$n".txt bs=1M count=5;done

查看同步速度,当10K个文件同步完后,在/app/rsync_server里发现才同步了600多个文件

多实例情况

配置多个confxml.xml文件(比如:www、bbs、blog….等等),根据不同的需求同步对应的实例文件

[root@localhost rsync_client]# /app/sersync/sersync2 -d -r -n 8 -o /app/sersync/www_confxml.xml
[root@localhost rsync_client]# /app/sersync/sersync2 -d -r -n 8 -o /app/sersync/bbs_confxml.xml
[root@localhost rsync_client]# /app/sersync/sersync2 -d -r -n 8 -o /app/sersync/blog_confxml.xml

rsync本地复制、比对文件、增量同步示例

A fast,versatile,remote (and local) file-copying tool.

rsync基于ssh协议实现高效率远程或本地文件复制,传输速度比scp快。复制文件时会比对本地文件与远程主机的文件,仅复制有差异的文件。

常用选项:

-q,--quiet:suppress non-error messages 静默模式
-v,--verbose:increase verbosity
-a,--archive:archive mode; equals -rlptgoD (no -H,-A,-X) 归档模式,相当于-rlptgoD,不包括(no -H,-A,-X);最常用的参数
-H,--hard-links:preserve hard links 保留硬链接
-A,--acls:preserve ACLs (implies --perms) 保留ACL权限
-X,--xattrs:preserve extended attributes 保留扩展属性
-c, --checksum:skip based on checksum, not mod-time & size
-r,--recursive:recurse into directories 递归
-l,--links:copy symlinks as symlinks 保留软链接,而不跟踪原文件
-p,--perms:preserve permissions 保留权限
-t,--times:preserve modification times 保留mtime
-g,--group:preserve group 保留属组
-o,--owner:preserve owner (super-user only) 保留属主
-D:same as --devices,--specials 保留设备文件和特殊文件
--devices:preserve device files (super-user only)
--specials:preserve special files

-z,--compress:compress file data during the transfer 输过程中压缩文件数据
-n, --dry-run:perform a trial run with no changes made 干跑测试
-u,--update:skip files that are newer on the receiver 增量同步,跳过比本地较新的文件
--delete:delete extraneous files from destination dirs 删除目标目录多余文件
--progress:show progress during transfer 显示传输进度

本地复制

# rsync -av ansible_auto/public/uy-s-192-v01.cfg objects/ansible_auto/public/uy-s-192-v01.cfg

比对文件

# cd /usr/local/nagios/etc/
# rsync -acvn ansible_auto/ objects/ansible_auto/ > diff

增量同步

# rsync -avzu --progress /opt/* [email protected]:/opt/

另外,可以设置计划任务实时同步和备份文件

# crontab -e
*/1 * * * * rsync -avzu /opt/media/* 192.168.201.123:/opt/media

配置rsync Inotify进行文件实时同步

一、简介

rsync用于网络间数据备份 具备高安全性,能实现增量备份,监控的文件必须扫描 文件量大时扫描花费大量时间 所以使用inotify的异步文件系统监控

调用内核监控 检测到文件的修改 同时rsync同步文件

rpmfind.NET搜索 rsync 找到官网地址为 http://rsync.samba.org/ 可以下载源码包进行安装

二、安装过程

1、模拟环境

  • 源服务器192.168.58.142 目标服务器(可以有多台) 192.168.58.143

  • 目标服务器需要监听服务 等待源服务器(客户端)推送文件

2、演示rsync安装过程

目标服务器rsync安装和配置

yum install rsync xinetd

xinetd即extended internet daemon,xinetd是新一代的网络守护进程服务程序,可以用于管理其他的网络服务

  • 关闭selinux(不关闭有可能抛出rsync: mkstemp “/.a.txt.SqAQVm” (in test) failed: Permission denied (13))

  • 临时关闭 setenforce 0 或者编辑 etc/selinux/config 修改 SELINUX=disabled 永久关闭

编辑/etc/xinted.d/rsync

service rsync  
{  
        disable = no  #将禁用改成no  
        flags           = IPv6  
        socket_type     = stream  
        wait            = no  
        user            = root  
        server          = /usr/bin/rsync  
        server_args     = --daemon  
        log_on_failure  += USERID  
}   

通过官方文档 查看rsync的配置文件格式(http://everythinglinux.org/rsync/)

[root@ha etc]# vi rsyncd.conf  
log file = /var/log/rsyncd.log  #日志文件  
pid file = /var/run/rsyncd.pid  #进程文件  
lock file = /var/run/rsync.lock #同步锁文件  

[simple_path_name]   
   path = /data   #表示要同步的目录  
   comment = My Very Own Rsync Server #表示注释  
   uid = root   #同步有权限的用户名  
   gid = root   #同步有权限的用户组  
   port = 873   #对外监听的端口  
   read only = no  #是否文件目录是只读的  
   list = yes   # 是否显示服务端文件列表  
   auth users = mysync  需要同步的文件需要用到的内部账号 可以在下面参数文件中定义  
   secrets file = /etc/rsyncd.scrt 同步的内部通信的账号和密码文件  

创建 用户密码文件 /etc/rsyncd.scrt 格式为:用户名:密码 可以定义多个用户

mysync:123456  

给该两个文件设置600权限

chmod 600 rsyncd.conf

chmod 600 /etc/rsyncd.scrt 

启动rsync

service xinetd start |  stop |restart  

查看运行状态

[root@ha etc]# service xinetd status  
xinetd (pid  7283) is running...  

查看默认 873端口是否开放

[root@ha etc]# netstat -aon | grep 873  
    tcp        0      0 :::873                      :::*                        LISTEN      off (0.00/0/0)  

尝试在任意机器 使用 telnet 192.168.58.142 873 可以查看配置的日志文件

[root@ha run]# more /var/log/rsyncd.log  
    2017/08/03 12:13:30 [7406] connect from ha1 (192.168.58.143)  
    2017/08/03 12:14:11 [7505] connect from ha1 (192.168.58.143)  

源服务器(192.168.58.143)rsync安装和配置

yum install rsync xinetd

xinetd即extended internet daemon,xinetd是新一代的网络守护进程服务程序,可以用于管理其他的网络服务

编辑/etc/xinted.d/rsync

service rsync  
{  
        disable = no  #将禁用改成no  
        flags           = IPv6  
        socket_type     = stream  
        wait            = no  
        user            = root  
        server          = /usr/bin/rsync  
        server_args     = --daemon  
        log_on_failure  += USERID  
}  
  • 关闭selinux(不关闭有可能抛出rsync: mkstemp “/.a.txt.SqAQVm” (in test) failed: Permission denied (13))

  • 临时关闭 setenforce 0 或者编辑 etc/selinux/config 修改 SELINUX=disabled 永久关闭

编辑/etc/xinted.d/rsync

使用命令测试同步文件-v表示增量备份 -a表示传输文件 /data/ 表示当前机器需要同步到目标服务器的文件目录 :test 表示目标服务器 rsyncd.conf定义的[模块名称]

rsync -avH --port=873 --progress  /data/   [email protected]::test  要求你输入目标服务器用户名密码配置文件中的密码 输入123456即可

多次执行只有一次同步 修改文件后再次尝试

3、演示inotify安装过程

  • rpmfind.net搜索 inotify-tool 搜索到官网 https://github.com/rvoicilas/inotify-tools/wiki

  • 下载 源代码 wget https://cloud.github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz

  • inotify只需要在源服务器监听文件的修改状态 然后调用rsync即可 或者使用scp都行

  • 解压后 ./configure –prefix=/usr/local/inotify & make & make install 安装 (安装过rsync 一般gcc都已经安装 如果没有 yum -y install gcc)

  • 安装完成后 /usr/local/inotify/bin下的可执行文件 必须设置到环境变量中

  • vi ~/.bash_profile 添加 或者添加到/etc/rc.local文件中

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

source .bash_profile 执行

查看inotify的三个内核参数是否成功配置

[root@ha1 bin]# ll /proc/sys/fs/inotify    
total 0  
-rw-r--r-- 1 root root 0 Aug  3 14:24 max_queued_events  
-rw-r--r-- 1 root root 0 Aug  3 14:24 max_user_instances  
-rw-r--r-- 1 root root 0 Aug  3 14:24 max_user_watches  

该三个参数的作用为

max_user_instances:用户创建inotify实例最大值  
max_queued_events:inotify产生的事件队列最大长度,如果值太小,会出现错误,导致监控文件不准确  
max_user_watches:监控同步的文件包含的最大目录数,  
可以用:find /data -type d|wc -l 统计,必须保证参数值大于统计结果(/home/rain为同步文件目录)。  

查看查看完整的参数名称

[root@ha1 bin]# sysctl -a | grep max_user_instances  
fs.inotify.max_user_instances = 128  #可以看出参数名 带了前缀 fs.inotify.  

可以通过sysctl -w 参数名=参数值修改 或者编辑 /etc/sysctl.conf 添加参数名=参数值键值对
查看官方教程 https://github.com/rvoicilas/inotify-tools/wiki#info 通过查看/usr/local/inotify/bin目录 发现就两个命令

inotifywait 用于等待一个事件被触发(增删改移动等)  
inotifywatch 监听某个目录后者文件的所有事件 一有事件就触发  

4、inotifywatch 演示

(-e表示监听事件(create创建 access表示读取 delete删除 modify修改) 可以通过inotifywatch –help查看 -t表示监听事件 -r 表示监听目录)

inotifywatch -v -e access -e modify -t 60 -r /data 

60s内尝试读取或者修改文件 过了60s后统计记录就会出现在一个列表中

[root@ha1 bin]# inotifywatch -v -e access -e modify -t 60 -r /data  
Establishing watches...  
Setting up watch(es) on /data  
OK, /data is now being watched.  
Total of 1 watches.  
Finished establishing watches, now collecting statistics.  
Will listen for events for 60 seconds.  
total  access  modify  filename  
4      1       3       /data/  

5、inotifywait演示

该命令 等待一个事件触发 触发后返回状态 此时根据状态调用rsync同步

测试该命令 创建一个脚本文件

vi ~/test.sh
#!/bin/sh  

EVENT=$(inotifywait --format '%e' ~/data)  
[ $? != 0 ] && exit  # 返回值0表示成功 不为0表示出现错误 就退出  
[ "$EVENT" = "MODIFY" ] && echo 'file modified!' 根据变量EVENT判断是哪个事件被触发了   
[ "$EVENT" = "DELETE" ] && echo 'file deleted!'  
chmod +x ~/test.sh
~/test.sh 

发现出现了阻塞 等待一个事件

[root@ha1 bin]# ./test.sh  
Setting up watches.  
Watches established.  

另外一个客户端上执行 删除/data下一个文件

[root@ha1 bin]# ./test.sh  
Setting up watches.  
Watches established.  
file delete  #输出了脚本中 输出的内容  

当然可以通过设置输出输出文件名等信息

  • –format表示时间输出格式 %T表示时间 时间格式–timefmt表示 %d表示天 %m月 %y年 %H 24小时制小时 %M分钟

  • -e表示事件 可以多个 %w表示目录 %f表示文件

[root@ha1 bin]# inotifywait -mr --timefmt '%d/%m/%y %H:%M' --format '%T %w %f' -e create /data  
Setting up watches.  Beware: since -r was given, this may take a while!  
Watches established.  
03/08/17 15:16 /data/ a.txt  
03/08/17 15:17 /data/ b.txt  

也可以通过while循环 使用管道命令获取到对应的文件目录

inotifywait -mr --timefmt '%d/%m/%y %H:%M' --format '%T %w %f' -e create /data |  
while read date time dir file  
do  
 echo $file  
done  

6、inotifywait整合rsync备份文件从源服务

有了上面 inotifywait的基础后 只需要在循环中获取到file 调用同步即可

使用 以下脚本测试 所有的时间 (通过inotifywait –help查看)

inotifywait -mr --timefmt '%d/%m/%y %H:%M' --format '%T %w %f %e' -e create,modify,delete,move /data |  
while read date time dir file event  
do  
 echo "$dir $file $event"  
done  

尝试去修改文件创建目录等操作 控制台输出如下 代码中根据不同的类型进行不同的处理

/data/ cc CREATE,ISDIR  创建目录  
/data/cc/ a.txt CREATE  创建文件  
/data/cc/ a.txt MOVED_FROM 从哪里移动  
/data/cc/ b.txt MOVED_TO   移动到这里  
/data/cc/ b.txt DELETE  删除文件  
/data/ cc DELETE,ISDIR 删除目录  
/data/ bb MOVED_FROM,ISDIR 从目录移动  
/data/ cc MOVED_TO,ISDIR   移动到新目录  

完整代码如下(如果有一个文件被修改就同步该文件就行了 但是实现起来较为复杂 这里直接更新整个目录)还是rsync的环境

srcDir=/data  
echo 123456 > /pass.wd  
chmod 600 /pass.wd  
inotifywait -mr --timefmt '%d/%m/%y %H:%M' --format '%T %w %f %e' -e create,modify,delete,move $srcDir |  
while read date time dir file event  
do  
 echo "$dir $file $event begin syncing"  
 rsync -avH --port=873 --progress --password-file=/pass.wd  $srcDir [email protected]::test   
done  

目标服务器 192.168.58.143 新建/my.sh 填写上边内容

nohup /my.sh & 运行 测试在/data目录修改数据 是否能同步到 58.142对应的/data目录。

Ubuntu系统配置rsync文件同步服务

简介

rsync(remote synchronize)是类unix系统下的实现远程数据同步功能的工具,它的特性如下:

  • 可以镜像保存整个目录树和文件系统
  • 可以很容易做到保持原来文件的权限、事件、软硬链接等信息
  • 无需特殊权限即可安装
  • 快速:第一次同步时 rsync 会复制全部内容,但在下一次只传输修改过的文件。rsync 在传输数据的过程中可以实行压缩及解压缩操作,因此可以使用更少的带宽。
  • 安全:可以使用scp、ssh等方式来传输文件,当然也可以通过直接的socket连接。
  • 支持匿名传输,以方便进行网站镜象。

安装

在Ubuntu下安装rsync通过以步骤可以实现:

sudo apt-get install rsync xinetd

默认情况下Ubuntu安装了rsync,因此只需安装xinetd

服务器端配置

1、 编辑/etc/default/rsync 启动rsync作为使用xinetd的守护进程

# 打开rsync
sudo vim /etc/default/rsync
# 编辑rsync
RSYNC_ENABLE=inetd

2、 创建/etc/xinetd.d/rsync, 通过xinetd使rsync开始工作

# 创建并打开文件
sudo vim /etc/xinetd.d/rsync
# 编辑内容
service rsync
{
    disable         = no
    socket_type     = stream
    wait            = no
    user            = root
    server          = /usr/bin/rsync
    server_args     = --daemon
    log_on_failure  += USERID
}

3、 创建/etc/rsyncd.conf,并填写配置信息

# 创建并打开文件
sudo vim /etc/rsyncd.conf
# 编辑配置信息
max connections = 2
log file = /var/log/rsync.log
timeout = 300

[share] # 模块名
comment = Public Share
# path为需要同步的文件夹路径
path = /home/share
read only = no
list = yes
uid = root
gid = root
# 必须和 rsyncd.secrets中的用户名对应
auth users = user
secrets file = /etc/rsyncd.secrets

4、 创建/etc/rsyncd.secrets,配置用户名和密码.

# 创建并打开文件
sudo vim /etc/rsyncd.secrets
# 配置用户名和密码,密码可以任意设置
user:password

5、 修改rsyncd.secrets文件的权限

sudo chmod 600 /etc/rsyncd.secrets

6、 启动/重启 xinetd

sudo /etc/init.d/xinetd restart

客户端配置

由于我用的系统是windows,所以需要在windows上安装rsync的客户端cwRsync

1、 下载并安装cwRsync

下载地址http://pan.baidu.com/s/1pJ3B1FX

2、 安装后将其添加到环境变量path中,我的cwRsync安装在D:cwRsync目录下,将D:cwRsyncbin添加到环境变量path中

测试

在客户端运行下面的命令检查,确认rsync配置成功

# user是在服务器中rsyncd.secrets文件中配置的用户名
# xx.xx.xx.xx 是服务器的ip地址,也可以填写服务器对应的域名
# share 是rsyncd.conf中定义的模块
rsync [email protected]::share

输入密码后,如果输出以下类似内容,说明配置成功

drwxr-xr-x        4096 2006/12/13 09:41:59 .
drwxr-xr-x        4096 2006/11/23 18:00:03 folders

同步

1、 将本地文件同步至服务器

将当前目录下public路径下的全部内容,同步至服务器,服务器的同步路径在rsyncd.conf中指定

rsync -av ./public/ [email protected]::share

2、 将服务器文件同步至本地

rsync -cvazu --progress [email protected]::share /rsyn

使用inotify rsync实现linux文件批量实时更新

如果只对经常改动的目录进行同步,也可以忽略这个问题,如果每次改动的目录多较大,那么就要用到inotify了,Inotify是一种强大的、细粒度的、异步的文件系统事件监控机制,Linux内核从2.6.13起,加入了对Inotify的支持,通过Inotify可以监控文件系统中的添加、删除、修改、移动等各种事件,但inotify只提供了C语言接口,不方便调用,所以我们需要先安装inotify-tools

系统环境

CentOS_5.7-x86_64
更新源服务器:192.168.9.227
目的服务器:192.168.9.226 192.168.9.228 …

目的服务器配置

192.168.9.226 192.168.9.228(rsync服务端):

检查rsync是否安装

rpm -qa|grep rsync

如果没有发装,执以下命令进行安装

yum -y install rsync

定义rsync配置文件/etc/rsyncd.conf

192.168.9.226:

cat >> /etc/rsyncd.conf << EOF
uid = nobody 
gid = nobody 
use chroot = no 
max connections = 100 
timeout = 600 
pid file = /var/run/rsyncd.pid 
lock file = /var/run/rsyncd.lock 
log file = /var/log/rsyncd.log 
[web1] 
path = /data/www1/ 
ignore errors 
read only = no 
list = no 
hosts allow = 192.168.9.0/255.255.255.0 
auth users = www1 
secrets file = /etc/www1.pwd 
EOF

192.168.9.228:

cat >> /etc/rsyncd.conf << EOF
uid = nobody 
gid = nobody 
use chroot = no 
max connections = 100 
timeout = 600 
pid file = /var/run/rsyncd.pid 
lock file = /var/run/rsyncd.lock 
log file = /var/log/rsyncd.log 
[web2] 
path = /data/www2/ 
ignore errors 
read only = no 
list = no 
hosts allow = 192.168.9.0/255.255.255.0 
auth users = www2 
secrets file = /etc/www2.pwd 
EOF

rsyncd.conf配置文件详解

uid = nobody //运行RSYNC守护进程的用户
gid = nobody //运行RSYNC守护进程的组
use chroot = 0 //不使用chroot
max connections = 0 // 最大连接数,0为不限制
port = 873 //默认端口873
下面这些文件是安装完RSYNC服务后自动生成的文件
pid file = /var/run/rsyncd.pid //pid文件的存放位置
lock file = /var/run/rsync.lock //锁文件的存放位置.指定支持max connections参数的锁文件,默认值是/var/run/rsyncd.lock.
log file = /var/log/rsyncd.log //日志记录文件的存放位置
Timeout = 300 通过该选项可以覆盖客户指定的IP超时时间.通过该选项可以确保rsync服务器不会永远等待一个崩溃的客户端.超时单位为秒钟,0表示没有超时定义,这也是默认值.对于匿名rsync服务器来说,一个理想的数字是600.
Log format = %t %a %m %f %b 通过该选项用户在使用transfer logging可以自己定制日志文件的字段.其格式是一个包含格式定义符的字符串,可以使用的格式定义符如下所示:
%h 远程主机名
%a 远程IP地址
%l 文件长度字符数
%p 该次rsync会话的进程id
%o 操作类型:" send" 或" recv"
%f 文件名
%P 模块路径
%m 模块名
%t 当前时间
%u 认证的用户名(匿名时是null)
%b 实际传输的字节数
%c 当发送文件时,该字段记录该文件的校验码
默认log格式为:" %o %h [%a] %m (%u) %f %l" ,一般来说,在每行的头上会添加" %t [%p] " .在源代码中同时发布有一个叫rsyncstats的perl脚本程序来统计这种格式的日志文件.
#transfer logging = yes
使rsync服务器使用ftp格式的文件来记录下载和上载操作在自己单独的日志中.
syslog facility = local3 指定rsync发送日志消息给syslog时的消息级别,常见的消息级别是:uth, authpriv, cron, daemon, ftp, kern, lpr, mail, news, security, sys-log, user, uucp, local0, local1, local2, local3,local4, local5, local6和local7.默认值是daemon.
模块参数 [web1] //这里是认证的模块名,在client端需要指定
path = /data/www1/ //需要做镜像的目录,不可缺少!
comment = backup web //这个模块的注释信息
ignore errors //可以忽略一些无关的IO错误
read only = yes //该选项设定是否允许客户上载文件.如果为true那么任何上载请求都会失败,如果为false并且服务器目录读写权限允许那么上载是允许的.默认值为true.
list = no //不允许列文件
auth users = bak //认证的用户名,如果没有这行则表明是匿名,此用户与系统无关 该选项指定由空格或逗号分隔的用户名列表,只有这些用户才允许连接该模块.这里的用户和系统用户没有任何关系.如果" auth users" 被设置,那么客户端发出对该模块的连接请求以后会被rsync请求challenged进行验证身份这里使用的challenge/response认证协议.用户的名和密码以明文方式存放在" secrets file" 选项指定的文件中.默认情况下无需密码就可以连接模块(也就是匿名方式).
secrets file = /etc/www1.pwd //密码和用户名对比表,密码文件自己生成 该选项指定一个包含定义用户名:密码对的文件.只有在" auth users" 被定义时,该文件才有作用.文件每行包含一个username:passwd对.一般来说密码最好不要超过8个字符.没有默认的secures file名,需要限式指定一个(例如:/etc/www1.pwd).注意:该文件的权限一定要是600,否则客户端将不能连接服务器.
hosts allow = 192.168.9.0/255.255.255.0 //允许主机或网段
该选项指定哪些IP的客户允许连接该模块.客户模式定义可以是以下形式:
单个IP地址,例如:192.168.9.227
整个网段,例如:192.168.9.0/24,也可以是192.168.9.0/255.255.255.0
多个IP或网段需要用空格隔开,“*”则表示所有,默认是允许所有主机连接.
hosts deny = 0.0.0.0/0 //禁止主机

建立认证文件/etc/www1.pwd

此文件须与配置文件中指定文件名保持一致
此处格式为:username:password,安全问题,并不建议实际使用中使用root用户
192.168.9.226:

echo "www1:741852" >> /etc/www1.pwd

192.168.9.228:

echo "www2:951753" >> /etc/www2.pwd

并且我们需要设置此文件的权限为600

chmod 600 /etc/www1.pwd
chmod 600 /etc/www2.pwd
chmod 600 /etc/rsyncd.conf

建立motd文件(可有可无)

rsyncd.motd记录了rsync服务的欢迎信息,你可以在其中输入任何文本信息,如:

echo "Welcome to use the rsync services!" >> /var/rsyncd.motd

启动rsync

/usr/bin/rsync --daemon
echo "/usr/bin/rsync --daemon" >> /etc/rc.local

更新源服务器配置

192.168.9.227 (rsync客户端)

inotify 可以监视的文件系统事件包括

IN_ACCESS,即文件被访问
IN_MODIFY,文件被 write
IN_ATTRIB,文件属性被修改,如 chmod、chown、touch 等
IN_CLOSE_WRITE,可写文件被 close
IN_CLOSE_NOWRITE,不可写文件被 close
IN_OPEN,文件被 open
IN_MOVED_FROM,文件被移走,如 mv
IN_MOVED_TO,文件被移来,如 mv、cp
IN_CREATE,创建新文件
IN_DELETE,文件被删除,如 rm
IN_DELETE_SELF,自删除,即一个可执行文件在执行时删除自己
IN_MOVE_SELF,自移动,即一个可执行文件在执行时移动自己
IN_UNMOUNT,宿主文件系统被 umount
IN_CLOSE,文件被关闭,等同于(IN_CLOSE_WRITE | IN_CLOSE_NOWRITE)
IN_MOVE,文件被移动,等同于(IN_MOVED_FROM | IN_MOVED_TO)

注:上面所说的文件也包括目录。

安装inotify-tools

在安装inotify-tools前请先确认你的linux内核是否打到了2.6.13,并且在编译时开启了CONFIG_INOTIFY选项,也可以通过以下命令检测

ls /proc/sys/fs/inotify

如果有 max_queued_events,max_user_instances,max_user_watches 三项就说明支持

wget http://cloud.github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
tar xvf inotify-tools-3.14.tar.gz
cd inotify-tools-3.14
./configure
make;make install

编写rsync监控脚本

vi /root/rsync.sh
#!/bin/bash
host1=192.168.9.226
host2=192.168.9.228
src=/data/www/
des1=web1
des2=web2
user1=www1
user2=www2
/usr/local/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' 
-e modify,delete,create,attrib 
${src} 
| while read file
do
rsync -vzrtopg --delete --progress ${src} ${user1}@${host1}::${des1} --password-file=/etc/www1.pwd &&
rsync -vzrtopg --delete --progress ${src} ${user2}@${host2}::${des2} --password-file=/etc/www2.pwd &&
echo "${files} was rsynced" >> /tmp/rsync.log 2>&1
echo "---------------------------------------------------------------------------"
done
-m, 即--monitor,表示始终保持事件监听状态。
-r, 即--recursive,表示递归查询目录。
-q, 即--quiet,表示打印出监控事件。
-e, 即--event,通过此参数可以指定要监控的事件,常见的事件有modify、delete、create、attrib等
--timefmt:指定时间的输出格式
--format:指定变化文件的详细信息

建立认证文件 (rsync客户端认证文件只用加入密码)

echo "741852" >> /etc/www1.pwd
echo "951753" >> /etc/www2.pwd
chmod 600 /etc/www1.pwd
chmod 600 /etc/www2.pwd
/bin/sh -n /root/rsync.sh //语法检查
chmod +x /root/rsync.sh
nohup sh /root/rsync.sh &
echo "nohup sh /root/rsync.sh &" >> /etc/rc.local

同步测试

在更新源服务器上新建一个文件,运行以下的命令,看文件是否可以正常同步,看有无报错信息

rsync -vzrtopg --delete --progress /data/www1/ [email protected]::web1 --password-file=/etc/www1.pwd

将要更新的文件提交到更新源服务器中,这样就通过inotify+rsync批量的将更新文件同步到所有的目的服务器中,相当方便快捷.