我可以通过一次读取将文件同时复制到两个驱动器吗?(在单次操作中一对二复制,最好在 rsync 中)

rel*_*mot 4 archive backup files rsync hard-drive

我经常需要将一个源磁盘备份到多个目标磁盘。目前工作流程是rsyncsourcetarget1然后ddtarget1克隆到target2
如果我能这将是可爱的rsync目标1 + TARGET2顺序在同一读操作,大大加快速度。将文件一次读入内存并同步写入两个独立的硬盘驱动器。
有没有办法rsync将文件读入内存并将输出同时通过管道传输到两个写操作?没有rsync但那将是非常受欢迎的。

san*_*lio 6

将来自多个来源的信息放在一起,有几个选项。

结论是,只有使用命令,parallel你才能得到你想要的,见下文。

重要笔记:

  1. 我已经进行cp了复制测试。您还应该考虑使用rsyncvscp或其他替代命令获得的加速(或降低!),结合parallel.
  2. 我已经测试过只复制一个文件。如果复制许多文件(例如,根据需要将几个大文件与许多其他小文件和子目录合并),结果可能会改变。

对于每个选项,我都测试了
time <option #N, copying to one target>
time <option #N, copying to two targets>
Run Code Online (Sandbox Code Playgroud)

进行比较,使用 1.2Gb 的文件。此外,在某些情况下,我测试了两到三遍相同的命令,以评估结果的分散性。我没有计算平均值和标准偏差,但结果很明显。

这是我在上面指定的测试条件下得到的,并附有简短的评论。我已将多个测试的结果连接在一行中,只要可用。

基本情况

$ time cp -p source/file1 target1/

real    0m0,846s    0m0,680s    0m0,659s
user    0m0,000s    0m0,001s    0m0,016s
sys     0m0,777s    0m0,662s    0m0,643s
Run Code Online (Sandbox Code Playgroud)

复印选项

  1. 选项 parallel

    $ parallel cp -p source/file1 ::: target1/
    real    0m0,745s    0m0,740s
    user    0m0,121s    0m0,108s
    sys     0m0,609s    0m0,619s
    
    $ parallel cp -p source/file1 ::: target1/ target2/
    real    0m0,794s    0m0,860s
    user    0m0,116s    0m0,134s
    sys     0m1,300s    0m1,380s
    
    Run Code Online (Sandbox Code Playgroud)
  2. 选项 tee(附加 > /dev/null以避免输出到stdout

    $ tee target1/file1 < source/file1 > /dev/null
    real    0m0,874s    0m1,040s    0m1,028s
    user    0m0,160s    0m0,172s    0m0,137s
    sys     0m0,714s    0m0,868s    0m0,887s
    
    $ tee target1/file1 target2/file1 < source/file1 > /dev/null
    real    0m1,802s    0m1,680s    0m1,833s
    user    0m0,136s    0m0,212s    0m0,197s
    sys     0m1,642s    0m1,468s    0m1,619s
    
    Run Code Online (Sandbox Code Playgroud)

    复制到两个targets 大约使一个的时间加倍target,这比基本情况的时间略长。

  3. 选项 xargs

    $ echo target1 | xargs -n 1 cp -p source/file1
    real    0m0,666s
    user    0m0,021s
    sys     0m0,646s
    
    $ echo target1 target2 | xargs -n 1 cp -p source/file1
    real    0m1,197s
    user    0m0,018s
    sys     0m1,173s
    
    Run Code Online (Sandbox Code Playgroud)

    复制到两个targets 大约使一个的时间加倍target,这与基本情况的时间相似。

  4. 选项 find

    $ find target1 -exec cp -p source/file1 {} \;
    real    0m2,167s
    user    0m0,017s
    sys     0m1,627s
    
    $ find target1 target2 -exec cp -p source/file1 {} \;
    real    0m3,905s
    user    0m0,020s
    sys     0m3,185s
    
    Run Code Online (Sandbox Code Playgroud)

    复制到两个targets 大约使 one 的时间增加一倍target,这比基本情况的时间要长得多……显然是输家。

“多重复制”的来源

  1. https://www.cyberciti.biz/faq/linux-unix-copy-a-file-to-multiple-directories-using-cp-command/
  2. 如何使用命令行将文件复制到多个文件夹?
  3. /sf/ask/13695881/

性能来源cprsync:

  1. https://unix.stackexchange.com/questions/91382/rsync-is-very-slow-factor-8-to-10-compared-to-cp-on-copying-files-from-nfs-sha
  2. https://lwn.net/Articles/400489/
  3. https://superuser.com/questions/1170636/why-is-there-a-write-speed-difference-between-dd-cp-rsync-and-macos-finder-to
  4. `cp`和`rsync`有什么区别?

  • 您是否检查了使用的时间(使用相当大的文件)以验证您的示例确实避免了重新读取源文件,或者与“复制两次”相比提高了速度? (2认同)
  • @sudodus - 你是对的,很快就会完善这一点。 (2认同)
  • @sudodus - `parallel` 似乎不仅是最好的,而且是实现目标的唯一方法。 (2认同)