rsync模块名称错误 rsync错误码

rsync命令详解

1.rsync简介

rsync是linux系统下的数据镜像备份工具。使用快速增量备份工具Remote Sync可以远程同步,支持本地复制,或者与其他SSH、rsync主机同步。

2. rsync特性

rsync支持很多特性:

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

3. rsync的ssh认证协议

rsync命令来同步系统文件之前要先登录remote主机认证,认证过程中用到的协议有2种:

  • ssh协议
  • rsync协议
  • rsync server端不用启动rsync的daemon进程,只要获取remote host的用户名和密码就可以直接rsync同步文件
  • rsync server端因为不用启动daemon进程,所以也不用配置文件/etc/rsyncd.conf
  • ssh认证协议跟scp的原理是一样的,如果在同步过程中不想输入密码就用ssh-keygen -t rsa打通通道

4. rsync命令

源:
目标:

5. rsync参数的具体解释如下:

一般同步传输目录都使用azv选项. 
 
-v, --verbose 详细模式输出 
 
-q, --quiet 精简输出模式 
 
-c, --checksum 打开校验开关,强制对文件传输进行校验 
 
-a, --archive 归档模式,表示以递归方式传输文件,并保持所有文件属性,等于-rlptgoD 
 
-r, --recursive 对子目录以递归模式处理 
 
-R, --relative 使用相对路径信息 
 
-b, --backup 创建备份,也就是对于目的已经存在有同样的文件名时,将老的文件重新命名为~filename。可以使用--suffix选项来指定不同的备份文件前缀。 
 
--backup-dir 将备份文件(如~filename)存放在在目录下。 
 
-suffix=SUFFIX 定义备份文件前缀 
 
-u, --update 仅仅进行更新,也就是跳过所有已经存在于DST,并且文件时间晚于要备份的文件。(不覆盖更新的文件) 
 
-l, --links 保留软链结 
 
-L, --copy-links 想对待常规文件一样处理软链结 
 
--copy-unsafe-links 仅仅拷贝指向SRC路径目录树以外的链结 
 
--safe-links 忽略指向SRC路径目录树以外的链结 
 
-H, --hard-links 保留硬链结 
 
-p, --perms 保持文件权限 
 
-o, --owner 保持文件属主信息 
 
-g, --group 保持文件属组信息 
 
-D, --devices 保持设备文件信息 
 
-t, --times 保持文件时间信息 
 
-S, --sparse 对稀疏文件进行特殊处理以节省DST的空间 
 
-n, --dry-run现实哪些文件将被传输 
 
-W, --whole-file 拷贝文件,不进行增量检测 
 
-x, --one-file-system 不要跨越文件系统边界 
 
-B, --block-size=SIZE 检验算法使用的块尺寸,默认是字节 
 
-e, --rsh=COMMAND 指定使用rsh、ssh方式进行数据同步 
 
--rsync-path=PATH 指定远程服务器上的rsync命令所在路径信息 
 
-C, --cvs-exclude 使用和CVS一样的方法自动忽略文件,用来排除那些不希望传输的文件 
 
--existing 仅仅更新那些已经存在于DST的文件,而不备份那些新创建的文件 
 
--delete 删除那些DST中SRC没有的文件 
 
--delete-excluded 同样删除接收端那些被该选项指定排除的文件 
 
--delete-after 传输结束以后再删除 
 
--ignore-errors 及时出现IO错误也进行删除 
 
--max-delete=NUM 最多删除NUM个文件 
 
--partial 保留那些因故没有完全传输的文件,以是加快随后的再次传输 
 
--force 强制删除目录,即使不为空 
 
--numeric-ids 不将数字的用户和组ID匹配为用户名和组名 
 
--timeout=TIME IP超时时间,单位为秒 
 
-I, --ignore-times 不跳过那些有同样的时间和长度的文件 
 
--size-only 当决定是否要备份文件时,仅仅察看文件大小而不考虑文件时间 
 
--modify-window=NUM 决定文件是否时间相同时使用的时间戳窗口,默认为0 
 
-T --temp-dir=DIR 在DIR中创建临时文件 
 
--compare-dest=DIR 同样比较DIR中的文件来决定是否需要备份 
 
-P 等同于 --partial 
 
--progress 显示备份过程 
 
-z, --compress 对备份的文件在传输时进行压缩处理 
 
--exclude=PATTERN 指定排除不需要传输的文件模式 
 
--include=PATTERN 指定不排除而需要传输的文件模式 
 
--exclude-from=FILE 排除FILE中指定模式的文件 
 
--include-from=FILE 不排除FILE指定模式匹配的文件 
 
--version 打印版本信息 
 
--address 绑定到特定的地址 
 
--config=FILE 指定其他的配置文件,不使用默认的rsyncd.conf文件 
 
--port=PORT 指定其他的rsync服务端口 
 
--blocking-io 对远程shell使用阻塞IO 
 
-stats 给出某些文件的传输状态 
 
--progress 在传输时现实传输过程 
 
--log-format=formAT 指定日志文件格式 
 
--password-file=FILE 从FILE中得到密码 
 
--bwlimit=KBPS 限制I/O带宽,KBytes per second 
 
-h, --help 显示帮助信息 
 
//Rsync的命令格式常用的有以下三种:
    rsync [OPTION]... SRC DEST
    rsync [OPTION]... SRC [USER@]HOST:DEST
    rsync [OPTION]... [USER@]HOST:SRC DEST
  
 
//下载安装
[root@ ~]# yum -y install rsync
[root@ ~]# yum -y install rsync
//同步
[root@ ~]# rsync -avz anaconda-ks.cfg root@:/tmp/
The authenticity of host ' ()' can't be established.
ECDSA key fingerprint is SHA256:lJPrijoSJH4gTHmc79E4mv86JbN64aX7Ywpzgk4s8JU.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '' (ECDSA) to the list of known hosts.
root@'s password: 
sending incremental file list
anaconda-ks.cfg
 
sent  bytes  received  bytes   bytes/sec
total size is 1,  speedup is 
//将对面的文件同步到当前主机
[root@ ~]# ls
  initial-setup-ks.cfg
[root@ ~]# 
[root@ ~]# rsync -avz root@:/tmp/anaconda-ks.cfg .
root@'s password: 
receiving incremental file list
anaconda-ks.cfg
sent  bytes  received  bytes   bytes/sec
total size is 1,  speedup is 
[root@ ~]# ls
anaconda-ks.cfg  initial-setup-ks.cfg
//同步目录
[root@ ~]# ls /tmp/
anaconda.log
[root@ ~]# ls /opt/
[root@ ~]# rsync -avz /tmp root@:/opt/
root@'s password: 
sending incremental file list
[root@ ~]# ls /opt/
tmp
 
 
//sync常用选项
    -a, --archive       //归档
    -v, --verbose       //啰嗦模式
    -q, --quiet         //静默模式
    -r, --recursive     //递归
    -p, --perms         //保持原有的权限属性
    -z, --compress      //在传输时压缩,节省带宽,加快传输速度
    --delete            //在源服务器上做的删除操作也会在目标服务器上同步

6.使用示例

[举例] 
 
*拷贝上面的一个文件到本地机器: 
 
$ rsync -v quietheart@:/home/quietheart/tmpTrans/linux.tar.gz ./ 
 
输入之后输出如下: 
 
quietheart@'s password: 
linux.tar.gz 
sent  bytes  received  bytes   bytes/sec 
total size is   speedup is  
这里,根据提示,输入密码之后就开始传输了。 
*“假装”拷贝上面的一个文件到本地机器: 
$ rsync -n quietheart@:/home/quietheart/tmpTrans/linux.tar.gz ./ 
输入之后输出如下: 
quietheart@'s password: 
 
这里,根据提示,输入密码之后就结束了,并不实际拷贝内容,而是假执行一下。 
 
 
 
*“假装”拷贝上面的一个文件到本地机器,显示详细信息: 
 
$ rsync -nv quietheart@:/home/quietheart/tmpTrans/linux.tar.gz ./ 
 
quietheart@'s password: 
linux.tar.gz 
sent  bytes  received  bytes   bytes/sec 
total size is   speedup is  
这里,将会看到,实际并没把文件linux.tar.gz拷贝到本地机器上面。 
*拷贝文件并且显示进度: 
$ rsync --progress quietheart@:/home/quietheart/tmpTrans/linux.tar.gz ./ 
如果当前目录刚刚拷贝了linux.tar.gz,那么输入之后,输出如下: 
quietheart@'s password: 
 
linux.tar.gz 
 
   %   .42MB/s    0:: (xfer#1, to-check=0/1) 
 
 
 
sent  bytes  received  bytes   bytes/sec 
 
total size is   speedup is  
 
 
 
如果当前目录没linux.tar.gz,那么输入之后输出如下: 
 
quietheart@'s password: 
linux.tar.gz 
   %    8.67MB/s    0:: (xfer#1, to-check=0/1) 
sent  bytes  received  bytes   bytes/sec 
total size is   speedup is  
这里,文件,我们可以看到,如果当前文件是存在的,那么拷贝的速度会大大加快,因为rsync只拷贝文件中改动的部分。这个命令在拷贝文件的时候想要了解拷贝的进度的时候挺有用。使用scp拷贝也会显示进度。 
*以递归方式传输目录中的文件(不含最外层目录),显示进度和传输详细信息,并保留所有文件所有属性: 
$ rsync --progress -av openoffice/ quietheart@:/home/quietheart/tmpTrans/ 
输入之后,输出如下: 
quietheart@'s password: 
 
building file list ... 
 
3 files to consider 
 
./ 
 
OOo_3.3.0_Linux_x86-64_install-rpm-wJRE_en-US.tar.gz 
 
   %    9.41MB/s    0:: (xfer#1, to-check=1/3) 
 
good.conf 
 
           %    0.01kB/s    0:: (xfer#2, to-check=0/3) 
 
 
 
sent  bytes  received  bytes   bytes/sec 
 
total size is   speedup is  
 
这里,将文件传输到机器上,使用-a选项,等价于使用-rlptgoD选项,-v选项显示详细信息,--progress显示进度,注意传输完毕之后,会发现目标机器上面的/home/quietheart/tmpTrans中将会出现openoffice中的内容,但是不会有openoffice目录。实践发现,如果目标目录的某个文件被改变了,那么这样复制就会用源文件覆盖目标同名的文件,也就是说改变就丢失了。 
 
 
 
*递归传输目录中的文件(不含最外目录),对备份文件传输时压缩处理,显示进度和传输详细信息,并保留所有文件所有属性: 
 
$ rsync --progress -azv openoffice/ quietheart@:/home/quietheart/tmpTrans/ 
 
输入之后,输出如下: 
 
quietheart@'s password: 
building file list ... 
3 files to consider 
./ 
OOo_3.3.0_Linux_x86-64_install-rpm-wJRE_en-US.tar.gz 
   %    9.67MB/s    0:: (xfer#1, to-check=1/3) 
good.conf 
           %    0.04kB/s    0:: (xfer#2, to-check=0/3) 
sent  bytes  received  bytes   bytes/sec 
total size is   speedup is  
这里,使用-z选项在传输之前进行了压缩处理,但是由于这里文件都压缩过了所以也没有看到有什么不同。 
*以递归方式传输目录中的文件(含最外层目录),显示进度和传输详细信息,并保留所有文件所有属性: 
$ rsync --progress -av openoffice quietheart@:/home/quietheart/tmpTrans/ 
输入之后,输出如下: 
quietheart@'s password: 
 
building file list ... 
 
3 files to consider 
 
./ 
 
OOo_3.3.0_Linux_x86-64_install-rpm-wJRE_en-US.tar.gz 
 
   %    9.41MB/s    0:: (xfer#1, to-check=1/3) 
 
good.conf 
 
           %    0.01kB/s    0:: (xfer#2, to-check=0/3) 
 
 
 
sent  bytes  received  bytes   bytes/sec 
 
total size is   speedup is  
 
这里,注意如果保留最外层目录,那么就去掉在源目录名后面的'/'目录分割符号,将目录传输到机器上,使用-a选项,等价于使用-rlptgoD选项,-v选项显示详细信息,--progress显示进度,注意传输完毕之后,会发现目标机器上面的/home/quietheart/tmpTrans中将会出现openoffice目录,里面有相应的内容。 
 
 
 
**传输目录中包含非压缩文件,不用-z选项和使用-z选项的差别: 
 
一般传输大量数据的时候都使用-avz,使用-z选项在传输之前会将文件压缩并传输,这样减少传输的数据量,参见后面的对比。 
 
*不用-z选项: 
 
$rsync --progress -av quietheart@:/home/quietheart/tmpTrans/openoffice ./ 
 
quietheart@'s password: 
receiving file list ... 
3 files to consider 
openoffice/ 
openoffice/OOo_3.3.0_Linux_x86-64_install-rpm-wJRE_en-US.tar 
   %   .32MB/s    0:: (xfer#1, to-check=1/3) 
openoffice/good.conf 
           %    0.05kB/s    0:: (xfer#2, to-check=0/3) 
sent  bytes  received  bytes   bytes/sec 
total size is   speedup is  
这里,没使用-z压缩选项,传输的数据为:,最后大小:。 
*使用-z选项 
$ rsync --progress -azv quietheart@:/home/quietheart/tmpTrans/openoffice ./ 
quietheart@'s password: 
 
receiving file list ... 
 
3 files to consider 
 
openoffice/ 
 
openoffice/OOo_3.3.0_Linux_x86-64_install-rpm-wJRE_en-US.tar 
 
   %   .49MB/s    0:: (xfer#1, to-check=1/3) 
 
openoffice/good.conf 
 
           %    0.01kB/s    0:: (xfer#2, to-check=0/3) 
 
 
 
sent  bytes  received  bytes   bytes/sec 
 
total size is   speedup is  
 
这里,使用压缩的-z选项,传输的数据为:,最后大小为:。 
 
** 
 
 
 
*同步文件的时候,在目标中删除源中没有的文件: 
 
$ rsync --delete --progress -azv quietheart@:/home/quietheart/tmpTrans/openoffice ./ 
 
quietheart@'s password: 
receiving file list ... 
3 files to consider 
deleting openoffice/aa 
openoffice/         
sent  bytes  received  bytes   bytes/sec 
total size is   speedup is  
这里,本地中的office中有个aa文件在源目录中没有,使用--delete选项,所以就把它删除了(如果目标是远程机器,也会删除远程机器上面多余的文件)。如果不确定,可以实现使用-n假运行查看一下,输出都一样,不过不做实际的删除和传输。 
**使用rsync服务 
*不输入密码查看rsync服务器指定目录下的文件: 
$export RSYNC_PASSWORD= 
$ rsync --list-only quietheart@::QuietHeart_tmpTrans 
+++++++++++++++++++++++++++ 
+ QuietHeart  rsync   
+++++++++++++++++++++++++++ 
drwxrwxr-x        //:: . 
-rw-r--r--        //:: CentOS-Base.repo 
-rw-rw-r--   //:: RO50ForLinBeta.tar.gz 
-rwxr-xr-x       //:: diablo 
-rw-rw-r--   //:: eclipse-linuxtools-helios-SR2-incubation-linux-gtk.tar.gz 
-rw-rw-r--      //:: gtest-.tar.bz2 
-rw-r--r--           //:: log_error 
-rw-rw-r--       //:: log_error_20110415_19_37 
-rw-r--r--    //:: site-.zip 
drwx------        //:: zh-CN 
这里,rsync服务器通过一个配置文件配置用户名,密码,以及QuietHeart_tmpTrans(服务器展现给外面用于访问共享目录的虚拟目录名称),具体在后面配置rsync时候会说。事先设置好RSYNC_PASSWORD变量可以不用输入密码直接传输。 
*输入密码查看rsync服务器指定目录下的文件: 
$ unset RSYNC_PASSWORD 
$ rsync --list-only quietheart@::QuietHeart_tmpTrans 
+++++++++++++++++++++++++++ 
+ QuietHeart  rsync   
+++++++++++++++++++++++++++ 
Password: 
drwxrwxr-x        //:: . 
-rw-r--r--        //:: CentOS-Base.repo 
-rw-rw-r--   //:: RO50ForLinBeta.tar.gz 
-rwxr-xr-x       //:: diablo 
-rw-rw-r--   //:: eclipse-linuxtools-helios-SR2-incubation-linux-gtk.tar.gz 
-rw-rw-r--      //:: gtest-.tar.bz2 
-rw-r--r--           //:: log_error 
-rw-rw-r--       //:: log_error_20110415_19_37 
-rw-r--r--    //:: site-.zip 
drwx------        //:: zh-CN 
$ rsync --list-only quietheart@::QuietHeart_tmpTrans/zh-CN 
+++++++++++++++++++++++++++ 
+ QuietHeart  rsync   
+++++++++++++++++++++++++++ 
Password: 
drwx------        //:: zh-CN 
$ rsync --list-only quietheart@::QuietHeart_tmpTrans/zh-CN/ 
+++++++++++++++++++++++++++ 
+ QuietHeart  rsync   
+++++++++++++++++++++++++++ 
Password: 
drwx------        //:: . 
-rwxrwxr-x        //:: update 
drwx------        //:: DEBS 
drwx------        //:: RO 
drwx------        //:: licenses 
drwx------        //:: readmes 
这里,连续执行了次查看。如果不设置那个RSYNC_PASSWORD变量(这里使用unset取消这个变量之前的设置),那么就需要输入密码了。可以查看子目录的内容(例如这里的zh-CN),如果目录后面有'/'那么可以看到目录里面,如果没有'/'那么只显示那个目录名。 
*将文件通过服务形式拷贝到本地: 
$ rsync --progress -avz quietheart@::QuietHeart_tmpTrans ./ 
+++++++++++++++++++++++++++   
+ QuietHeart  rsync   
+++++++++++++++++++++++++++ 
Password: 
receiving file list ... 
 files to consider 
./ 
CentOS-Base.repo 
        %    2.14MB/s    0:: (xfer#1, to-check=/) 
RO50ForLinBeta.tar.gz 
   %    4.21MB/s    0:: (xfer#2, to-check=/) 
...剩下的省略... 
这样,将::QuietHeart_tmpTrans中的内容拷贝到当前目录,这里注意拷贝之后没有最外层的QuietHeart_tmpTrans目录。 
*将文件通过服务形式拷贝到服务器(失败的情况): 
$ rsync --progress -avz ./rsynctest quietheart@::QuietHeart_tmpTrans 
+++++++++++++++++++++++++++ 
+ QuietHeart  rsync   
+++++++++++++++++++++++++++ 
Password: 
building file list ... 
2 files to consider 
ERROR: module is read only 
rsync error: syntax or usage error (code 1) at main.c) [receiver=] 
rsync: connection unexpectedly closed (4 bytes received so far) [sender] 
rsync error: error in rsync protocol data stream (code ) at io.c) [sender=] 
这里,我配置了服务目录只读(参见后面配置文件中的read only选项),所以拷贝失败。 
*将文件通过服务形式拷贝到服务器(成功的情况): 
$ rsync --progress -avz ./rsynctest quietheart@::QuietHeart_tmpTrans 
+++++++++++++++++++++++++++ 
+ QuietHeart  rsync   
+++++++++++++++++++++++++++ 
Password: 
building file list ... 
2 files to consider 
rsynctest/ 
rsynctest/aaa 
           %    0.00kB/s    0:: (xfer#1, to-check=0/2) 
sent  bytes  received  bytes   bytes/sec 
total size is 0  speedup is  
这里,我添加了可写权限(参见后面配置文件中的read only选项),所以拷贝成功。 
** 
[其它] 
1,配置rsync服务: 
这里,介绍rsync服务的配置过程: 
1)建立配置文件如下: 
[root@lv-k test]#mkdir /etc/rsyncd 
[root@lv-k test]#>> /etc/rsyncd/rsyncd.conf 
[root@lv-k test]#>> /etc/rsyncd/rsyncd.secrets 
[root@lv-k test]#chmod  /etc/rsyncd/rsyncd.secrets 
[root@lv-k test]#>> /etc/rsyncd/rsyncd.motd 
这里,rsyncd.conf是rsync服务的配置文件,rsyncd.secrets存放使用rsync服务的用户名和密码(有说用户名应该是主机上存在的用户,但是实践发现可以不是主机上面已经存在的,密码随意),rsyncd.motd存放登陆时候的欢迎信息。 
2)配置文件内容如下: 
rsyncd.conf文件: 
###################### 
# Minimal configuration file for rsync daemon 
# See rsync(1) and rsyncd.conf(5) man pages for help 
pid file = /var/run/rsyncd.pid  
port =  
address =  
#uid = nobody 
#gid = nobody   
uid = root  
gid = root  
use chroot = yes 
read only = yes 
#limit access to private LANs 
#hosts allow=/ / 
hosts allow=* 
#hosts deny=* 
max connections = 5 
motd file = /etc/rsyncd/rsyncd.motd 
#This will give you a separate log file 
#log file = /var/log/rsync.log 
#This will log every file transferred - up to ,+ per user, per sync 
#transfer logging = yes 
log format = %t %a %m %f %b 
syslog facility = local3 
timeout =  
[QuietHeart_tmpTrans]  
path = /home/quietheart/tmpTrans  
list=yes 
ignore errors 
auth users = quietheart lv-k test1 
secrets file = /etc/rsyncd/rsyncd.secrets 
comment = quietheart tmpfile transfer dir 
#exclude =   home/  samba/   
###################### 
这里,文件的内容不细说了,自己应该能够悟出来,或者可以参见"man rsyncd.conf"一个关键的地方就是:"hosts allow"可以设置访问的IP;以及后面设置可以访问的目录的组模块内容,组名就是给客户端看的名字例如这里的"QuietHeart_tmpTrans"。另外,如果设置read only = no那么别人就可以通过服务向这个地方写了。 
rsyncd.secrets文件: 
###################### 
quietheart: 
lv-k: 
test1:test1 
###################### 
这个文件存放密码信息,管理可以使用rsync的用户以及相应的密码。 
rsyncd.motd文件: 
###################### 
+++++++++++++++++++++++++++ 
+ QuietHeart  rsync   
+++++++++++++++++++++++++++ 
###################### 
这里只存放一些登陆的时候打印的信息,motd意思是:"Message Of The Day"。 
3)测试配置: 
[root@lv-k test]#rsync --daemon  --config=/etc/rsyncd/rsyncd.conf 
[root@lv-k test]#rsync --list-only quietheart@::QuietHeart_tmpTrans 
+++++++++++++++++++++++++++ 
+ QuietHeart  rsync   
+++++++++++++++++++++++++++ 
Password: 
drwxrwxr-x        //:: . 
-rw-r--r--        //:: CentOS-Base.repo 
-rw-rw-r--   //:: RO50ForLinBeta.tar.gz 
-rwxr-xr-x       //:: diablo 
-rw-rw-r--   //:: eclipse-linuxtools-helios-SR2-incubation-linux-gtk.tar.gz 
-rw-rw-r--      //:: gtest-.tar.bz2 
-rw-r--r--           //:: log_error 
-rw-rw-r--       //:: log_error_20110415_19_37 
-rw-r--r--    //:: site-.zip 
drwx------        //:: zh-CN 
这里,先指定用配置文件rsyncd.conf启动rsync 服务器,然后使用"--list-only"选项运行客户端(可以在其它机器上运行),当出现Password密码的时候,输入"即可". 

7. rsync+inotify

rsync与传统的cp、tar备份方式相比,rsync具有安全性高、备份迅速、支持增量备份等优点,通过rsync可以解决对实时性要求不高的数据备份需求,例如定期的备份文件服务器数据到远端服务器,对本地磁盘定期做数据镜像等。
随着应用系统规模的不断扩大,对数据的安全性和可靠性也提出的更好的要求,rsync在高端业务系统中也逐渐暴露出了很多不足,首先,rsync同步数据时,需要扫描所有文件后进行比对,进行差量传输。如果文件数量达到了百万甚至千万量级,扫描所有文件将是非常耗时的。而且正在发生变化的往往是其中很少的一部分,这是非常低效的方式。其次,rsync不能实时的去监测、同步数据,虽然它可以通过linux守护进程的方式进行触发同步,但是两次触发动作一定会有时间差,这样就导致了服务端和客户端数据可能出现不一致,无法在应用故障时完全的恢复数据。基于以上原因,rsync+inotify组合出现了!

Inotify是一种强大的、细粒度的、异步的文件系统事件监控机制,linux内核从起,加入了Inotify支持,通过Inotify可以监控文件系统中添加、删除,修改、移动等各种细微事件,利用这个内核接口,第三方软件就可以监控文件系统下文件的各种变化情况,而inotify-tools就是这样的一个第三方软件。
在前面有讲到,rsync可以实现触发式的文件同步,但是通过crontab守护进程方式进行触发,同步的数据和实际数据会有差异,而inotify可以监控文件系统的各种变化,当文件有任何变动时,就触发rsync同步,这样刚好解决了同步数据的实时性问题。

部署sync+inotify同步/runtime目录至目标服务器的/ysd/下:

//关闭防火墙和selinux
[root@ ~]# systemctl disable --now firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@ ~]# setenforce 0
[root@ ~]# vi /etc/selinux/config 
[root@ ~]# systemctl disable --now firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@ ~]# setenforce 0
[root@ ~]# vi /etc/selinux/config 
//安装
[root@ ~]# yum -y install rsync
[root@ ~]# yum -y install rsync
//在目标上生成配置文件
[root@ ~]# vi /etc/rsyncd.conf
 
log file = /var/log/rsyncd.log    # 日志文件位置,启动rsync后自动产生这个文件,无需提前创建
pidfile = /var/run/rsyncd.pid     # pid文件的存放位置
lock file = /var/run/rsync.lock   # 支持max connections参数的锁文件
secrets file = /etc/rsync.pass    # 用户认证配置文件,里面保存用户名称和密码,必须手动创建这个文件
 
[etc_from_client]     # 自定义同步名称
path = /ysd/          # rsync服务端数据存放路径,客户端的数据将同步至此目录
comment = sync etc from client
uid = root        # 设置rsync运行权限为root
gid = root        # 设置rsync运行权限为root
port =         # 默认端口
ignore errors     # 表示出现错误忽略错误
use chroot = no       # 默认为true,修改为no,增加对目录文件软连接的备份
read only = no    # 设置rsync服务端为读写权限
list = no     # 不显示rsync服务端资源列表
max connections =      # 最大连接数
timeout =      # 设置超时时间
auth users = admin        # 执行数据同步的用户名,可以设置多个,用英文状态下逗号隔开
hosts allow =    # 允许进行数据同步的客户端IP地址,可以设置多个,用英文状态下逗号隔开
hosts deny =       # 禁止数据同步的客户端IP地址,可以设置多个,用英文状态下逗号隔开
EOF
//创建用户认证文件
[root@ ~]# echo 'admin:' > /etc/rsync.pass
[root@ ~]# cat /etc/rsync.pass
admin:
[root@ ~]# chmod  /etc/rsync*
[root@ ~]# yum -y install rsync-daemon
[root@ ~]# systemctl enable --now rsyncd
Created symlink /etc/systemd/system/multi-user.target.wants/rsyncd.service → /usr/lib/systemd/system/rsyncd.service.
[root@ ~]# ss -antl
State        Recv-Q       Send-Q             Local Address:Port             Peer Address:Port      
LISTEN       0            5                        :                   :*         
LISTEN       0                                  :                    :*         
LISTEN       0            5                           [::]:                      [::]:*         
LISTEN       0                                     [::]:   
 
        
//在源上
[root@ ~]# echo '' > /etc/rsync.pass
[root@ ~]# chmod  /etc/rsync.pass 
[root@ ~]# cat /etc/redhat-release //查看系统版本,确保系统版本和epel版本匹配
Red Hat Enterprise Linux release  (Ootpa)
若得到的结果为Red Hat Enterprise Linux release 8.***,则需要选择的epel版本为8。
进入epel官网https://dl.fedoraproject.org/pub/epel,下载对应版本epel安装包
[root@ ~]# wget https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm
-- ::--  https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm
正在解析主机 dl.fedoraproject.org (dl.fedoraproject.org)... , , 
正在连接 dl.fedoraproject.org (dl.fedoraproject.org)||:... 已连接。
已发出 HTTP 请求,正在等待回应...  OK
长度: (23K) [application/x-rpm]
正在保存至: “epel-release-latest-8.noarch.rpm”
 
epel-release-latest-8.no %[=================================>]  .09K  4.38KB/s  用时 5.3s    
 
 :: KB/s) - 已保存 “epel-release-latest-8.noarch.rpm” [/])
 
[root@ ~]# rpm -vih epel-release-latest-8.noarch.rpm
警告:epel-release-latest-8.noarch.rpm: 头V4 RSA/SHA256 Signature, 密钥 ID 2f86d6a1: NOKEY
Verifying...                          ################################# [%]
准备中...                          ################################# [%]
正在升级/安装...
   1:epel-release-.el8            ################################# [%]
[root@ ~]# ls /etc/yum.repos.d/
epel-modular.repo     epel.repo                  epel-testing.repo  ym.repo
epel-playground.repo  epel-testing-modular.repo  redhat.repo
[root@ ~]# yum -y install inotify-tools
[root@ ~]# mkdir /scripts
[root@ ~]# cd /scripts/
[root@ scripts]# ls
[root@ scripts]# vim inotify.sh
[root@ scripts]# cat inotify.sh 
host=      # 目标服务器的ip(备份服务器)
src=/runtime        # 在源服务器上所要监控的备份目录(此处可以自定义,但是要保证存在)
des=etc_from_client     # 自定义的模块名,需要与目标服务器上定义的同步名称一致
password=/etc/rsync.pass        # 执行数据同步的密码文件
user=admin          # 执行数据同步的用户名
inotifywait=/usr/bin/inotifywait
 
$inotifywait -mrq --timefmt '%Y%m%d %H:%M' --format '%T %w%f%e' -e modify,delete,create,attrib $src \
| while read files;do
    rsync -avzP --delete  --timeout= --password-file=${password} $src $user@$host::$des
    echo "${files} was rsynced" >>/tmp/rsync.log 2>&1
done
[root@ scripts]# chmod +x inotify.sh 
[root@ scripts]# ls
inotify.sh
[root@ ~]# /scripts/inotify.sh  //执行
[root@ ~]# cd /runtime/  //开另一个终端
[root@ runtime]# ls
[root@ runtime]# touch awm //随便创建一个文件
[root@ ~]# /scripts/inotify.sh 
sending incremental file list     //开始同步
runtime/
runtime/awm
              %    0.00kB/s    0:: (xfr#1, to-chk=0/2)
 
sent  bytes  received  bytes   bytes/sec
total size is 0  speedup is 
[root@ ~]# cd /ysd/
[root@ ysd]# ls
runtime
[root@ ym]# ll runtime/
总用量 0
-rw-r--r--. 1 root root 月 : awm
//让其在后台运行,并开机自启
[root@ ~]# chmod +x /etc/rc.d/rc.local 
[root@ ~]# ll /etc/rc.
rc.d/     rc.local  
[root@ ~]# ll /etc/rc.d/rc.local 
-rwxr-xr-x. 1 root root 月   /etc/rc.d/rc.local
[root@ ~]# vim /etc/rc.d/rc.local 
# that this script will be executed during boot.
 
nohup /scripts/inotify.sh &
touch /var/lock/subsys/local
[root@localhost ~]# ps -ef|grep inotify
root               1  : ?        :: /bin/sh /scripts/inotify.sh
root              : ?        :: /usr/bin/inotifywait -mrq --timefmt %Y%m%d %H:%M --format %T %w%f%e -e modify,delete,create,attrib /runtime
root              : ?        :: /bin/sh /scripts/inotify.sh
root              : pts/0    :: grep --color=auto inotify
 
原文链接:,转发请注明来源!