一、前提
源目录 /lvmdata/testsrc
,存在两个文件
[root@VM_1_6_centos testsrc]# ll
total 0
-rw-r--r-- 1 root root 0 Aug 31 17:27 file_1
-rw-r--r-- 1 root root 0 Aug 31 17:32 file_2
目标目录 /lvmdata/testdst
,空
目标目录 /nfsdata/test
,空。NFS挂载目录,all_squash
配置,所有的文件ower属性会被设置为nfsnobody
二、rsync基础操作
- 列出目录信息
rsync xxx
仅列出目录自身信息
rsync xxx/
列出目录下文件信息
[root@VM_1_6_centos testdst]# rsync /lvmdata/testsrc
drwxr-xr-x 34 2021/08/31 17:32:42 testsrc
[root@VM_1_6_centos testdst]# rsync /lvmdata/testsrc/
drwxr-xr-x 34 2021/08/31 17:32:42 .
-rw-r--r-- 0 2021/08/31 17:27:44 file_1
-rw-r--r-- 0 2021/08/31 17:32:42 file_2
- 同步文件
- 同步到 lvm目录
/lvmdata/testdst
[root@VM_1_6_centos testdst]# rsync -av /lvmdata/testsrc/ /lvmdata/testdst/
sending incremental file list
./
file_1
file_2
sent 137 bytes received 53 bytes 380.00 bytes/sec
total size is 0 speedup is 0.00
[root@VM_1_6_centos testdst]# ll /lvmdata/testdst/
total 0
-rw-r--r-- 1 root root 0 Aug 31 17:27 file_1
-rw-r--r-- 1 root root 0 Aug 31 17:32 file_2
- 同步到nfs目录
/nfsdata/test
ownership会变,下次再同步的时候会判断为文件不一致,全部重新同步。
[root@VM_1_6_centos testdst]# rsync -av /lvmdata/testsrc/ /nfsdata/test/
sending incremental file list
./
rsync: chown "/nfsdata/test/." failed: Operation not permitted (1)
file_1
file_2
rsync: chown "/nfsdata/test/.file_1.k8yvyI" failed: Operation not permitted (1)
rsync: chown "/nfsdata/test/.file_2.msX3xg" failed: Operation not permitted (1)
sent 137 bytes received 53 bytes 380.00 bytes/sec
total size is 0 speedup is 0.00
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1052) [sender=3.0.9]
[root@VM_1_6_centos testdst]# ll /nfsdata/test/
total 0
-rw------- 1 nfsnobody nfsnobody 0 Aug 31 17:44 file_1
-rw------- 1 nfsnobody nfsnobody 0 Aug 31 17:44 file_2
- 以
--delete
同步,删除不存在的文件
删除file_1
失败范例
[root@VM_1_6_centos testdst]# rsync -av /lvmdata/testsrc/ /lvmdata/testdst/
sending incremental file list
./
sent 48 bytes received 15 bytes 126.00 bytes/sec
total size is 0 speedup is 0.00
[root@VM_1_6_centos testdst]# ll /lvmdata/testdst/
total 0
-rw-r--r-- 1 root root 0 Aug 31 17:27 file_1
-rw-r--r-- 1 root root 0 Aug 31 17:32 file_2
成功范例
[root@VM_1_6_centos testdst]# rsync -av /lvmdata/testsrc/ /lvmdata/testdst/ --delete
sending incremental file list
deleting file_1
sent 45 bytes received 12 bytes 114.00 bytes/sec
total size is 0 speedup is 0.00
[root@VM_1_6_centos testdst]#
[root@VM_1_6_centos testdst]# ll /lvmdata/testdst/
total 0
-rw-r--r-- 1 root root 0 Aug 31 17:32 file_2
- 以
--no
同步,不带属性
通过该参数,才能实现nfs的差异同步
[root@VM_1_6_centos test]# rsync -av --no-perms --no-owner --no-group /lvmdata/testsrc/ /nfsdata/test/
sending incremental file list
./
file_2
file_3
sent 135 bytes received 53 bytes 376.00 bytes/sec
total size is 0 speedup is 0.00
[root@VM_1_6_centos test]#
[root@VM_1_6_centos test]#
[root@VM_1_6_centos test]# ll /nfsdata/test/
total 0
-rw-r--r-- 1 nfsnobody nfsnobody 0 Aug 31 17:32 file_2
-rw-r--r-- 1 nfsnobody nfsnobody 0 Aug 31 17:54 file_3
三、通过-n
、-vv
实现模拟同步
适用于需要查看同步效果,但是不能真实同步的场景,比如数PB文件的同步。
源目录/lvmdata/testsrc
[root@VM_1_6_centos testsrc]# ll
total 0
-rw-r--r-- 1 root root 0 Sep 1 14:36 file_1
-rw-r--r-- 1 root root 0 Aug 31 17:32 file_2
-rw-r--r-- 1 root root 0 Aug 31 17:54 file_3
同步到/lvmdata/testdst
rsync -av --delete /lvmdata/testsrc/ /lvmdata/testdst/
- 增加1个文件 file_4
uptodata :最新的
command | grep -v 'uptodate'
可以过滤出当前要更新的文件信息
[root@VM_1_6_centos testsrc]# rsync -a -n -vv --delete /lvmdata/testsrc/ /lvmdata/testdst/
sending incremental file list
delta-transmission disabled for local transfer or --whole-file
./
file_1 is uptodate
file_2 is uptodate
file_3 is uptodate
file_4
total: matches=0 hash_hits=0 false_alarms=0 data=0
sent 93 bytes received 27 bytes 240.00 bytes/sec
total size is 0 speedup is 0.00 (DRY RUN)
- 删除1个文件 file_1
如下deleting file_1
表示要删除文件 file_1 ,file_4
代表新增文件 file_4。
[root@VM_1_6_centos testsrc]# rsync -a -n -vv --delete /lvmdata/testsrc/ /lvmdata/testdst/
sending incremental file list
delta-transmission disabled for local transfer or --whole-file
./
deleting file_1
file_2 is uptodate
file_3 is uptodate
file_4
total: matches=0 hash_hits=0 false_alarms=0 data=0
sent 79 bytes received 24 bytes 206.00 bytes/sec
total size is 0 speedup is 0.00 (DRY RUN)
- nas存储,
--no
参数的意义
- 不加
--no
可以看到,需要同步文件 file_2,file_3,file_4。其实,file_2,file_3是前面同步的结果,只是存储到nas存储后,文件的owner属性变更为 nfsnobody 了。
[root@VM_1_6_centos testsrc]# rsync -a -n -vv --delete /lvmdata/testsrc/ /nfsdata/test/
sending incremental file list
delta-transmission disabled for local transfer or --whole-file
./
file_2
file_3
file_4
total: matches=0 hash_hits=0 false_alarms=0 data=0
sent 79 bytes received 24 bytes 206.00 bytes/sec
total size is 0 speedup is 0.00 (DRY RUN)
- 加
--no
不检测权限、owner、group,结果如吾所愿,只需要更新file_4。
[root@VM_1_6_centos testsrc]# rsync -a -n -vv --delete --no-perms --no-owner --no-group /lvmdata/testsrc/ /nfsdata/test/
sending incremental file list
delta-transmission disabled for local transfer or --whole-file
./
file_2 is uptodate
file_3 is uptodate
file_4
total: matches=0 hash_hits=0 false_alarms=0 data=0
sent 77 bytes received 24 bytes 202.00 bytes/sec
total size is 0 speedup is 0.00 (DRY RUN)
网友评论