rsync(二):rsync的三种方式的练习

前言:
rsync的常用参数:

[options]:
-v:显示rsync过程中详细信息。可以使用"-vvvv"获取更详细的信息。
-P:显示文件传输的进度信息。(实际上"-P"="--partial --progress",其中的"--progress"才是显示进度信息的。)
-n / --dry-run:仅测试传输,而不实际传输。常和"-vvvv"配合使用来查看rsync是如何工作的。
-a / --archive:归档模式,表示递归传输并保持文件属性。等同于"-rtogpDl"
-r / --recursive:递归到目录中去。
-t / ---times:保持mtime属性。强烈建议任何时候都加上"-t",否则目标文件mtime会设置为系统时间,导致下次更新检查出mtime不同从而导致增量传输无效。
-o / --owner:保持owner属性(属主)。
-g / --group:保持gruop属性(属组)。
-p / --perms:保持perms属性(权限,不包括特殊权限)。
-D:是"--device --specials"选项的组合,即也拷贝设备文件和特殊文件。
-l / --links:如果文件时软链接文件,则拷贝软链接本身而非软链接所指向的对象。
-z:传输时进行压缩提高效率。
-R / --relative:使用相对路径。意味着将命令行中指定的全路径而非路径最尾部的的文件名发送给服务端,包括他们的属性。用法见下文示例。
--size-only:默认算法是检查文件大小和mtime不同的文件,使用此选项将只检查文件大小。
-u / --update:仅在源mtime比目标已存在文件的mtime新时才拷贝。注意,该选项是接收端判断的,不会影响删除行为。
-d / --dirs:以不递归的方式拷贝目录本身。默认递归时,如果源为"dir1/file1",则不会拷贝dir1目录,使用该选项将拷贝dir1但不拷贝file1。
--bwlimit: 限制带宽,默认单位是:kb(案例:某DBA做数据同步,导致用户无法访问网站)
-max-size:限制rsync传输的最大文件大小。可以使用单位后缀,还可以是一个小数值(例如:"--max-size=1.5m")
-min-size:限制rsync传输的最小文件大小。这可以用于禁止传输小文件或那些垃圾文件。
-b / --backup:对目标上已存在的文件做一个备份,备份的文件名默认使用"~"做后缀。
--backup-dir:指定备份文件的保存路径。不指定时默认和待备份文件保存在同一目录下。
-e:指定所要使用的远程shell程序,默认为ssh。
--port:连接daemon时使用的端口号,默认为873端口。
--progress:显示数据传输的进度信息
--password-file:daemon模式时的密码文件,可以从中读取密码实现非交互式。注意,这不是远程shell认证的密码,而是rsync模块认证的密码。
-W --whole-file:rsync将不再使用增量传输,而是全量传输。在网络带宽高于磁盘带宽时,该选项比增量传输更高效。(rsync默认是增量传输)
--existing:要求只更新目标端已存在的文件,目标端还不存在的文件不传输。注意,使用相对路径时如果上层目录不存在也不会传输。
ignore-existing:要求只更新目标端不存在的文件。和"--existing"结合使用有特殊功能,见下文示例。
-exclude:指定排除规则来排除不需要传输的文件。
-delete:以SRC为主,对DEST进行同步。多则删之,少则补之。注意"--delete"是在接收端执行的,所以它是在exclude/include规则生效之后才执行的。
--remove-source-files:要求删除源端已经成功传输的文件。

在这里插入图片描述

方式一:本地模式

Local:rsync [OPTION…] SRC… [DEST]
练习:目录1–>/data/client-1,空目录2–>/data/client-2
在这里插入图片描述
练习:

'1.将/data/client-1目录及其子文件和子目录同步到/data/client-2/目录下。'
#rsync -a /data/client-1  /data/client-2/
'2.将/data/client-1目录下的文件和子目录同步到/data/client-2/目录下。'
#rsync -a /data/client-1/  /data/client-2/
'3.将/data/client-1目录下的一级子文件同步到/data/client-2/目录下,不用保留源文件的属性。'
[root@gaokai data]# rsync -Dl  /data/client-1/*  /data/client-2/
skipping directory DIR
'4.将/data/client-1目录下的的file文件同步到/data/client-2目录下。'
[root@gaokai data]# rsync  /data/client-1/file  /data/client-2/
'5.将目录/data/client-1/file复制到/data/client-2目录下,同时将file文件的绝对路径作为目标目录下子目录依次生成。'
[root@gaokai client-1]# rsync -Rr /data/client-1/file  /data/client-2/
'6.将目录/data/client-1/file复制到/data/client-2目录下,同时将file文件的相对路径/client-1/file作为目标目录下子目录依次生成。'
[root@gaokai client-1]# rsync -Rr /data/./client-1/file  /data/client-2/
'7.将/data/client-1目录下的内容同步到/data/client-2目录下,除去/data/client-1/file文件.'
#rsync -nr -i  /data/client-1  /data/client-2/     ##可以用该命令判断--exculde参数所指定的文件相对路径
[root@gaokai client-1]# rsync -a --exclude="file"  /data/client-1/  /data/client-2/
[root@gaokai client-1]# rsync -a --exclude="client-1/file"  /data/client-1  /data/client-2/
'8.将/data/client-1目录下的内容同步到/data/client-2目录下,除去/data/client-1/file文件,然后经过3分钟,将/data/client-1目录下的file文件更新到/data/client-2目录下,旧有的file文件以~为文件后缀,作为备份文件。'
[root@gaokai client-1]# rsync -r  --backup  /data/client-1/file  /data/client-2/
[root@gaokai client-1]# ll /data/client-2/
total 16
drwxr-xr-x 3 root root   33 May 19 16:53 DIR
-rw-r--r-- 1 root root    3 May 19 15:09 ex1.txt
-rw-r--r-- 1 root root    3 May 19 15:09 ex2.txt
-rw-r--r-- 1 root root 3075 May 19 17:26 file
-rw-r--r-- 1 root root 3072 May 19 17:24 file~
'9.问题同8,只是将默认文件后缀"~",修改为"-"     '
[root@gaokai client-1]# rsync -r  --backup --suffix=-  /data/client-1/file  /data/client-2/
[root@gaokai client-1]# ll /data/client-2/
total 16
drwxr-xr-x 3 root root   33 May 19 16:53 DIR
-rw-r--r-- 1 root root    3 May 19 15:09 ex1.txt
-rw-r--r-- 1 root root    3 May 19 15:09 ex2.txt
-rw-r--r-- 1 root root 3075 May 19 17:34 file
-rw-r--r-- 1 root root 3075 May 19 17:26 file-

方拾二:shell模式

Access via remote shell:
Pull:#rsync [option…] [USER@]HOST:SRC… [DEST]
Push:#rsync [option…] SRC… [USER@]HOST:DEST
练习:
源IP地址:192.168.171.129 /data/src_1 有cctv用户
目标IP地址:192.168.171.128 /data/dest_1 有cctv用户
在目标主机操作

`1.将/data/src_1目录及其子文件和子目录同步到/data/dest_1/目录下。`
[root@gaokai data]# rsync  -avz  -e "ssh -p 22" cctv@192.168.171.129:/data/src_1  /data/dest_1/
cctv@192.168.171.129s password: 
receiving incremental file list
src_1/
src_1/file1
src_1/file2
src_1/file3
src_1/file4
src_1/example/

sent 112 bytes  received 297 bytes  54.53 bytes/sec
total size is 0  speedup is 0.00
[root@gaokai data]# tree dest_1/
dest_1/
└── src_1
    ├── example
    ├── file1
    ├── file2
    ├── file3
    └── file4

2 directories, 4 files

`2.将/data/src_1目录下的文件和子目录同步到/data/dest_1/目录下。`
[root@gaokai dest_1]# rsync  -avz  -e "ssh -p 22" cctv@192.168.171.129:/data/src_1/  /data/dest_1/
cctv@192.168.171.129s password: 
receiving incremental file list
./
file1
file2
file3
file4
example/

sent 111 bytes  received 286 bytes  113.43 bytes/sec
total size is 0  speedup is 0.00
[root@gaokai dest_1]# tree
.
├── example
├── file1
├── file2
├── file3
└── file4

1 directory, 4 files
[root@gaokai dest_1]# ls
example  file1  file2  file3  file4
`3.将/data/src_1目录下的一级子文件同步到/data/dest_1/目录下,且保留源文件的各种属性。`
[root@gaokai dest_1]# rsync   -togpDl  -e "ssh -p 22" cctv@192.168.171.129:/data/src_1/*  /data/dest_1/
cctv@192.168.171.129s password: 
skipping directory example
[root@gaokai dest_1]# ls
file1  file2  file3  file4

`4.将/data/src_1/example/目录下的的11file文件同步到/data/dest_1目录下,且保留原属性。`
[root@gaokai dest_1]# rsync  -togp  -e "ssh -p 22" cctv@192.168.171.129:/data/src_1/example/11file  /data/dest_1/
cctv@192.168.171.129s password: 
[root@gaokai dest_1]# ll
total 0
-rw-r--r-- 1 cctv cctv 0 May 19 20:56 11file

`5.将/data/src_1/example/目录下的超过5M的文件同步到/data/dest_1目录下,且保留原属性。`
[root@gaokai src_1]# ll -h
total 10M
drwxr-xr-x 2 root root  20 May 19 21:24 example
-rw-r--r-- 1 cctv cctv   0 May 19 18:19 file1
-rw-r--r-- 1 root root   0 May 19 18:19 file2
-rw-r--r-- 1 root root   0 May 19 18:19 file3
-rw-r--r-- 1 root root 10M May 19 21:24 file4
[root@gaokai src_1]# ll -h example/
total 20M
-rw-r--r-- 1 root root 20M May 19 21:24 11file
[root@gaokai dest_1]# rsync  -avz --min-size=5M  -e "ssh -p 22" cctv@192.168.171.129:/data/src_1/  /data/dest_1/
cctv@192.168.171.129s password: 
receiving incremental file list
./
file4
example/
example/11file

sent 77 bytes  received 30,881 bytes  8,845.14 bytes/sec
total size is 31,457,280  speedup is 1,016.13
[root@gaokai dest_1]# ll -h 
total 10M
drwxr-xr-x 2 root root  20 May 19 21:24 example
-rw-r--r-- 1 root root 10M May 19 21:24 file4
[root@gaokai dest_1]# ll -h  example/
total 20M
-rw-r--r-- 1 root root 20M May 19 21:24 11file
`6.将/data/src_1目录下的文件同步到/data/dest_1目录后,等待3min,然后再次进行同步,同时将旧有文件做备份,备份目录为/data/dest_bak,备份文件后缀为-bak,保留原属性。`
------src旧
[root@gaokai src_1]# ll -h
total 10M
drwxr-xr-x 2 root root  20 May 19 21:24 example
-rw-r--r-- 1 cctv cctv   0 May 19 18:19 file1
-rw-r--r-- 1 root root   0 May 19 18:19 file2
-rw-r--r-- 1 root root   0 May 19 18:19 file3
-rw-r--r-- 1 root root 10M May 19 21:24 file4
[root@gaokai src_1]# ll -h example/
total 20M
-rw-r--r-- 1 root root 20M May 19 21:24 11file
------同步1
[root@gaokai dest_1]# rsync -avz  -e "ssh -p 22" cctv@192.168.171.129:/data/src_1/  /data/dest_1/
cctv@192.168.171.129s password: 
receiving incremental file list
./
file1
file2
file3
file4
example/
example/11file

sent 134 bytes  received 30,989 bytes  12,449.20 bytes/sec
total size is 31,457,280  speedup is 1,010.74
-----src文件修改后
[root@gaokai src_1]# ll
total 10256
drwxr-xr-x 2 root root       20 May 19 21:24 example
-rw-r--r-- 1 cctv cctv        2 May 19 22:05 file1
-rw-r--r-- 1 root root        2 May 19 22:05 file2
-rw-r--r-- 1 root root        2 May 19 22:05 file3
-rw-r--r-- 1 root root 10485762 May 19 22:05 file4
----同步且备份
[root@gaokai dest_1]# rsync -avz  --backup --suffix=-bak --backup-dir=/data/dest_bak/ -e "ssh -p 22" cctv@192.168.171.129:/data/src_1/  /data/dest_1/
cctv@192.168.171.129s password: 
receiving incremental file list
file1
file2
file3
file4
example/11file

sent 47,096 bytes  received 447 bytes  13,583.71 bytes/sec
total size is 31,457,291  speedup is 661.66
----查看
[root@gaokai dest_1]# ll /data/dest_1/
total 10256
drwxr-xr-x 2 root root       20 May 19 21:24 example
-rw-r--r-- 1 cctv cctv        2 May 19 22:05 file1
-rw-r--r-- 1 root root        2 May 19 22:05 file2
-rw-r--r-- 1 root root        2 May 19 22:05 file3
-rw-r--r-- 1 root root 10485762 May 19 22:05 file4
[root@gaokai dest_1]# ll /data/dest_bak/
total 10240
drwxr-xr-x 2 root root       24 May 19 22:06 example
-rw-r--r-- 1 cctv cctv        0 May 19 18:19 file1-bak
-rw-r--r-- 1 root root        0 May 19 18:19 file2-bak
-rw-r--r-- 1 root root        0 May 19 18:19 file3-bak
-rw-r--r-- 1 root root 10485760 May 19 21:24 file4-bak

`7.以/data/src_1为源,对于/data/dest_1目录,只更新目标端已存在的文件。`
---目标端、源端(已修改后)[root@gaokai dest_1]# ll
total 0
-rw-r--r-- 1 root root 0 May 19 22:11 file1
-rw-r--r-- 1 root root 0 May 19 22:11 file2
-rw-r--r-- 1 root root 0 May 19 22:11 file3

[root@gaokai src_1]# ll
total 10256
drwxr-xr-x 2 root root       20 May 19 21:24 example
-rw-r--r-- 1 cctv cctv        5 May 19 22:13 file1
-rw-r--r-- 1 root root        5 May 19 22:13 file2
-rw-r--r-- 1 root root        5 May 19 22:13 file3
-rw-r--r-- 1 root root 10485765 May 19 22:13 file4
---执行
[root@gaokai dest_1]# rsync -avz  --existing  -e "ssh -p 22" cctv@192.168.171.129:/data/src_1/  /data/dest_1/
cctv@192.168.171.129s password: 
receiving incremental file list
./
file1
file2
file3

sent 85 bytes  received 333 bytes  167.20 bytes/sec
total size is 31,457,306  speedup is 75,256.71
[root@gaokai dest_1]# ll 
total 12
-rw-r--r-- 1 cctv cctv 5 May 19 22:13 file1
-rw-r--r-- 1 root root 5 May 19 22:13 file2
-rw-r--r-- 1 root root 5 May 19 22:13 file3
`8.以/data/src_1为源,对于/data/dest_1目录,只更新目标端不已存在的文件。`
---目标端、源端(已修改后)[root@gaokai dest_1]# ll
total 0
-rw-r--r-- 1 root root 0 May 19 22:13 file1
-rw-r--r-- 1 root root 0 May 19 22:13 file2
-rw-r--r-- 1 root root 0 May 19 22:13 file3

[root@gaokai src_1]# ll
total 10256
drwxr-xr-x 2 root root       20 May 19 21:24 example
-rw-r--r-- 1 cctv cctv        5 May 19 22:16 file1
-rw-r--r-- 1 root root        5 May 19 22:16 file2
-rw-r--r-- 1 root root        5 May 19 22:16 file3
-rw-r--r-- 1 root root 10485765 May 19 22:16 file4
---执行
[root@gaokai dest_1]# rsync -avz  --ignore-existing  -e "ssh -p 22" cctv@192.168.171.129:/data/src_1/  /data/dest_1/
cctv@192.168.171.129s password: 
receiving incremental file list
file4
example/
example/11file

sent 74 bytes  received 30,896 bytes  12,388.00 bytes/sec
total size is 31,457,321  speedup is 1,015.74
[root@gaokai dest_1]# ll
total 10256
drwxr-xr-x 2 root root       20 May 19 21:24 example
-rw-r--r-- 1 cctv cctv        5 May 19 22:13 file1
-rw-r--r-- 1 root root        5 May 19 22:13 file2
-rw-r--r-- 1 root root        5 May 19 22:13 file3
-rw-r--r-- 1 root root 10485768 May 19 22:16 file4
`9.以/data/src_1为源,先删除/data/dest_1目录下多出来的文件,再同步/data/dest_1目录。`
----前:若/data/dest_1/目录下原有其它文件,直接avz同步的结果:
[root@gaokai src_1]# ll
total 10256
drwxr-xr-x 2 root root       20 May 19 21:24 example
-rw-r--r-- 1 cctv cctv        8 May 19 22:16 file1
-rw-r--r-- 1 root root        8 May 19 22:16 file2
-rw-r--r-- 1 root root        8 May 19 22:16 file3
-rw-r--r-- 1 root root 10485768 May 19 22:16 file4
[root@gaokai dest_1]# ls
r  rr
[root@gaokai dest_1]# rsync -avz    -e "ssh -p 22" cctv@192.168.171.129:/data/src_1/  /data/dest_1/
cctv@192.168.171.129s password: 
receiving incremental file list
./
file1
file2
file3
file4
example/
example/11file

sent 134 bytes  received 31,033 bytes  12,466.80 bytes/sec
total size is 31,457,321  speedup is 1,009.32
[root@gaokai dest_1]# ls
example  file1  file2  file3  file4  r  rr
----结果:原有文件/目录依旧存在
--执行
[root@gaokai src_1]# ll
total 10256
drwxr-xr-x 2 root root       20 May 19 21:24 example
-rw-r--r-- 1 cctv cctv       14 May 19 22:29 file1
-rw-r--r-- 1 root root       14 May 19 22:29 file2
-rw-r--r-- 1 root root       11 May 19 22:29 file3
-rw-r--r-- 1 root root 10485771 May 19 22:29 file4
[root@gaokai dest_1]# ll
total 0
drwxr-xr-x 2 root root 6 May 20 03:15 r
-rw-r--r-- 1 root root 0 May 20 03:15 rr
[root@gaokai dest_1]# rsync -avz   --delete  -e "ssh -p 22" cctv@192.168.171.129:/data/src_1/  /data/dest_1/
cctv@192.168.171.129s password: 
receiving incremental file list
deleting r/
deleting rr
./
file1
file2
file3
file4
example/
example/11file

sent 134 bytes  received 31,031 bytes  12,466.00 bytes/sec
total size is 31,457,342  speedup is 1,009.38
[root@gaokai dest_1]# ll
total 10256
drwxr-xr-x 2 root root       20 May 19 21:24 example
-rw-r--r-- 1 cctv cctv       14 May 19 22:29 file1
-rw-r--r-- 1 root root       14 May 19 22:29 file2
-rw-r--r-- 1 root root       11 May 19 22:29 file3
-rw-r--r-- 1 root root 10485771 May 19 22:29 file4
`10.以/data/src_1为源,不传输文件,但是会删除接收端/data/dest_1目录下多出来的文件`
[root@gaokai src_1]# ll
total 10256
drwxr-xr-x 2 root root       20 May 19 21:24 example
-rw-r--r-- 1 cctv cctv       14 May 19 22:29 file1
-rw-r--r-- 1 root root       14 May 19 22:29 file2
-rw-r--r-- 1 root root       11 May 19 22:29 file3
-rw-r--r-- 1 root root 10485771 May 19 22:29 file4

[root@gaokai dest_1]# ll
total 10256
drwxr-xr-x 2 root root       20 May 19 21:24 example
-rw-r--r-- 1 cctv cctv       11 May 19 22:27 file1
-rw-r--r-- 1 root root       11 May 19 22:27 file2
-rw-r--r-- 1 root root        8 May 19 22:16 file3
-rw-r--r-- 1 root root 10485768 May 19 22:16 file4
drwxr-xr-x 2 root root        6 May 19 22:29 r
-rw-r--r-- 1 root root        0 May 19 22:29 rr

----执行

[root@gaokai dest_1]# rsync -avz   --delete --ignore-existing -existing  -e "ssh -p 22" cctv@192.168.171.129:/data/src_1/  /data/dest_1/
cctv@192.168.171.129s password: 
receiving incremental file list
deleting r/
deleting rr
./

sent 28 bytes  received 193 bytes  147.33 bytes/sec
total size is 31,457,342  speedup is 142,340.91
[root@gaokai dest_1]# ll
total 10256
drwxr-xr-x 2 root root       20 May 19 21:24 example
-rw-r--r-- 1 cctv cctv       11 May 19 22:27 file1
-rw-r--r-- 1 root root       11 May 19 22:27 file2
-rw-r--r-- 1 root root        8 May 19 22:16 file3
-rw-r--r-- 1 root root 10485768 May 19 22:16 file4

`11.以/data/src_1目录为源,同步到/data/dest_1目录,然后删除源/data/src_1目录下的文件。(注意用户,此同步最好用root用户远程,防止权限不够无法删除源端目录/文件)`
[root@gaokai src_1]# ll
total 10256
drwxr-xr-x 2 root root       20 May 19 21:24 example
-rw-r--r-- 1 cctv cctv       14 May 19 22:29 file1
-rw-r--r-- 1 root root       14 May 19 22:29 file2
-rw-r--r-- 1 root root       11 May 19 22:29 file3
-rw-r--r-- 1 root root 10485771 May 19 22:29 file4
[root@gaokai dest_1]# mkdir r
[root@gaokai dest_1]# touch rr
[root@gaokai dest_1]# rsync -avz   --remove-source-files  -e "ssh -p 22" root@192.168.171.129:/data/src_1/  /data/dest_1/
root@192.168.171.129s password: 
receiving incremental file list
./
file1
file2
file3
file4
example/
example/11file

sent 174 bytes  received 31,035 bytes  8,916.86 bytes/sec
total size is 31,457,342  speedup is 1,007.96
---查看
[root@gaokai dest_1]# ls
example  file1  file2  file3  file4  r  rr
[root@gaokai src_1]# tree /data/src_1(递归操作-a有r参数,将源目录下的文件全部删除,但会保留目录)
/data/src_1
└── example

1 directory, 0 files

`12.以/data/src_1为源,先删除/data/dest_1目录下多出来的文件,再同步/data/dest_1目录,不传输file1文件。`
[root@gaokai src_1]# ls
example  file1  file2  file3  file4
[root@gaokai dest_1]# rsync -avz   --delete --exclude=file1  -e "ssh -p 22" root@192.168.171.129:/data/src_1/  /data/dest_1/
root@192.168.171.129s password: 
receiving incremental file list
./
file2
file3
file4
example/
example/11file

sent 126 bytes  received 30,952 bytes  5,650.55 bytes/sec
total size is 31,457,328  speedup is 1,012.21
[root@gaokai dest_1]# ls
example  file2  file3  file4
`13.以/data/src_1为源,先删除/data/dest_1目录下多出来的文件,再同步/data/dest_1目录,不传输file1,file2文件。`
[root@gaokai src_1]# ls
example  file1  file2  file3  file4
[root@gaokai dest_1]# rsync -avz   --delete  --exclude=file1 --exclude=file2  -e "ssh -p 22" root@192.168.171.129:/data/src_1/  /data/dest_1/
root@192.168.171.129s password: 
receiving incremental file list
./
file3
file4
example/
example/11file

sent 118 bytes  received 30,894 bytes  12,404.80 bytes/sec
total size is 31,457,314  speedup is 1,014.36
[root@gaokai dest_1]# ls
example  file3  file4
`13.以/data/src_1为源,先删除/data/dest_1目录下多出来的文件,再同步/data/dest_1目录,不传输file1,file2,file3文件。`
[root@gaokai src_1]# ls
example  file1  file2  file3  file4
[root@gaokai dest_1]# cat /data/excl.txt 
file1
file2
file3
[root@gaokai dest_1]# rsync -avz   --delete  --exclude-from=/data/excl.txt   -e "ssh -p 22" root@192.168.171.129:/data/src_1/  /data/dest_1/        ###当exclude文件较多时,可编写文件。
root@192.168.171.129s password: 
receiving incremental file list
./
file4
example/
example/11file

sent 110 bytes  received 30,832 bytes  8,840.57 bytes/sec
total size is 31,457,303  speedup is 1,016.65
[root@gaokai dest_1]# ls
example  file4

`14.以/data/src_1为源,先删除/data/dest_1目录下多出来的文件,再同步/data/dest_1目录,不传输file1,file2,file3文件,执行命令时显示传输进度。`
[root@gaokai src_1]# ls
example  file1  file2  file3  file4
[root@gaokai dest_1]# cat /data/excl.txt 
file1
file2
file3
[root@gaokai dest_1]# rsync -avz   --delete  --exclude-from=/data/excl.txt --progress  -e "ssh -p 22" root@192.168.171.129:/data/src_1/  /data/dest_1/
root@192.168.171.129's password: 
receiving incremental file list
./
file4
     10,485,771 100%  333.33MB/s    0:00:00 (xfr#1, to-chk=3/5)
incl.txt
              0 100%    0.00kB/s    0:00:00 (xfr#2, to-chk=2/5)
example/
example/11file
     20,971,532 100%  215.05MB/s    0:00:00 (xfr#3, to-chk=0/5)

sent 129 bytes  received 30,885 bytes  12,405.60 bytes/sec
total size is 31,457,303  speedup is 1,014.29

[root@gaokai dest_1]# ls
example  file4

方式三:rsync daemon模式

服务器端:需要编辑rsync的配置文件,设置全局参数和模块参数;然后创建相应的文件/目录/用户,开启rsync服务。
服务端:192.168.10.135
客户端:192.168.10.134

配置步骤:

1.服务器端:

(1)/etc/rsyncd.conf是rsync的默认配置文件,该配置文件不存在,需要编辑内容:

[root@localhost ~]# cat /etc/rsyncd.conf
#######全局配置参数###########
port=873                  //指定rsync端口,默认是873
uid = rsync		//指定rsync服务的运行用户,默认是nobody,文件传输成功后属主将是这个uid,即rsync;
pid = rsync		//指定rsync服务的运行用户组,默认是nobody,文件传输成功后属组将是这个uid,即rsync;
use chroot = no    //rsync daemon在传输前是否切换到指定的path目录下,并将其监禁在内,有关安全;一般为no
max connections = 200    //最大连接数,0表示没有限制
timeout = 300		//超时时间,确保rsync服务器不会永远等待一个崩溃的客户端,0表示永远等待。
motd file = /var/rsyncd/rsync.motd  //客户端连接过来显示的消息。
pid file = /var/run/rsyncd.pid		//指定rsync daemon的pid文件。
lock file = /var/run/rsync.lock		//指定锁文件。
log file = /var/log/rsyncd.log		//指定rsync的日志文件,而不是把日志发送给syslog;
dont compress   = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2   //指定哪些文件不用进行压缩传输。

#########指定模块参数############
[example]		 		##模块ID
path = /data/example/		##指定模块ID的路径,该参数必须指定,启动rsync服务前该目录必须存在。rsync请求访问模块本质就是访问该路径。
ignore errors				##忽略某些IO错误信息。
read only = false			##指定该模块是否可读写,即能否上传文件,false表示可读写,true表示可读不可写。所有模块默认不可上传
write only = false			##指定该模式是否支持下载,设置为true表示客户端不能下载。所有模块默认可下载
list = false
host allow = 192.168.10.0/24  ##指定允许连接到该模块的机器,多个ip用空格隔开或者设置区间
##host deny = 0.0.0.0/32		##指定不允许连接到该模块的机器
auth users = rsync_exam,back	##指定连接到该模块的用户列表,只有列表里的用户才能连接到模块,用户名和对应密码保存在secrts file中。 这里使用的不是系统用户,而是虚拟用户。不设置时,默认所有用户都能连接,但使用的是匿名连接。
secrets file = /etc/rsyncd.passwd   ##保存auth users用户列表的用户名和密码,每行包含一个。username:passwd。		##由于"strict modes"默认为true,所以此文件要求非rsync daemon用户不可读写。只有启用了auth users该选项才有效。
[example_2]					##定义的第二个模块参数,因为设置用户,此模块登录可匿名登录。
path = /data/example_2/
read only = false
ignore errors
comment = anyone can access 

(2)依据/etc/rsyncd.conf配置文件:--->创建rsync系统用户,及模块对应的目录,编写/etc/rsyncd.passwd文件。

[root@localhost ~]#useradd  -s  /sbin/nologin  rsync.
[root@localhost ~]#mkdir /data/{example,example_2}
[root@localhost ~]#chown  -R  rsync:rsync /data/example
[root@localhost ~]#chown  -R  rsync:rsync /data/example_2
[root@localhost ~]#echo "rsync_exam:123456" >>/etc/rsyncd.passwd
[root@localhost ~]#echo "back:1234" >>/etc/rsyncd.passwd
[root@localhost ~]#chown 600 /etc/rsyncd.passwd		//rsync的用户密码文件权限必须是600

(3)启动rsync daemon服务。

[root@localhost ~]#rsync --daemon
[root@localhost ~]#ps -ef |grep rsync
至此,服务端的配置已经完毕!
2.客户端:

(1)编辑/etc/rsync.passwd配置文件,默认不存在。客户端的rsync密码配置文件的路径名称随意。

[root@localhost ~]#echo "123456" >> /etc/rsync_exam.passwd
[root@localhost ~]#echo "1234" >> /etc/rsync_back.passwd
[root@localhost ~]#chmod 600 /etc/rsync_exam.passwd     //rsync的用户密码文件权限必须是600
[root@localhost ~]#chmod 600 /etc/rsync_back.passwd      
注意:无论是服务端还是客户端,rsync服务的用户密码文件权限必须是600,否则命令会执行失败,虽然会有报错提示:ERROR: password file must not be other-accessible
3.练习:

在客户端操作:

`1.使用rsync_exam虚拟用户查看服务端的example模块下的目录文件列表。`
[root@localhost data]# rsync --list-only --port 873 rsync_exam@192.168.10.135::example/  --password-file=/etc/rsync.passwd

drwxr-xr-x             50 2020/05/21 16:03:41 .
-rw-r--r--              0 2020/05/21 16:03:41 file1
-rw-r--r--              0 2020/05/21 16:03:41 file2
drwxr-xr-x             19 2020/05/21 16:04:32 1
drwxr-xr-x             19 2020/05/21 16:04:40 2
`2.使用back虚拟用户查看example模块文件列表,同时辨别--password-file参数加与否的区别。`
[root@localhost data]# rsync --list-only --port 873 back@192.168.10.135::example/  --password-file=/etc/rsync_back.passwd

drwxr-xr-x             50 2020/05/21 16:03:41 .
-rw-r--r--              0 2020/05/21 16:03:41 file1
-rw-r--r--              0 2020/05/21 16:03:41 file2
drwxr-xr-x             19 2020/05/21 16:04:32 1
drwxr-xr-x             19 2020/05/21 16:04:40 2
[root@localhost data]# rsync --list-only --port 873 back@192.168.10.135::example/  

Password: 
drwxr-xr-x             50 2020/05/21 16:03:41 .
-rw-r--r--              0 2020/05/21 16:03:41 file1
-rw-r--r--              0 2020/05/21 16:03:41 file2
drwxr-xr-x             19 2020/05/21 16:04:32 1
drwxr-xr-x             19 2020/05/21 16:04:40 2
`3.查看没有虚拟用户的example_2模块目录列表。`
[root@localhost data]# rsync --list-only --port 873 192.168.10.135::example_2/  

drwxr-xr-x             41 2020/05/21 16:20:39 .
-rw-r--r--              0 2020/05/21 16:20:39 e.txt
-rw-r--r--              0 2020/05/21 16:20:36 w.txt
drwxr-xr-x             19 2020/05/21 16:20:45 q
[root@localhost data]# rsync --list-only --port 873 back@192.168.10.135::example_2/  

drwxr-xr-x             41 2020/05/21 16:20:39 .
-rw-r--r--              0 2020/05/21 16:20:39 e.txt
-rw-r--r--              0 2020/05/21 16:20:36 w.txt
drwxr-xr-x             19 2020/05/21 16:20:45 q
由此可见:对于example_2模块来说,没有虚拟用户限制,任何远程用户都可访问/操作。
`4.使用back用户,以example模块作为源,同步到客户端的/data/dets目录,保留文件/目录的原有属性。`
[root@localhost data]# rsync -avz --port 873 back@192.168.10.135::example/  /data/dest/ --password-file=/etc/rsync_back.passwd

receiving incremental file list
./
file1
file2
1/
1/1.txt
2/
2/2.txt

sent 123 bytes  received 354 bytes  22.19 bytes/sec
total size is 0  speedup is 0.00
[root@localhost dest]# ls
1  2  file1  file2
`5.使用back用户,以example模块作为源,同步到客户端的/data/dets目录后,同时自动删除源端传输的文件,保留文件/目录的原有属性。`
[root@localhost dest]# rsync -avz --remove-source-files  --port 873 back@192.168.10.135::example/  /data/dest/ --password-file=/etc/rsync_back.passwd

receiving incremental file list
./
file1
file2
1/
1/1.txt
2/
2/2.txt

sent 151 bytes  received 350 bytes  24.44 bytes/sec
total size is 0  speedup is 0.00
[root@localhost example]# tree		//服务端
.
├── 1
└── 2

2 directories, 0 files
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值