AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / ubuntu / 问题 / 970573
Accepted
SPooKYiNeSS
SPooKYiNeSS
Asked: 2017-10-30 05:59:18 +0800 CST2017-10-30 05:59:18 +0800 CST 2017-10-30 05:59:18 +0800 CST

rsync 究竟如何决定同步什么?

  • 772

我找到了这个问题的多个答案,所以想问问实际使用它的人,而不是只想通过填写随机的半无用信息来制作最大的博客。

场景:我 rsync -av --progress /dir/a /dir/b 和它做它的事。

我将新文件添加到 /dir/a 并再次运行相同的命令,它知道它做了什么并且只复制新文件。

我将新文件添加到 /dir/a 并重命名 /dir/b 中的一些文件,并且可能还会删除一些文件。

如果我rsync -av --progress /dir/a /dir/b再次运行,将复制什么?只是新文件,因为它知道它以前复制的内容,或者也被重命名/删除的文件,因为它们不再存在。

作为奖励,如果再次复制先前复制的文件,有没有办法防止这种情况发生,以便只复制 /dir/a 的新增内容?

目前我很乐意手动检查,但随着数据变得越来越大,我将需要更多的自动化来执行这项任务。

rsync
  • 3 3 个回答
  • 11119 Views

3 个回答

  • Voted
  1. Best Answer
    muru
    2017-10-30T06:40:14+08:002017-10-30T06:40:14+08:00

    我将新文件添加到 /dir/a 并再次运行相同的命令,它知道它做了什么并且只复制新文件。

    不,它不知道它在之前的运行中做了什么。它将接收端的数据与要发送的数据进行比较。对于足够小的数据,这不会很明显,但是当您有足够大的目录时,很容易感觉到在复制实际开始之前比较所花费的时间。

    默认检查文件修改时间和大小。来自man rsync:

    -c, --checksum
          This changes the way rsync checks if the files have been changed
          and  are in need of a transfer.  Without this option, rsync uses
          a "quick check" that (by default) checks if each file’s size and
          time of last modification match between the sender and receiver.
          This option changes this to compare a 128-bit checksum for  each
          file  that  has a matching size.  Generating the checksums means
          that both sides will expend a lot of disk I/O  reading  all  the
          data  in  the  files  in  the transfer (and this is prior to any
          reading that will be done to transfer changed  files),  so  this
          can slow things down significantly.
    

    和:

    -u, --update
          This  forces  rsync  to  skip  any  files  which  exist  on  the
          destination  and  have  a  modified  time that is newer than the
          source  file.   (If  an  existing   destination   file   has   a
          modification time equal to the source file’s, it will be updated
          if the sizes are different.)
    

    请注意,您使用的选项并不暗示这些。-a是:

    -a, --archive               archive mode; same as -rlptgoD (no -H)
    -r, --recursive             recurse into directories
    -l, --links                 copy symlinks as symlinks
    -p, --perms                 preserve permissions
    -o, --owner                 preserve owner (super-user only)
    -g, --group                 preserve group
        --devices               preserve device files (super-user only)
        --specials              preserve special files
    -D                          same as --devices --specials
    -t, --times                 preserve times
    
    • 27
  2. sudodus
    2017-10-30T06:57:20+08:002017-10-30T06:57:20+08:00

    一般的

    如果我理解正确,rsync -av没有内存,所以它也会复制重命名/删除的文件,因为它们存在于源中但不再存在于目标中。

    提示

    • 使用选项“试运行”来检查运行命令行-n之前发生的情况。rsync

    • 注意源目录后面的斜杠的特殊含义,看看两者的区别

      rsync -av --progress dir/a/ dir/b
      

      和

      rsync -av --progress dir/a dir/b
      

      这在手册中有所描述man rsync。

    例子

    您的特殊情况(将文件添加到源目录“a”并从目标目录“b”中删除文件)将添加添加的文件和先前复制的文件,因为它仍在源目录中。无论有没有该选项-u,都会发生这种情况,如果您想将其保留在源目录中,我不知道有任何选项rsync可以轻松解决该问题。

    但是您可以将它从源目录中删除或将文件名放入文件中excluded并使用该选项--exclude-from=excluded(对于许多文件)或仅--exclude=PATTERN用于一个或几个文件。

    $ rsync -avn --progress dir/a/ dir/b
    sending incremental file list
    ./
    file-1
    file-2
    
    sent 103 bytes  received 25 bytes  256.00 bytes/sec
    total size is 13  speedup is 0.10 (DRY RUN)
    
    $ rsync -av --progress dir/a/ dir/b
    sending incremental file list
    ./
    file-1
                  6 100%    0.00kB/s    0:00:00 (xfr#1, to-chk=1/3)
    file-2
                  7 100%    6.84kB/s    0:00:00 (xfr#2, to-chk=0/3)
    
    sent 196 bytes  received 57 bytes  506.00 bytes/sec
    total size is 13  speedup is 0.05
    
    $ echo textx-3>./dir/a/file-3
    
    $ rsync -avn --progress dir/a/ dir/b
    sending incremental file list
    ./
    file-3
    
    sent 121 bytes  received 22 bytes  286.00 bytes/sec
    total size is 21  speedup is 0.15 (DRY RUN)
    
    $ rm dir/b/file-1 
    rm: ta bort normal fil 'dir/b/file-1'? y
    
    $ rsync -avn --progress dir/a/ dir/b
    sending incremental file list
    ./
    file-1
    file-3
    
    sent 124 bytes  received 25 bytes  298.00 bytes/sec
    total size is 21  speedup is 0.14 (DRY RUN)
    
    $ rsync -avun --progress dir/a/ dir/b
    sending incremental file list
    ./
    file-1
    file-3
    
    sent 124 bytes  received 25 bytes  298.00 bytes/sec
    total size is 21  speedup is 0.14 (DRY RUN)
    
    $ rsync -avun --exclude=file-1 --progress dir/a/ dir/b
    sending incremental file list
    ./
    file-3
    
    sent 104 bytes  received 22 bytes  252.00 bytes/sec
    total size is 15  speedup is 0.12 (DRY RUN)
    

    选择:unison

    您可能想测试该工具unison,它是一个同步工具。它提供了一种可视化的方法来识别特殊情况并决定做什么。有一个 GUI 版本 ( unison-gtk)。

    • 7
  3. john-jones
    2017-10-30T06:24:21+08:002017-10-30T06:24:21+08:00

    它只复制 /dir/a 中的新文件。除非您使用 --delete 选项,否则您在 /dir/b 中所做的任何事情都将被忽略。在这种情况下,/dir/b 中重命名的文件将被删除。它将强制 /dir/b 变得与 /dir/a 完全一样。

    关于奖金,你的意思是像在 /dir/a 中重命名文件,然后 rsync 到 /dir/b 的情况?在这种情况下,我认为没有办法阻止 rsync 再次复制文件。

    • 1

相关问题

  • 当我同步到 USB 密钥时,为什么 rsync 会重新复制大量文件?

  • 当我在 USB 密钥上 rsync 时如何替换冒号?

  • 克隆系统并自动复制更改

  • 如果 rsnapshot / rdiff-backup 在传输过程中被中断会发生什么?

  • 如何 rdiff-backup / rsnapshot / backupninja 从 LOCAL 到 REMOTE 机器?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何运行 .sh 脚本?

    • 16 个回答
  • Marko Smith

    如何安装 .tar.gz(或 .tar.bz2)文件?

    • 14 个回答
  • Marko Smith

    如何列出所有已安装的软件包

    • 24 个回答
  • Marko Smith

    无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗?

    • 25 个回答
  • Martin Hope
    Flimm 如何在没有 sudo 的情况下使用 docker? 2014-06-07 00:17:43 +0800 CST
  • Martin Hope
    Ivan 如何列出所有已安装的软件包 2010-12-17 18:08:49 +0800 CST
  • Martin Hope
    La Ode Adam Saputra 无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗? 2010-11-30 18:12:48 +0800 CST
  • Martin Hope
    David Barry 如何从命令行确定目录(文件夹)的总大小? 2010-08-06 10:20:23 +0800 CST
  • Martin Hope
    jfoucher “以下软件包已被保留:”为什么以及如何解决? 2010-08-01 13:59:22 +0800 CST
  • Martin Hope
    David Ashford 如何删除 PPA? 2010-07-30 01:09:42 +0800 CST

热门标签

10.10 10.04 gnome networking server command-line package-management software-recommendation sound xorg

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve