美文网首页
rsync 同步文件

rsync 同步文件

作者: 偷油考拉 | 来源:发表于2021-09-01 14:55 被阅读0次

一、前提

源目录 /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基础操作

  1. 列出目录信息
    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

  1. 同步文件
  • 同步到 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

  1. --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

  1. --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. 增加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. 删除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)
  1. 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)

相关文章

  • rsync通过服务同步、Linux系统日志、screen

    rsync通过服务同步 Linux文件同步工具-rsync rsync通过服务同步 Linux文件同步工具-rsy...

  • rsync工具介绍、rsync常用选项、rsync通过ssh同步

    rsync工具介绍 Linux文件同步工具-rsync 安装rsync使用rsync同步到本机同步到其它机器 rs...

  • uBuntu Rsync文件同步服务

    安装Rsync文件同步服务 uBuntu系统默认安装Rsync服务,查看帮助文档: 本地文件夹同步命令: 同步到远...

  • 使用inotify 监控文件变化

    文件监控可以配合rsync实现文件自动同步,例如监听某个目录,当文件变化时,使用rsync命令将变化的文件同步。(...

  • Rsync同步

    Rsync在linux端 rsync 同步当前目录 加名字同步该名字文件夹

  • rsync使用简介

    介绍 rsync是Linux下用于远程同步文件的工具,全称remote sync。rsync可以同步整个目录,同步...

  • Rsync+inotify的文件同步部署之介绍

    Rsync远程文件同步工具 它在同步文件的同时,可以保持原来文件的权限、时间、软硬链接等附加信息。 rsync...

  • rsync

    rsync rsync命令是一个远程数据同步工具,可通过LAN/WAN快速同步多台主机间的文件。rsync使用所谓...

  • 2018-11-05

    10月31日任务 10.28rsync工具介绍 Linux文件同步工具 rsync rsync -av /etc/...

  • rsync配置

    rsync 做文件同步 或者 svn 同步后 钩子钩到服务器上的工具 rsync 安装 (centos) yum...

网友评论

      本文标题:rsync 同步文件

      本文链接:https://www.haomeiwen.com/subject/qvjziltx.html