4-10.7 Linux 中的文件同步传输 --- rsync

2021-08-13  本文已影响0人  捌千里路雲和月
[root@localhost ~]# ls
test
[root@localhost ~]# cd test
[root@localhost test]# 
[root@localhost test]# mkdir backups_r
[root@localhost test]# 
[root@localhost test]# touch SRC/file1.txt
[root@localhost test]# 
[root@localhost test]# mkdir SRC/directory
[root@localhost test]# 
[root@localhost test]# touch SRC/directory/file2.txt
[root@localhost test]# 
[root@localhost test]# tree
.
├── backups_r
└── SRC
    ├── directory
    │   └── file2.txt
    └── file1.txt

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

2、SRC 目录下有子目录,rsync 和 rsync -r 传输 SRC/ 对比。

[root@localhost test]# rsync SRC/ backups_r/
skipping directory .    ## 没有 -r会跳过目录传输,因为纯 rsync只能传输文件 
[root@localhost test]# 
[root@localhost test]# tree
.
├── backups_r    ## backups_r目录没有任何文件。
└── SRC
    ├── directory
    │   └── file2.txt
    └── file1.txt

3 directories, 2 files

## -r 可以传输目录,并且递归传输目录下的所有内容
[root@localhost test]# rsync -r SRC/ backups_r/   
[root@localhost test]# 
[root@localhost test]# tree
.
├── backups_r    ## SRC目录下的所有内容都传输到 backups_r
│   ├── directory
│   │   └── file2.txt
│   └── file1.txt
└── SRC
    ├── directory
    │   └── file2.txt
    └── file1.txt

4 directories, 4 files
[root@localhost test]# 
[root@localhost test]# ll SRC/    
total 0
drwxr-xr-x. 2 root root 23 Aug 12 22:22 directory
-rw-r--r--. 1 root root  0 Aug 12 22:20 file1.txt
[root@localhost test]# 
[root@localhost test]# ll backups_r/    ## 除了时间以外,其余属性和 SRC源目录一致
total 0
drwxr-xr-x. 2 root root 23 Aug 12 23:23 directory
-rw-r--r--. 1 root root  0 Aug 12 23:23 file1.txt
[root@localhost test]# 

[root@localhost test]# rsync -a SRC/ backups_r/
[root@localhost test]# 
[root@localhost test]# tree
.
├── backups_r
│   ├── directory
│   │   └── file2.txt
│   └── file1.txt
└── SRC
    ├── directory
    │   └── file2.txt
    └── file1.txt

4 directories, 4 files
[root@localhost test]# 
[root@localhost test]# ll SRC/
total 0
drwxr-xr-x. 2 root root 23 Aug 12 22:22 directory
-rw-r--r--. 1 root root  0 Aug 12 22:20 file1.txt
[root@localhost test]# 
[root@localhost test]# ll backups_r/    ## 属性和 SRC源目录一致
total 0
drwxr-xr-x. 2 root root 23 Aug 12 22:22 directory
-rw-r--r--. 1 root root  0 Aug 12 22:20 file1.txt
[root@localhost test]# 

3、rsync 和 rsync -r 传输 SRC 目录不加 / 对比

[root@localhost test]# ls
backups_r  SRC
[root@localhost test]# tree
.
├── backups_r
│   ├── directory
│   │   └── file2.txt
│   └── file1.txt
└── SRC
    ├── directory
    │   └── file2.txt
    └── file1.txt

4 directories, 4 files
[root@localhost test]# rm -rf backups_r/*    ## 删除 backups_r目录下所有内容
[root@localhost test]# 
[root@localhost test]# tree
.
├── backups_r    ## backups_r已清空
└── SRC
    ├── directory
    │   └── file2.txt
    └── file1.txt

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

[root@localhost test]# rsync SRC backups_r/
skipping directory SRC    ## 没有 -r会跳过 SRC 目录
[root@localhost test]# 
[root@localhost test]# tree
.
├── backups_r
└── SRC
    ├── directory
    │   └── file2.txt
    └── file1.txt

3 directories, 2 files

## -r 可以把整个 SRC 目录传输到 backups_r下
[root@localhost test]# rsync -r SRC backups_r/
[root@localhost test]# 
[root@localhost test]# tree
.
├── backups_r  ## 整个 SRC 目录传输到 backups_r
│   └── SRC
│       ├── directory
│       │   └── file2.txt
│       └── file1.txt
└── SRC
    ├── directory
    │   └── file2.txt
    └── file1.txt

5 directories, 4 files
[root@localhost test]# 
[root@localhost test]# 
[root@localhost test]# ll SRC/
total 0
drwxr-xr-x. 2 root root 23 Aug 12 22:22 directory
-rw-r--r--. 1 root root  0 Aug 12 22:20 file1.txt
[root@localhost test]# 
[root@localhost test]# ll backups_r/SRC/
total 0
drwxr-xr-x. 2 root root 23 Aug 12 23:53 directory
-rw-r--r--. 1 root root  0 Aug 12 23:53 file1.txt
[root@localhost test]# 

[root@localhost test]# 
[root@localhost test]# rsync -a SRC backups_r/  
[root@localhost test]# 
[root@localhost test]# tree
.
├── backups_r
│   └── SRC
│       ├── directory
│       │   └── file2.txt
│       └── file1.txt
└── SRC
    ├── directory
    │   └── file2.txt
    └── file1.txt

5 directories, 4 files
[root@localhost test]# ll SRC/
total 0
drwxr-xr-x. 2 root root 23 Aug 12 22:22 directory
-rw-r--r--. 1 root root  0 Aug 12 22:20 file1.txt
[root@localhost test]# 
[root@localhost test]# ll backups_r/SRC/    ## rsync -a 同步了原 SRC的属性(时间)
total 0
drwxr-xr-x. 2 root root 23 Aug 12 22:22 directory
-rw-r--r--. 1 root root  0 Aug 12 22:20 file1.txt
[root@localhost test]# 

4、SRC 目录下有子目录,rsync 和 rsync -r 传输 SRC/* 对比。

[root@localhost test]# rm -rf backups_r/*
[root@localhost test]# 
[root@localhost test]# tree
.
├── backups_r
└── SRC
    ├── directory
    │   └── file2.txt
    └── file1.txt

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

[root@localhost test]# rsync SRC/* backups_r/
skipping directory directory    ## 没有 -r 传输 SRC 下所有内容,
                                ## 可以很明显看出只传输文件,跳过目录。
[root@localhost test]# 
[root@localhost test]# tree
.
├── backups_r
│   └── file1.txt    ## 只成功传输了文件
└── SRC
    ├── directory    ## 被跳过的 directory 目录
    │   └── file2.txt
    └── file1.txt

3 directories, 3 files
[root@localhost test]# 
[root@localhost test]# rm -rf backups_r/*
[root@localhost test]# 
[root@localhost test]# tree
.
├── backups_r
└── SRC
    ├── directory
    │   └── file2.txt
    └── file1.txt

3 directories, 2 files
[root@localhost test]# rsync -r SRC/* backups_r/
[root@localhost test]# 
[root@localhost test]# tree
.
├── backups_r    ## -r 可以把 SRC 目录下的所有内容递归传输到 backups_r 
│   ├── directory
│   │   └── file2.txt
│   └── file1.txt
└── SRC
    ├── directory
    │   └── file2.txt
    └── file1.txt

4 directories, 4 files
[root@localhost test]# 

[root@localhost test]# 
[root@localhost test]# ll SRC/
total 0
drwxr-xr-x. 2 root root 23 Aug 12 22:22 directory
-rw-r--r--. 1 root root  0 Aug 12 22:20 file1.txt
[root@localhost test]# 
[root@localhost test]# ll backups_r/    ## 用 -r 参数传输过来的时间
total 0
drwxr-xr-x. 2 root root 23 Aug 13 00:11 directory
-rw-r--r--. 1 root root  0 Aug 13 00:11 file1.txt

## -a 传输 SRC 所有文件到 backups_r 目录下
[root@localhost test]# rsync -a SRC/* backups_r/    
[root@localhost test]# 
[root@localhost test]# ll backups_r/    ## 用 -a 参数传输 SRC源的时间
total 0
drwxr-xr-x. 2 root root 23 Aug 12 22:22 directory
-rw-r--r--. 1 root root  0 Aug 12 22:20 file1.txt
[root@localhost test]# 

[root@localhost test]# rm -rf backups_r/*
[root@localhost test]# 
[root@localhost test]# tree
.
├── backups_r
└── SRC
    ├── directory
    │   └── file2.txt
    └── file1.txt

3 directories, 2 files
[root@localhost test]# 
[root@localhost test]# rsync --recursive SRC backups_r/
[root@localhost test]# 
[root@localhost test]# tree
.
├── backups_r    ## --recursive 和 -r 一样,整个 SRC 目录传输到 backups_r
│   └── SRC
│       ├── directory
│       │   └── file2.txt
│       └── file1.txt
└── SRC
    ├── directory
    │   └── file2.txt
    └── file1.txt

5 directories, 4 files
[root@localhost test]# 
[root@localhost test]# rm -rf backups_r/*
[root@localhost test]# 
[root@localhost test]# tree
.
├── backups_r
└── SRC
    ├── directory
    │   └── file2.txt
    └── file1.txt

3 directories, 2 files
[root@localhost test]# rsync --recursive SRC/ backups_r/

 ## --recursive 和 -r 一样,SRC目录下的所有内容都传输到 backups_r
[root@localhost test]# tree   
.
├── backups_r
│   ├── directory
│   │   └── file2.txt
│   └── file1.txt
└── SRC
    ├── directory
    │   └── file2.txt
    └── file1.txt

4 directories, 4 files
[root@localhost test]# 

上一篇 下一篇

猜你喜欢

热点阅读