4-10.1 Linux 中的文件同步传输 --- rsync 常用参数(rsync 不加参数、-a,--archive参数)

  • 本地使用 rsync 进行传输,熟悉 rsync 常用参数功能 。
  • 4-10-1 内容:
    1、rsync 不加参数传输文件。
    2、-a,--archive:归档模式,递归传输和保存文件属性。
    3、源文件与目标文件大小、时间属性一致。rsync -a / --archive 目标文件不会发生变化。
    4、源文件和目标文件大小、时间属性不一致,rsync -a / --archive 目标文件会更新为源文件。
    5、源文件与目标文件大小、时间属性一致。但,权限属性不一致。rsync -a / --archive 目标文件也会更新为源文件一致的权限属性。

  • 首先创建测试资料。~ 家目录下新建一个 test 目录作为测试资料的装载目录(父目录)。test 目录下新建 SRC 目录用作源文件的目录。SRC 目录下新建三个文件,分别是 demo1.txt、demo2.txt 和 demo3.txt。
[root@localhost ~]# mkdir test    ## 家目录下新建 test 目录
[root@localhost ~]# 
[root@localhost ~]# cd test/
[root@localhost test]# 
[root@localhost test]# mkdir SRC    ## test 目录下新建 SRC 目录
[root@localhost test]# 
[root@localhost test]# cd SRC/
[root@localhost SRC]# 

## SRC 目录下新建 demo1.txt、demo2.txt 和 demo3.txt 三个文件
[root@localhost SRC]# touch demo1.txt ; touch demo2.txt ; touch demo3.txt    
[root@localhost SRC]# 
[root@localhost SRC]# ll    ## 查看新建的文件
total 0
-rw-r--r--. 1 root root 0 Jul 22 22:44 demo1.txt
-rw-r--r--. 1 root root 0 Jul 22 22:44 demo2.txt
-rw-r--r--. 1 root root 0 Jul 22 22:44 demo3.txt
0 directories, 3 files
  • 安装 tree 工具,树形显示目录结构,方便查看,yum -y install tree。
[root@localhost test]# 
[root@localhost test]# rpm -qa tree      ## 查看系统有没有安装 tree 包

## 没有返回空,有会返回 rpm包。有 tree包不用再安装。
[root@localhost test]#                  
[root@localhost test]# yum -y install tree      ## yum 安装 tree 包。
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
base                                                                            | 3.6 kB  00:00:00     
extras                                                                          | 2.9 kB  00:00:00     
updates                                                                         | 2.9 kB  00:00:00     
Resolving Dependencies
--> Running transaction check
---> Package tree.x86_64 0:1.6.0-10.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

=======================================================================================================
 Package              Arch                   Version                        Repository            Size
=======================================================================================================
Installing:
 tree                 x86_64                 1.6.0-10.el7                   base                  46 k

Transaction Summary
=======================================================================================================
Install  1 Package

Total download size: 46 k
Installed size: 87 k
Downloading packages:
tree-1.6.0-10.el7.x86_64.rpm                                                    |  46 kB  00:00:00     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Warning: RPMDB altered outside of yum.
  Installing : tree-1.6.0-10.el7.x86_64                                                            1/1 
  Verifying  : tree-1.6.0-10.el7.x86_64                                                            1/1 

Installed:
  tree.x86_64 0:1.6.0-10.el7      ## 安装完成会显示 tree 包的版本                                                                           

Complete!

## test 目录下输入 tree 就可以树形结构显示 test 目录下的文件信息。
[root@localhost test]# tree
.
└── SRC
    ├── demo1.txt
    ├── demo2.txt
    └── demo3.txt

1 directory, 3 files
[root@localhost test]# 

1、rsync 不加参数传输文件。

  • 步骤如下:
  • ~ 家目录下创建一个 backup_null_option 目录,用作目标目录(备份目录)。
  • 查看当前目录结构和 SRC 目录下的文件详情。
  • rsync 不加参数和 cp 不加参数传输文件对比。
  • 查看 backup_null_option 、SRC 目录下的文件进行对比,查看当前目录结构。
## 第一阶段:创建 backup_null_option 目标目录

[root@localhost test]# mkdir backup_null_option   
[root@localhost test]# 
-----------------------------------------------------------------

## 第二阶段:查看当前目录结构和 SRC 目录下的文件详情。

[root@localhost test]# tree    ## 当前目录结构
.
├── backup_null_option
└── SRC
    ├── demo1.txt
    ├── demo2.txt
    └── demo3.txt

2 directories, 3 files
[root@localhost test]# 

[root@localhost test]# 
[root@localhost test]# ll SRC/    ## SRC 源目录下的文件详情
total 0
-rw-r--r--. 1 root root 0 Jul 22 22:44 demo1.txt
-rw-r--r--. 1 root root 0 Jul 22 22:44 demo2.txt
-rw-r--r--. 1 root root 0 Jul 22 22:44 demo3.txt
[root@localhost test]# 
------------------------------------------------------------------

## 第三阶段:rsync 不加参数和 cp 不加参数传输 SRC文件对比。

[root@localhost test]# 
## rsync 不加参数传输 SRC 目录下 demo1.txt 文件到 backup_null_option/ 目录。
[root@localhost test]# rsync SRC/demo1.txt backup_null_option/    
[root@localhost test]# 
## cp 不加参数传输 SRC 目录下 demo2.txt 文件到 backup_null_option/ 目录。
[root@localhost test]# cp SRC/demo2.txt backup_null_option/
[root@localhost test]# 
-------------------------------------------------------------------

##  第四阶段:查看 backup_null_option 、SRC 目录下的文件进行对比,和查看当前目录结构。

[root@localhost test]# ll backup_null_option/    ## backup_null_option 目标目录下的文件详情
total 0
## demo1.txt、demo2.txt 文件时间和 SRC 源目录下的不一致。
-rw-r--r--. 1 root root 0 Jul 22 23:31 demo1.txt    
-rw-r--r--. 1 root root 0 Jul 22 23:31 demo2.txt
[root@localhost test]# 
[root@localhost test]# tree    ## 现时目录结构
.
├── backup_null_option
│   ├── demo1.txt
│   └── demo2.txt
└── SRC
    ├── demo1.txt
    ├── demo2.txt
    └── demo3.txt

2 directories, 5 files
[root@localhost test]# 
  • 通过上述的测试,不加参数的 rsync 传输文件 和 cp 不加参数效果一样,目标目录的文件都是跟系统时间,时间属性相对于源文件时间属性有所改变。

2、-a,--archive:归档模式,递归传输和保存文件属性。

  • 步骤如下:
  • test 目录下创建一个 backups_a 用作目标目录。
  • test 目录下新建 SRC 目录用作装载源文件的目录。SRC 目录下新建三个文件,分别是 demo1.txt、demo2.txt 和 demo3.txt。
  • 目录结构如 tree 所示。
  • 列出 SRC 目录下的文件详细信息,留意源文件的时间。
  • 分别使用 rsync -a、rsync --archive 和 cp -p 把 SRC 目录下的源文件传输到 backups_a 目录。
  • 执行完上述命令后,用 tree 查看新的目录结构。
  • 查看 backups_a 目标目录接收的文件 对比 SRC 下的源文件属性是否一致。
## 第一阶段:
## test 目录下创建一个 backups_a 用作目标目录。
## test 目录下新建 SRC 目录用作装载源文件的目录。
## SRC 目录下新建三个文件,分别是 demo1.txt、demo2.txt 和 demo3.txt。
## 目录结构如 tree 所示。
## 列出 SRC 目录下的文件详细信息,留意源文件的时间。

[root@localhost test]# tree
.
├── backups_a
└── SRC
    ├── demo1.txt
    ├── demo2.txt
    └── demo3.txt

2 directories, 3 files
[root@localhost test]# ll SRC/
total 0
-rw-r--r--. 1 root root 0 Jul 23 11:11 demo1.txt
-rw-r--r--. 1 root root 0 Jul 23 11:11 demo2.txt
-rw-r--r--. 1 root root 0 Jul 23 11:11 demo3.txt
------------------------------------------------------------------

## 第二阶段:
## 分别使用 rsync -a、rsync --archive 和 cp -p 把 SRC 目录下的源文件传输到 backups_a 目录。

[root@localhost test]# 
[root@localhost test]# 
[root@localhost test]# rsync -a SRC/demo1.txt backups_a/
[root@localhost test]# 
[root@localhost test]# rsync --archive SRC/demo2.txt backups_a/
[root@localhost test]# 
[root@localhost test]# cp -p SRC/demo3.txt backups_a/
[root@localhost test]#
---------------------------------------------------------------------------

## 第三阶段:
## 执行完上述命令后,用 tree 查看新的目录结构。
## 查看 backups_a 目标目录接收的文件 对比 SRC 下的源文件属性是否一致。 

[root@localhost test]# tree
.
├── backups_a
│   ├── demo1.txt
│   ├── demo2.txt
│   └── demo3.txt
└── SRC
    ├── demo1.txt
    ├── demo2.txt
    └── demo3.txt

2 directories, 6 files
[root@localhost test]# 
[root@localhost test]# ll backups_a/
total 0
-rw-r--r--. 1 root root 0 Jul 23 11:11 demo1.txt
-rw-r--r--. 1 root root 0 Jul 23 11:11 demo2.txt
-rw-r--r--. 1 root root 0 Jul 23 11:11 demo3.txt
[root@localhost test]# 

通过上述的测试,rsync -a、rsync --archive 和 cp -p 效果一致,都可以原属性一起传输 或 复制到目标目录。


3、当源文件与目标文件大小、时间属性一致时,rsync -a / --archive 目标文件不会发生变化。

[root@localhost test]# tree    ##当前目录结构
.
├── backups_a
│   ├── demo1.txt
│   ├── demo2.txt
│   └── demo3.txt
└── SRC
    ├── demo1.txt
    ├── demo2.txt
    └── demo3.txt

2 directories, 6 files
[root@localhost test]# 
[root@localhost test]#
[root@localhost test]# ll backups_a/    ## backups_a 目标目录文件详情
total 0
-rw-r--r--. 1 root root 0 Jul 22 22:44 demo1.txt
-rw-r--r--. 1 root root 0 Jul 22 22:44 demo2.txt
-rw-r--r--. 1 root root 0 Jul 22 22:44 demo3.txt
[root@localhost test]# 
[root@localhost test]#
[root@localhost test]# ll SRC/    ## SRC 源目录文件详情
total 0
-rw-r--r--. 1 root root 0 Jul 22 22:44 demo1.txt
-rw-r--r--. 1 root root 0 Jul 22 22:44 demo2.txt
-rw-r--r--. 1 root root 0 Jul 22 22:44 demo3.txt
## 留意目标目录的文件 和 源目录的文件一模一样。
---------------------------------------------------------------------
## 此时执行 rsync -a / --archive 再从 SRC源目录传输一次文件到 backups_a目标目录
## 由于 rsync 检查发送方和接收方已有的文件大小、时间一致,所以不会更新 backups_a 的文件。
## SRC 目录和 backups_a 目录的文件仍然保持一致。
[root@localhost test]#
[root@localhost test]# rsync -a SRC/demo1.txt backups_a/
[root@localhost test]# 
[root@localhost test]# rsync --archive SRC/demo2.txt backups_a/
[root@localhost test]# 

## backups_a 目标目录文件详情,与源目录文件一样。
[root@localhost test]# ll backups_a/    
total 0
-rw-r--r--. 1 root root 0 Jul 22 22:44 demo1.txt
-rw-r--r--. 1 root root 0 Jul 22 22:44 demo2.txt
-rw-r--r--. 1 root root 0 Jul 22 22:44 demo3.txt
[root@localhost test]# 
[root@localhost test]# ll SRC/    ## SRC 源目录文件详情
total 0
-rw-r--r--. 1 root root 0 Jul 22 22:44 demo1.txt
-rw-r--r--. 1 root root 0 Jul 22 22:44 demo2.txt
-rw-r--r--. 1 root root 0 Jul 22 22:44 demo3.txt
[root@localhost test]# 
[root@localhost test]# tree    ## 目录结构也和原来一样
.
├── backups_a
│   ├── demo1.txt
│   ├── demo2.txt
│   └── demo3.txt
└── SRC
    ├── demo1.txt
    ├── demo2.txt
    └── demo3.txt

2 directories, 6 files
[root@localhost test]# 


4、源文件和目标文件大小、时间属性不一致,rsync -a / --archive 目标文件会更新为源文件。

  • 首先把 SRC 源目录下的文件 rsync -a 备份到 SRC_backups 目录。(没有 SRC_backups 目录会自动创建)
## SRC 源目录下的文件备份到 SRC_backups 目录
[root@localhost test]# rsync -a SRC/* SRC_backups    
[root@localhost test]# 
[root@localhost test]# tree    ## 目录结构
.
├── backups_a
│   ├── demo1.txt
│   ├── demo2.txt
│   └── demo3.txt
├── SRC
│   ├── demo1.txt
│   ├── demo2.txt
│   └── demo3.txt
└── SRC_backups
    ├── demo1.txt
    ├── demo2.txt
    └── demo3.txt

3 directories, 9 files
[root@localhost test]# ll SRC
total 0
-rw-r--r--. 1 root root 0 Jul 22 22:44 demo1.txt
-rw-r--r--. 1 root root 0 Jul 22 22:44 demo2.txt
-rw-r--r--. 1 root root 0 Jul 22 22:44 demo3.txt
[root@localhost test]# 
[root@localhost test]# ll SRC_backups/      ## SRC_backups下的文件和 SRC目录下的文件一致
total 0
-rw-r--r--. 1 root root 0 Jul 22 22:44 demo1.txt
-rw-r--r--. 1 root root 0 Jul 22 22:44 demo2.txt
-rw-r--r--. 1 root root 0 Jul 22 22:44 demo3.txt
[root@localhost test]# 

  • vim 编辑 SRC/demo1.txt 文件。输入demo1.txt ,Esc 输入 :wq! 保存退出。SRC/demo1.txt 文件的大小和时间会有所改变,修改时间会更改为文件保存的时间。
[root@localhost test]# 
[root@localhost test]# vim SRC/demo1.txt 
demo1.txt                                                                                         
~                                                                                                             
~                                                                                                             
:wq!

[root@localhost test]# 
[root@localhost test]# ll SRC
total 4
-rw-r--r--. 1 root root 10 Jul 29 00:50 demo1.txt    ## 大小变为 10,时间更新为 Jul 29 00:50
-rw-r--r--. 1 root root  0 Jul 22 22:44 demo2.txt
-rw-r--r--. 1 root root  0 Jul 22 22:44 demo3.txt

  • 查看 backups_a 备份目录的 demo1.txt 详情(留意大小和时间)。然后, rsync -a SRC 目录下的 demo1.txt 到 backups_a/ 目录。rsync 检查发送方和接收方已有的文件大小、时间不一致,所以,把发送方 SRC 的 demo1.txt 文件传输到 backups_a/ 目录覆盖旧的 demo1.txt 文件。
[root@localhost test]# 
[root@localhost test]# ll backups_a/
total 0

 ## 原 demo1.txt 大小为 0,时间为 Jul 22 22:44
-rw-r--r--. 1 root root 0 Jul 22 22:44 demo1.txt   
-rw-r--r--. 1 root root 0 Jul 22 22:44 demo2.txt
-rw-r--r--. 1 root root 0 Jul 22 22:44 demo3.txt
[root@localhost test]# 

## 把 SRC 目录下添加内容的 demo1.txt 全属性传输到 backups_a 目录下
[root@localhost test]# rsync -a SRC/demo1.txt backups_a/
[root@localhost test]# 
[root@localhost test]# ll backups_a/    
total 4

## 新的 demo1.txt 大小为 10,时间为Jul 29 00:50
-rw-r--r--. 1 root root 10 Jul 29 00:50 demo1.txt    
-rw-r--r--. 1 root root  0 Jul 22 22:44 demo2.txt
-rw-r--r--. 1 root root  0 Jul 22 22:44 demo3.txt
[root@localhost test]# 
                   

5、源文件与目标文件大小、时间属性一致。但,权限属性不一致。rsync -a / --archive 目标文件也会更新为源文件一致的权限属性。文件大小、时间属性不会改变。

  • 首先,查看目标目录 backups_a 和 源目录 SRC 的 demo1.txt 文件信息是一致的(大小、时间)。
## backups_a 的 demo1.txt 和 SRC 的 demo1.txt 信息一致。

[root@localhost test]# ll backups_a/    
total 4
-rw-r--r--. 1 root root 10 Jul 29 00:50 demo1.txt
-rw-r--r--. 1 root root  0 Jul 22 22:44 demo2.txt
-rw-r--r--. 1 root root  0 Jul 22 22:44 demo3.txt
[root@localhost test]# 
[root@localhost test]# ll SRC
total 4
-rw-r--r--. 1 root root 10 Jul 29 00:50 demo1.txt
-rw-r--r--. 1 root root  0 Jul 22 22:44 demo2.txt
-rw-r--r--. 1 root root  0 Jul 22 22:44 demo3.txt
[root@localhost test]# 
  • 修改 SRC/demo1.txt 文件权限为所属组添加写的权限 g+w,其他人添加写的权限 o+w。更改权限不会改变文件的大小和时间。
[root@localhost test]# chmod g+w,o+w SRC/demo1.txt     ## 添加权限 g+w,o+w
[root@localhost test]# 
[root@localhost test]# ll SRC
total 4
-rw-rw-rw-. 1 root root 10 Jul 29 00:50 demo1.txt    ## 权限已经修改 -rw-rw-rw-
-rw-r--r--. 1 root root  0 Jul 22 22:44 demo2.txt
-rw-r--r--. 1 root root  0 Jul 22 22:44 demo3.txt
[root@localhost test]# 
  • rsync -a 把修改了权限的 SRC/demo1.txt 文件传输到 backups_a。backups_a 目录的 demo1.txt 权限会更新。
[root@localhost test]# rsync -a SRC/demo1.txt backups_a/
[root@localhost test]# 
[root@localhost test]# ll backups_a/
total 4
-rw-rw-rw-. 1 root root 10 Jul 29 00:50 demo1.txt    ## 权限已经更新 -rw-rw-rw-
-rw-r--r--. 1 root root  0 Jul 22 22:44 demo2.txt
-rw-r--r--. 1 root root  0 Jul 22 22:44 demo3.txt
[root@localhost test]# 

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 218,204评论 6 506
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,091评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 164,548评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,657评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,689评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,554评论 1 305
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,302评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,216评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,661评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,851评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,977评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,697评论 5 347
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,306评论 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,898评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,019评论 1 270
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,138评论 3 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,927评论 2 355

推荐阅读更多精彩内容