rsyncremote sync可以实现本地和远端数据的同步。基于rsync算法类似有提取特征码实现数据的增量备份。帮助文档中的定义是rsync -- a fast, versatile, remote (and local) file-copying tool。
一、rsync的特性
-
可以镜像保存整个目录和文件系统
-
安全可以使用scp、ssh方式来传输文件数据安全性较高
-
可以比较容易的做到保持文件原来的权限、时间、软硬链接等属性
-
文件支持匿名传输方便进行网站镜像
-
快速第一次同步时rsync会复制全部的内容但在下一次时只同步修改过的文件。rsync在传输数据过程中可以实行压缩和解压缩操作可以使用更少的带宽。
二、rsync的工作模式
第一种Local模式类似于本地cp,install命令。
使用格式 rsync [OPTION...] SRC... [DEST]
第二种Access via remote shell远端shell模式类似于scp命令
使用格式
Pull: rsync [OPTION...] [USER@]HOST:SRC... [DEST] 本地向远端推送文件
Push: rsync [OPTION...] SRC... [USER@]HOST:DEST 本地向远端来去文件
第三种列表模式
使用格式rsync -n [-v] [USER@]HOST:DEST
第四种 Access via rsync daemon服务模式此时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
三、rsyn命令
常见的参数如下:
-v,--verbose |
详细的输出模式 |
-q,--quiet |
静默模式 |
-c,--checksum |
开启校验功能 |
-r,--recursive |
递归复制 |
-a.--archive |
归档保留文件的原有属性。等价于 -rlptgoD |
-l,--links |
保留符号链接 |
-p,--perms |
保留文件的权限 |
-t,--times |
保留文件的时间戳 |
-g,-o
--group,--owner |
保留文件的属主属组 |
-D-devices --specials |
保留设备文件 |
-e ssh |
使用ssh作为传输参数 |
-z,--compress |
压缩传输可以使用--compress-level指定压缩级别 |
--progress |
显示进度 |
--password-file=FILE |
读取密码文件 |
--delete 参数的目的是在同步的时候删除多余的文件,只保留同步文件,其他无关的文件,都删除。
例如:rsync -a --delete /test1 /test2 ,test1是一个空文件目录,在没有同步之前,test2目录里有好多文件,但是同步之后,test2 此时就变为空目录了。
常常用于删除大量文件,例如:我们要删除 百万级 文件时,此时 rm 就不起作用了。此时,就会用到这种方式,而且所消耗的时间是可以接受的。
四、服务模式示例
至于本地模式远端shell模式和列表模式比较简单这里不再详述。但注意一点在远端shell模式下如果SRC中要复制的目录后有”/“此时会复制此目录的所有文件不包括目录本身如果没有”/“则会复制目录本身和此目录下的所有文件。
下图是对比效果


服务器模式此时rsync相当一个服务器。更简单的来说此时相当于samba文件服务器功能上主要值文件共享和上传有太大的差别配置文件有samba的也相当类似原因是rsync项目是由samba项目维护的官方站点是rsync.samba,org)。
rsync服务默认是不提供配置文件的,需要自己创建。配置文件时在/etc/rsyncd.conf。配置如下
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
|
uid = nobody
gid = root
use chroot = no
max connection = 10
strict modes = yes
pid file = /var/run/rsyncd .pid
log file = /var/log/rsyncd .log
[share]
path = /data
ignore errors = yes
read only = no
write only = no
hosts allow = 192.168.1.0 /24
uid = root
gid = root
touch /var/run/rsyncd .pid /var/log/rsyncd .log
chkconfig rsync on
service xinetd start
|
结果

此时传输是匿名传输不进行用户名认证

要想实现用户认证非系统用户需要修改配置文件。
1
2
3
4
5
6
7
8
9
10
|
auth users = tom,jerry
secrets file = /etc/rsyncd .pass
tom:guoting
jerry:guoting
chmod 600 /etc/rsyncd .pass
|

四、rsync+inotify实现数据的时时同步
1、inotify简介
inotify是一种强大的、细粒度的、异步的文件系统事件监控机制linux内核从2.6.13版本起加入了对inotify的支持。通过inotify可以监控文件系统中添加、删除、修改、移动等各种细微事件利用这个内核接口第三方软件可以监控文件系统下文件的各种变化情况inotify-tools就是这样的一个第三方软件。inotify-tools提供两种工具一是inotifywait它是用来监控文件或目录的变化二是inotifywatch它是用来统计文件系统访问的次数。
2、inotify软件的安装
首先查看内核支持inotify的机制有对应的参数表示支持。

安装inotify-tools软件可以使用源码和rpm方式安装epel源中。这里使用rpm方式
yum install inotify-tools -y

安装完成后会提供inotifywait和inotifywatch 2个命令。
inotifywait命令介绍
inotifywait - wait for changes to files using inotify。使用inotify机制来等待文件的变化。
使用格式inotifywait [ options ] file1 [ file2 ] [ file3 ] [ ... ]
常见的选项如下
-m|--monitor
|
保持监听
|
-r|--recursive
|
递归监听
|
-q|--quiet
|
只打印events信息
|
-qq
|
不打印任何信息
|
--format <fmt>
|
指定特定的格式
|
--timefmt <fmt>
|
指定时间格式
|
-e|--event <event1>
|
常见的监听动作
access、modify、attrib、close_write、close_nowrite、close、open、moved_to
moved_from、move、create、delete、delete_self、unmount
|
3、inotify的配置实现
实验拓扑图如下实现在192.168.1.77上只要有文件指定文件改变就push到远端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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
|
uid = nobody
gid = nobody
use chroot = no
max connection = 10
strict modes = yes
pid file = /var/run/rsyncd .pid
log file = /var/log/rsyncd .log
[share]
path = /data
ignore errors = yes
read only = no
write only = no
hosts allow = 192.168.1.0 /24
uid = root
gid = root
auth users = tom,jerry
secrets file = /etc/rsyncd .pass
mkdir /data
touch /var/run/rsyncd .pid /var/log/rsyncd .log
tom:guoting
jerry:guoting
chmod 600 /etc/rsyncd .pass
chkconfig rsync on
service xinetd restart
uid = nobody
gid = nobody
use chroot = no
max connection = 10
strict modes = yes
pid file = /var/run/rsyncd .pid
log file = /var/log/rsyncd .log
[share]
path = /data
ignore errors = yes
read only = no
write only = no
hosts allow = 192.168.1.0 /24
uid = root
gid = root
auth users = tom,jerry
secrets file = /etc/rsyncd .pass
mkdir /data
touch /var/run/rsyncd .pid /var/log/rsyncd .log
tom:guoting
jerry:guoting
chmod 600 /etc/rsyncd .pass
chkconfig rsync on
service xinetd restart
echo "guoting" > /etc/rsync .pass
chmod 600 /etc/rsync .pass
mkdir /mdata
yum install inotify-tools -y
passfile= /etc/rsync .pass
rsyncd1=192.168.1.66
rsyncd2=192.168.1.99
src= /mdata
dst=share
user=tom
/usr/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' -- format '%T %w%f' -e close_write,delete,create,attrib $src | while read line \
do
/usr/bin/rsync -az --delete --progress --timeout=120 --password- file =$passfile $src $user@$rsyncd1::$dst &> /dev/null
/usr/bin/rsync -az --delete --progress --timeout=120 --password- file =$passfile $src $user@$rsyncd2::$dst &> /dev/null
done
chmod +x ~ /rsyncmon .sh
~ /rsyncmon .sh &
|
结果
192.168.1.77上创建一个文件

在192.168.1.66和192.168.1.99上查看

查看对应的日志信息注意格式
补充
1、如果想开机自动监听echo "/root/rsyncmon.sh &" >> /etc/rc.d/rc.local
2、有关inotify的内核参数
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
查看系统默认参数值
sysctl -a | grep max_queued_events
结果是fs.inotify.max_queued_events = 16384
sysctl -a | grep max_user_watches
结果是fs.inotify.max_user_watches = 8192
sysctl -a | grep max_user_instances
结果是fs.inotify.max_user_instances = 128
参数说明
max_queued_events
inotify队列最大长度如果值太小会出现 "** Event Queue Overflow **" 错误导致监控文件不准确
max_user_watches
要同步的文件包含多少目录可以用 find /path - type d | wc -l 统计必须保证max_user_watches值大于统计结果这里 /path 为同步文件目录
max_user_instances
每个用户创建inotify实例最大值
|
over.
网友评论