Linux学习学习之Linux

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

2021-07-31  本文已影响0人  捌千里路雲和月

1、rsync -a -b / --backup 备份文件,如目标目录没有同名文件则会创建一个备份文件,新创建的备份文件不会带 ~ 符号。

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

2 directories, 3 files
[root@localhost test]# 
[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]# 
[root@localhost test]# rsync -a -b SRC/demo1.txt backups_ab/
[root@localhost test]# 
[root@localhost test]# rsync -a --backup SRC/demo2.txt backups_ab/
[root@localhost test]# 
[root@localhost test]# tree
.
├── backups_ab
│   ├── demo1.txt
│   └── demo2.txt
└── SRC
    ├── demo1.txt
    ├── demo2.txt
    └── demo3.txt

2 directories, 5 files

[root@localhost test]# ll backups_ab/
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
[root@localhost test]# 
[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 backups_ab/
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
# 编辑 SRC 下的 demo1.txt,输入内容,然后 Esc 输入 :wq! 保存文件
[root@localhost test]# vim SRC/demo1.txt     #

hello demo1
~                                                                                                             
~                                                                                                                                                                                                                         
:wq!     

# 同样方法编辑 SRC 下的 demo2.txt
[root@localhost test]# vim SRC/demo2.txt 

hello demo2
~                                                                                                             
~                                                                                                                                                                                                                    
:wq!        
                                   
[root@localhost test]# ll SRC/
total 8
-rw-r--r--. 1 root root 12 Jul 30 00:03 demo1.txt    ## 更新了文件大小和时间
-rw-r--r--. 1 root root 12 Jul 30 00:07 demo2.txt    ## 更新了文件大小和时间
-rw-r--r--. 1 root root  0 Jul 22 22:44 demo3.txt   
[root@localhost test]# rsync -a -b SRC/demo1.txt backups_ab/
[root@localhost test]# 
[root@localhost test]# rsync -a --backup SRC/demo2.txt backups_ab/
[root@localhost test]# 
[root@localhost test]# tree
.
├── backups_ab
│   ├── demo1.txt    ##新文件
│   ├── demo1.txt~    ##旧文件
│   ├── demo2.txt    ##新文件
│   └── demo2.txt~    ##旧文件
└── SRC
    ├── demo1.txt
    ├── demo2.txt
    └── demo3.txt

2 directories, 7 files
[root@localhost test]# 
[root@localhost test]# ll backups_ab/
total 16
-rw-r--r--. 1 root root 12 Jul 30 00:03 demo1.txt    ##新文件
-rw-r--r--. 1 root root 0 Jul 22 22:44 demo1.txt~     ##旧文件
-rw-r--r--. 1 root root 12 Jul 30 00:07 demo2.txt    ##新文件
-rw-r--r--. 1 root root 0 Jul 22 22:44 demo2.txt~     ##旧文件
[root@localhost test]# 
[root@localhost test]# ll SRC/
total 8
-rw-r--r--. 1 root root 12 Jul 30 00:03 demo1.txt
-rw-r--r--. 1 root root 12 Jul 30 00:07 demo2.txt
-rw-r--r--. 1 root root  0 Jul 22 22:44 demo3.txt
[root@localhost test]# 


2、如果,源文件没有改动,与目标目录文件同名、大小、修改时间一致,则不进行备份操作。

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

2 directories, 7 files
[root@localhost test]# 
[root@localhost test]# rsync -a -b SRC/demo3.txt backups_ab/
[root@localhost test]# 
[root@localhost test]# ll backups_ab/
total 16
-rw-r--r--. 1 root root 12 Jul 30 00:03 demo1.txt
-rw-r--r--. 1 root root 0 Jul 22 22:44 demo1.txt~  
-rw-r--r--. 1 root root 12 Jul 30 00:07 demo2.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    ## 备份文件 demo3.txt
[root@localhost test]# 
[root@localhost test]# ll SRC/
total 8
-rw-r--r--. 1 root root 12 Jul 30 00:03 demo1.txt
-rw-r--r--. 1 root root 12 Jul 30 00:07 demo2.txt
-rw-r--r--. 1 root root  0 Jul 22 22:44 demo3.txt
[root@localhost test]# 

## 再执行一次 rsync -a -b,把 SRC 目录下的 demo3.txt 备份到 backups_ab/ 目录下。
[root@localhost test]# rsync -a -b SRC/demo3.txt backups_ab/
[root@localhost test]# 
[root@localhost test]# ll backups_ab/
total 16
-rw-r--r--. 1 root root 12 Jul 30 00:03 demo1.txt
-rw-r--r--. 1 root root 0 Jul 22 22:44 demo1.txt~  
-rw-r--r--. 1 root root 12 Jul 30 00:07 demo2.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    ## 还是原来第一次备份的 demo3.txt
[root@localhost test]# 
[root@localhost test]# tree
.
├── backups_ab    ## 没有新增带 ~ 符号的demo3.txt 文件
│   ├── demo1.txt
│   ├── demo1.txt~
│   ├── demo2.txt
│   ├── demo2.txt~
│   └── demo3.txt
└── SRC
    ├── demo1.txt
    ├── demo2.txt
    └── demo3.txt

2 directories, 8 files


3、源文件只修改权限属性,文件大小和时间不改变的情况下 rsync -a -b / --backup传输文件到目标目录,如果目标目录已存在以前备份的同名文件则不会生成新的文件,但会更新目标目录文件的权限属性。

[root@localhost test]# 
[root@localhost test]# ll SRC/
total 8
-rw-r--r--. 1 root root 12 Jul 30 00:03 demo1.txt
-rw-r--r--. 1 root root 12 Jul 30 00:07 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 backups_ab/
total 16
-rw-r--r--. 1 root root 12 Jul 30 00:03 demo1.txt
-rw-r--r--. 1 root root 0 Jul 22 22:44 demo1.txt~  
-rw-r--r--. 1 root root 12 Jul 30 00:07 demo2.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

## demo3.txt 所属组和其他人权限增加可写的权限,权限修改为 -rw-rw-rw-。
[root@localhost test]# chmod g+w,o+w SRC/demo3.txt   
[root@localhost test]# 
[root@localhost test]# ll SRC/
total 8
-rw-r--r--. 1 root root 12 Jul 30 00:03 demo1.txt
-rw-r--r--. 1 root root 12 Jul 30 00:07 demo2.txt
-rw-rw-rw-. 1 root root  0 Jul 22 22:44 demo3.txt    ## 权限修改成功
[root@localhost test]# 

[root@localhost test]# rsync -a -b SRC/demo3.txt backups_ab/
[root@localhost test]# 
[root@localhost test]# tree
.
├── backups_ab
│   ├── demo1.txt
│   ├── demo1.txt~
│   ├── demo2.txt
│   ├── demo2.txt~
│   └── demo3.txt
└── SRC
    ├── demo1.txt
    ├── demo2.txt
    └── demo3.txt

2 directories, 8 files
[root@localhost test]# ll backups_ab/
total 16
-rw-r--r--. 1 root root 12 Jul 30 00:03 demo1.txt
-rw-r--r--. 1 root root 0 Jul 22 22:44 demo1.txt~  
-rw-r--r--. 1 root root 12 Jul 30 00:07 demo2.txt
-rw-r--r--. 1 root root 0 Jul 22 22:44 demo2.txt~
-rw-rw-rw-. 1 root root  0 Jul 22 22:44 demo3.txt    ## backups_ab目录的demo3.txt 权限已更新为 -rw-rw-rw-
[root@localhost test]# 

上一篇 下一篇

猜你喜欢

热点阅读