rsync之include、exclude使用

rsync之include、exclude使用

注意:exclude可单独使用,include必须和exclude配合使用 

环境:

服务端:

rsync之include、exclude使用_第1张图片

在做同步之前必须要知道的含义:

  • --exclude='*'   排除所有文件,包括目录,因为在linux一切皆文件,代表排除所有
  • --exclude='*/'   排除所有目录
  • --include='*/'    包含所有目录
  • --include='*'     包含所有文件,包括目录,因为在linux一切皆文件,代表包含所有

 需求1:

不同步目录

$ rsync  -e 'ssh -p 24858' -avz --progress   --exclude='*/' --prune-empty-dirs ./ [email protected]:/home/kk/test/
building file list ... 
4 files to consider
./
a.txt
              2 100%    0.00kB/s    0:00:00 (xfr#1, to-chk=2/4)
b.txt
              2 100%    1.95kB/s    0:00:00 (xfr#2, to-chk=1/4)
c.txt
              2 100%    1.95kB/s    0:00:00 (xfr#3, to-chk=0/4)

sent 262 bytes  received 76 bytes  676.00 bytes/sec
total size is 6  speedup is 0.02

rsync之include、exclude使用_第2张图片

需求2:

仅同步a目录

$ rsync  -e 'ssh -p 24858' -avz --progress  --include='*/' 'a' --exclude='*' --prune-empty-dirs ./ [email protected]:/home/kk/test/
building file list ... 
10 files to consider
a/
a/b/
a/b/c/

sent 235 bytes  received 21 bytes  512.00 bytes/sec
total size is 0  speedup is 0.00

需求3:

仅同步a目录下的a.txt文件

$ cd /home/kk/test
$ rsync  -e 'ssh -p 24858' -avz --progress  --include='*/' --include='a/a.txt'  --exclude='*' --prune-empty-dirs ./ [email protected]:/home/kk/test/
building file list ... 
8 files to consider
./
a/
a/a.txt
              2 100%    0.00kB/s    0:00:00 (xfr#1, to-chk=5/8)

sent 266 bytes  received 41 bytes  614.00 bytes/sec
total size is 2  speedup is 0.01

需求4:

同步所有的a.txt文件(这是在目标目录没有该文件的情况下,会全部同步,如果某个目录下有了该文件,则不会替换)

$ rsync  -e 'ssh -p 24858' -avz --progress  --include='*/' --include='a.txt'  --exclude='*' --prune-empty-dirs ./ [email protected]:/home/kk/test/
building file list ... 
9 files to consider
./
a.txt
              2 100%    0.00kB/s    0:00:00 (xfr#1, to-chk=7/9)
a/
a/a.txt
              2 100%    1.95kB/s    0:00:00 (xfr#2, to-chk=5/9)

sent 334 bytes  received 60 bytes  788.00 bytes/sec
total size is 4  speedup is 0.01

### 附加(可以结合jenkins的changelog插件去做同步,因为我这个需求比较奇葩,买的第三方公司产品,然后那边一直有版本更新,我们公司只需要改一些前端样式,所所以需要jenkins的changelog插件去获取变更记录,然后只更新变更记录的文件)

当然,如果要同步文件过多,还可以结合  --include-from 参数使用,将所有要通的文件路径写入到一个文件中

eg:

$ cat rsync.txt 
a/a.txt
b/b.txt
c/c.txt


$ rsync  -e 'ssh -p 24858' -avz --progress   --include='*/' --include-from='./rsync.txt' --exclude='*' --prune-empty-dirs ./ [email protected]:/home/kk/test/
building file list ... 
10 files to consider
./
a/
a/a.txt
              2 100%    0.00kB/s    0:00:00 (xfr#1, to-chk=7/10)
b/
b/b.txt
              2 100%    1.95kB/s    0:00:00 (xfr#2, to-chk=3/10)
c/
c/c.txt
              2 100%    1.95kB/s    0:00:00 (xfr#3, to-chk=1/10)

sent 429 bytes  received 85 bytes  1,028.00 bytes/sec
total size is 6  speedup is 0.01

rsync之include、exclude使用_第3张图片

 好了,这就是rsync之include、exclude使用的方法了,如有问题可与博主一起交流讨论!

你可能感兴趣的:(linux,rsync)