backup – 使用相同的文件列表rsync到多个目标?

发布时间:2020-01-06 发布网站:脚本之家
脚本之家收集整理的这篇文章主要介绍了backup – 使用相同的文件列表rsync到多个目标?脚本之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想知道rsync是否可以一次性将一个目录复制到多个远程目的地,甚至可以并行复制. (没必要,但会有用.)

通常,类似下面的内容可以正常工作:

$rsync -Pav /junk user@host1:/backup
$rsync -Pav /junk user@host2:/backup
$rsync -Pav /junk user@host3:/backup

如果这是唯一的选择,我会用它.但是,/ junk位于具有相当多文件的慢速驱动器上,并且与实际传输/更新相比,每次重建大约12,000个文件的文件列表令人痛苦地慢(约5分钟).有可能做这样的事情来完成同样的事情:

$rsync -Pav /junk user@host1:/backup user@host2:/backup user@host3:/backup

谢谢你的期待!

解决方法

以下是有关批处理模式的rsync手册页的信息.

BATCH MODE

Batch mode can be used to apply the same set of updates to many identical systems. Suppose one has a tree which is replicated on a number of hosts. Now suppose some changes have been made to this
source tree and those changes need to be propagated to the other hosts. In order to do this using batch mode,rsync is run with the write-batch option to apply the changes made to the source tree to
one of the destination trees. The write-batch option causes the rsync client to store in a “batch file” all the information needed to repeat this operation against other,identical destination
trees.

Generating the batch file once saves having to perform the file status,checksum,and data block generation more than once when updating multiple destination trees. Multicast transport protocols can
be used to transfer the batch update files in parallel to many hosts at once,instead of sending the same data to every host individually.

To apply the recorded changes to another destination tree,run rsync with the read-batch option,specifying the name of the same batch file,and the destination tree. Rsync updates the destination
tree using the information stored in the batch file.

For your convenience,a script file is also created when the write-batch option is used: it will be named the same as the batch file with “.sh” appended. This script file contains a command-line
suitable for updating a destination tree using the associated batch file. It can be executed using a Bourne (or Bourne-like) shell,optionally passing in an alternate destination tree pathname which
is then used instead of the original destination path. This is useful when the destination tree path on the current host differs from the one used to create the batch file.

Examples:

          $rsync --write-batch=foo -a host:/source/dir/ /adest/dir/
          $scp foo* remote:
          $ssh remote ./foo.sh /bdest/dir/

          $rsync --write-batch=foo -a /source/dir/ /adest/dir/
          $ssh remote rsync --read-batch=- -a /bdest/dir/ <foo

In these examples,rsync is used to update /adest/dir/ from /source/dir/ and the information to repeat this operation is stored in “foo” and “foo.sh”. The host “remote” is then updated with the
batched data going into the directory /bdest/dir. The differences between the two examples reveals some of the flexibility you have in how you deal with batches:

  • The first example shows that the initial copy doesn’t have to be local — you can push or pull data to/from a remote host using either the remote-shell syntax or rsync daemon syntax,as
    desired.

  • The first example uses the created “foo.sh” file to get the right rsync options when running the read-batch command on the remote host.

  • The second example reads the batch data via standard input so that the batch file doesn’t need to be copied to the remote machine first. This example avoids the foo.sh script because it
    needed to use a modified –read-batch option,but you could edit the script file if you wished to make use of it (just be sure that no other option is trying to use standard input,such as
    the “–exclude-from=-” option).

    Caveats:

    The read-batch option expects the destination tree that it is updating to be identical to the destination tree that was used to create the batch update fileset. When a difference between the desti‐
    nation trees is encountered the update might be discarded with a warning (if the file appears to be up-to-date already) or the file-update may be attempted and then,if the file fails to verify,the
    update discarded with an error. This means that it should be safe to re-run a read-batch operation if the command got interrupted. If you wish to force the batched-update to always be attempted
    regardless of the file’s size and date,use the -I option (when reading the batch). If an error occurs,the destination tree will probably be in a partially updated state. In that case,rsync can
    be used in its regular (non-batch) mode of operation to fix up the destination tree.

    The rsync version used on all destinations must be at least as new as the one used to generate the batch file. Rsync will die with an error if the protocol version in the batch file is too new for
    the batch-reading rsync to handle. See also the –protocol option for a way to have the creating rsync generate a batch file that an older rsync can understand. (Note that batch files changed for‐
    mat in version 2.6.3,so mixing versions older than that with newer versions will not work.)

    When reading a batch file,rsync will force the value of certain options to match the data in the batch file if you didn’t set them to the same as the batch-writing command. Other options can (and
    should) be changed. For instance –write-batch changes to –read-batch,–files-from is dropped,and the –filter/–include/–exclude options are not needed unless one of the –delete options is
    specified.

    The code that creates the BATCH.sh file transforms any filter/include/exclude options into a single list that is appended as a “here” document to the shell script file. An advanced user can use
    this to modify the exclude list if a change in what gets deleted by –delete is desired. A normal user can ignore this detail and just use the shell script as an easy way to run the appropriate
    –read-batch command for the batched data.

    The original batch mode in rsync was based on “rsync+”,but the latest version uses a new implementation.

我想你可以试试

rsync --write-batch=foo -Pav /junk user@host1:/backup
foo.sh user@host2:/backup
foo.sh user@host3:/backup

总结

以上是脚本之家为你收集整理的backup – 使用相同的文件列表rsync到多个目标?全部内容,希望文章能够帮你解决backup – 使用相同的文件列表rsync到多个目标?所遇到的程序开发问题。

如果觉得脚本之家网站内容还不错,欢迎将脚本之家网站推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您喜欢交流学习经验,点击链接加入脚本之家官方QQ群:1065694478