Rsync、Inotify-tools简介

简介:

Rsync简介

rsync,remote synchronize顾名思意就知道它是一款实现远程同步功能的软件,它在同步文件的同时,可以保持原来文件的权限、时间、软硬链接等附加信息。rsync是用 “rsync 算法”提供了一个客户机和远程文件服务器的文件同步的快速方法,而且可以通过ssh方式来传输文件,这样其保密性也非常好,另外它还是免费的软件。

地址:http://rsync.samba.org/ftp/rsync/src/


安装

CentOS系统默认已经安装rsync

1
2
3
4
5
6
7
8
9
[root@localhost ~] # rpm -qa|grep -i rsync
rsync -3.0.6-12.el6.x86_64
[root@localhost ~] # rpm -e --nodeps $(rpm -qa|grep -i rsync)
[root@localhost ~] # rpm -qa|grep -i rsync
[root@localhost ~] # cd /usr/local/src/
[root@localhost src] # tar -zxvf rsync-3.1.2.tar.gz         #yum install -y rsync
[root@localhost src] # cd rsync-3.1.2
[root@localhost  rsync -3.1.2] # ./configure --prefix=/app/rsync
[root@localhost  rsync -3.1.2] # make && make install


配置

同步目的端需要配置,数据源端无需配置。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
[root@localhost  rsync -3.1.2] # vim /etc/rsyncd.conf
[global]                         #全局配置
uid = nobody
gid = nobody
use chroot = no
timeout =300
port = 873
max connections = 30
pid  file  /var/runn/rsyncd .pid
lock  file  /var/runn/rsync .lock
log  file  /var/log/rsyncd .log
log  format  = %t %a %u %m %f %b     #日志的格式 %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] "
[app_rsync_server]                 # 要同步的模块名
path =  /app/rsync_server
comment = server                   #这个名名称无所谓  
read  only = no                     # no客户端可上传文件,yes只读
write only = no                     # no客户端可下载文件,yes不能下载
list = no                         #是否提供资源列表
ignore errors                     #忽略一些无关的IO错误
hosts allowd = 10.15.43.0 /24          #多个IP或网段需要用空格隔开,“*”则表示所有,默认是允许所有主机连接
hosts deny = *
auth  users  rsync                 #登陆系统使用的用户名,没有默认为匿名
secrets  file  /etc/rsyncd .secret     #密码文件存放的位置
[root@localhost  rsync -3.1.2] # vim /etc/rsyncd.secret
rsync :123456
[root@localhost  rsync -3.1.2] # chmod 600 /etc/rsyncd.secret

ignore errors

        这个选项最好加上,否则再很多crontab的时候往往发生错误你也未可知,因为你不可能天天去看每时每刻去看log,不加上这个出现错误的几率相对会很高,因为任何大点的项目和系统,磁盘IO都是一个瓶颈。


启动与关闭

daemon方式启动

1
2
3
4
5
6
7
[root@localhost  rsync -3.1.2] # /app/rsync/bin/rsync --daemon --config=/etc/rsyncd.conf
[root@localhost  rsync -3.1.2] # netstat -anotp|grep :873
tcp        0      0 0.0.0.0:873                 0.0.0.0:*                   LISTEN      9622 /rsync           off (0.00 /0/0 )
tcp        0      0 :::873                      :::*                        LISTEN      9622 /rsync           off (0.00 /0/0 )
[root@localhost  rsync -3.1.2] # pkill rsync
[root@localhost  rsync -3.1.2] # netstat -anotp|grep :873
[root@localhost  rsync -3.1.2] #

利用xinetd控制rsync启动

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[root@localhost  rsync -3.1.2] # vim /etc/xinetd.d/rsync 
 
# default: off
# description: The rsync server is a good addition to an ftp server, as it \
#       allows crc checksumming etc.
service  rsync
{
         disable = no                 #将yes改为no
         flags           = IPv6
         socket_type     = stream
         wait            = no
         user            = root
         server          =  /app/rsync/bin/rsync   #修改为rsync程序路径
         server_args     = --daemon
         log_on_failure  += USERID
}
[root@localhost  rsync -3.1.2] # /etc/init.d/xinetd restart


客户端配置和测试

客户端(数据源端)无需配置rsync配置文件/etc/rsyncd.conf,只需要安装并启动rsync和建一个和服务端一样的密码的密码文件

客户端

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
[root@localhost ~] # yum -y install rsync xinetd
[root@localhost ~] # vim /etc/xinetd.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
}
[root@localhost ~] # echo 123456 > /etc/rsyncd.secret
[root@localhost ~] # chmod 600 /etc/rsyncd.secret
[root@localhost rsync_client] # touch file{1..8}
[root@localhost rsync_client] # rsync -avH --port=873 --progress --delete  /app/rsync_client/ rsync@10.15.43.100::app_rsync_server --password-file=/etc/rsyncd.secret
sending incremental  file  list
./
file1
            0 100%    0.00kB /s     0:00:00 (xfer #1, to-check=7/9)
file2
            0 100%    0.00kB /s     0:00:00 (xfer #2, to-check=6/9)
file3
            0 100%    0.00kB /s     0:00:00 (xfer #3, to-check=5/9)
file4
            0 100%    0.00kB /s     0:00:00 (xfer #4, to-check=4/9)
file5
            0 100%    0.00kB /s     0:00:00 (xfer #5, to-check=3/9)
file6
            0 100%    0.00kB /s     0:00:00 (xfer #6, to-check=2/9)
file7
            0 100%    0.00kB /s     0:00:00 (xfer #7, to-check=1/9)
file8
            0 100%    0.00kB /s     0:00:00 (xfer #8, to-check=0/9)
 
sent 404 bytes  received 163 bytes  1134.00 bytes /sec
total size is 0  speedup is 0.00
[root@localhost rsync_client] #

Rsync参数:

-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 检验算法使用的块尺寸,默认是700字节

-e, --rsh=COMMAND 指定使用rsh、ssh方式进行数据同步

--rsync-path=PATH 指定远程服务器上的rsync命令所在路径信息

-C, --cvs-exclude 使用和CVS一样的方法自动忽略文件,用来排除那些不希望传输的文件

--existing 仅仅更新那些已经存在于DST的文件,而不备份那些新创建的文件

--delete 删除那些DST中SRC没有的文件

--delete-excluded 同样删除接收端那些被该选项指定排除的文件

 --exclude-from=文件名 排除文件中指定模式的文件,不传送,一行一个目录,使用相对路径

--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 显示帮助信息


错误

@ERROR: Unknown module 'app_rsync_server'

rsync error: error starting client-server protocol (code 5) at main.c(1503) [sender=3.0.6]

检查/etc/rsyncd.conf文件中hosts allowd项,删除了hosts deny项重启后恢复


inotify简介

            Inotify是一种强大的、细粒度的、异步的文件系统事件监控机制,Linux内核从2.6.13起,加入了对Inotify的支持,inotify-tools是用c编写的,除了要求内核支持inotify外,不依赖于其他。inotify-tools提供两种工具,一是inotifywait,它是用来监控文件或目录的变化,二是inotifywatch,它是用来统计文件系统访问的次数。通过Inotify可以监控文件系统中的添加、删除、修改、移动等各种事件.


安装

        见CentOS6.8使用Rsync+Inotify-tools实现数据实时同步

配置

      Rsync+Inotify-tools实现数据实时同步实现通过脚本来实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
[root@localhost inotify] # cat rsync.sh 
#!/bin/bash
src_dir= "/app/rsync_server/"
dst_dir= "app_rsync_client"
exclude_dir= "/app/inotify/exclude.list"
rsync_user= "rsync"
rsync_passwd= "/etc/passwd.txt"
dst_ip= "10.15.43.228 10.10.2.84"
rsync_command(){
                   rsync  -avH --port=873 --progress --delete --exclude-from=$exclude_dir $src_dir $rsync_user@$ip::$dst_dir --password- file =$rsync_passwd
}
for  ip  in  $dst_ip; do
      rsync_command     
done
     /app/inotify/bin/inotifywait  -mrq --timefmt  '%d/%m/%y %H:%M'  -- format  '%T %w%f%e'  -e close_write,modify,delete,create,attrib,move $src_dir \
while  read  file ; do
    for  ip  in  $dst_ip; do
        rsync_command
        echo  "${file} was rsynced"  >>  /tmp/rsync .log 2>&1
    done
  done        
[root@localhost inotify] # chmod +x rsync.sh
[root@localhost inotify] # touch /app/inotify/exclude.list
[root@localhost inotify] # vim /etc/rc.d/rc.local
nohup  /bin/sh  /app/inotify/rsync .sh &
[root@localhost inotify] # nohup /bin/sh /app/inotify/rsync.sh &


inotifywait

语法:

inotifywait [-hcmrq] [-e ] [-t ] [--format ] [--timefmt ] [ ... ]

参数:

-h,--help输出帮助信息

@排除不需要监视的文件,可以是相对路径,也可以是绝对路径。

--fromfile 从文件读取需要监视的文件或排除的文件,一个文件一行,排除的文件以@开头。

-m, --monitor保持一直监听,无限期地执行。默认的行为是接收到一个事情后立即退出。

-d, --daemon跟--monitor一样,除了是在后台运行,需要指定--outfile把事情输出到一个文件。也意味着使用了--syslog。

-o, --outfile 输出事情到一个文件而不是标准输出。

-s, --syslog输出错误信息到系统日志

-r, --recursive监视一个目录下的所有子目录。

-q, --quiet指定一次,不会输出详细信息,指定二次,除了致命错误,不会输出任何信息。

--exclude 正则匹配需要排除的文件,大小写敏感。

--excludei 正则匹配需要排除的文件,忽略大小写。

-t , --timeout 设置超时时间,如果为0,则无限期地执行下去。

-e , --event 指定监视的事件。

-c, --csv输出csv格式。

--timefmt 指定时间格式,用于--format选项中的%T格式。

                "%”后面的大小写代表不同的格式,如%y表示2位的年,%Y表示4位的年

%y年 %m月 %d日 %H小时 %M分钟 %S 秒

--format 指定输出格式。

                %w 表示发生事件的目录

                %f 表示发生事件的文件

                %e 表示发生的事件

                %Xe 事件以“X"分隔

                %T 使用由--timefmt定义的时间格式


inotifywatch

语法:

inotifywatch [-hvzrqf] [-e ] [-t ] [-a ] [-d ] [ ... ]

参数:

-h, --help输出帮助信息

-v, --verbose输出详细信息

@排除不需要监视的文件,可以是相对路径,也可以是绝对路径。

--fromfile 从文件读取需要监视的文件或排除的文件,一个文件一行,排除的文件以@开头。

-z, --zero输出表格的行和列,即使元素为空

--exclude 正则匹配需要排除的文件,大小写敏感。

--excludei 正则匹配需要排除的文件,忽略大小写。

-r, --recursive监视一个目录下的所有子目录。

-t , --timeout 设置超时时间

-e , --event 监听指定的事件。

-a , --ascending 以指定事件升序排列。

-d , --descending 以指定事件降序排列。


可监听事件

access文件读取

modify文件更改。

attrib文件属性更改,如权限,时间戳等。

close_write以可写模式打开的文件被关闭,不代表此文件一定已经写入数据。

close_nowrite以只读模式打开的文件被关闭。

close文件被关闭,不管它是如何打开的。

open文件打开。

moved_to一个文件或目录移动到监听的目录,即使是在同一目录内移动,此事件也触发。

moved_from一个文件或目录移出监听的目录,即使是在同一目录内移动,此事件也触发。

move包括moved_to和 moved_from

move_self文件或目录被移除,之后不再监听此文件或目录。

create文件或目录创建

delete文件或目录删除

delete_self文件或目录移除,之后不再监听此文件或目录

unmount文件系统取消挂载,之后不再监听此文件系统。




本文转自 justin_peng 51CTO博客,原文链接:http://blog.51cto.com/ityunwei2017/1952655,如需转载请自行联系原作者

相关文章
|
1月前
|
运维 监控 Linux
Linux Rsync服务详解(二)——Rsync服务实战
Linux Rsync服务详解(二)——Rsync服务实战
18 1
|
监控 Linux
Linux 使用inotify-tools监听文件或目录
转载: Inotify一种强大的、细粒度的、异步文件系统监控机制,它满足各种各样的文件监控需要,可以监控文件系统的访问属性、读写属性、权限属性、删除创建、移动等操作,也就是可以监控文件发生的一切变化。
1309 0
|
网络协议 Linux Shell
|
监控 Linux 数据安全/隐私保护

热门文章

最新文章