如何对 rsync 进程设置运行时间限制?

如何对 rsync 进程设置运行时间限制?

我想为 rsync 备份过程设置时间限制。我该怎么做?

根据这个帖子,有一个选项可以做到这一点:

--time-limit
When this option is used rsync will stop after T minutes and exit.

但是,我的 Ubuntu 12.04 上没有实现该选项。在帖子的评论中,暗示了一种可能的解决方案,但我不够熟练,不敢自己尝试,就这样。

似乎还有时间限制命令/程序,但我也没有。这可能是解决方案吗?

答案1

老问题,但 coreutilstimeout命令可以做到这一点(尽管它不会正常结束)。

timeout 60 rsync source destination

其中 60 是时间限制(以秒为单位)

答案2

以下是我修补的方法:

wget http://mirrors.ibiblio.org/rsync/src/rsync-patches-3.0.9.tar.gz
wget http://mirrors.ibiblio.org/rsync/src/rsync-3.0.9.tar.gz
tar xvf rsync-3.0.9.tar.gz
tar xvf rsync-patches-3.0.9.tar.gz
cd rsync-3.0.9
patch -p1 <patches/time-limit.diff
./configure
make
cp ./rsync /usr/local/bin

笔记: 我必须修补rsync备份连接的两端。

答案3

Shell脚本

我编写了一个 bash shellscript,它会不断迭代直到所有内容都复制完毕。当文件很大时,使用上一次迭代中已经复制的内容非常重要,并且能够“看到”复制过程的进度也很不错。

#!/bin/bash

########################################################################

function usage {

 echo "
Usage:   $0 source-dir/ target-dir  # copies content of source-dir
         $0 source-dir  target-dir  # copies source-dir (with subdirs)
"
 exit
}
########################################################################

# main

########################################################################

if [ $# -ne 2 ]
then
 usage
fi
if ! test -d "$1"
then
 echo "$1 is not a directory"
 if test -f "$1"
 then
  echo "but $1 is a file :-)"
 else
  echo "and $1 is not a file :-("
  usage
 fi
fi
if ! test -d "${2##*:}"  # allowing network directories
then
 echo "$2 is not a directory :-("
 usage
else
 echo "$2 is a directory :-)"
fi

cont=true
while $cont
do
 echo "copying ..."
 timeout --foreground 25 rsync --info=progress2 --partial -Ha "$1" "$2"  
 if [ "$?" != "0" ]
 then
  cont=true
  echo "flushing the buffers ..."
  sync
  echo "sleeping for 5 seconds ..."
  sleep 5
 else
  cont=false
 fi
done
echo "final flushing of buffers ..."
sync
echo "Dome :-)"

用法

当你使 shellscript 可执行并在没有任何参数的情况下运行它时,你会收到以下帮助消息,

Usage:   ./rsyncer-w-pause source-dir/ target-dir  # copies content of source-dir
         ./rsyncer-w-pause source-dir  target-dir  # copies source-dir (with subdirs)

我通过将一些带有 Linux 发行版的 ISO 文件从速度较慢的 USB 闪存盘复制到硬盘驱动器来测试 shellscript。这样就进行了几次迭代,复制在 ISO 文件的中间被中断,但复制的部分可以在下一次迭代中使用。所以我检查过它是否也适用于复制大文件。

为了用于实际复印,我认为您应该增加复印时间(从 25 秒开始),还应该增加休眠时间(从 5 秒开始)。使用最适合您特定任务的时间间隔。

关于命令行选项和参数的注释

timeout即使在复制文件的过程中,该命令也会停止其控制的命令的执行。

   --foreground

          when not running timeout directly from a shell prompt,

          allow COMMAND to read from the TTY and get TTY signals; in  this
          mode, children of COMMAND will not be timed out

该命令rsync是一个功能强大的复制命令。请参阅man rsync以获取可能选项的完整描述。

   --info=FLAGS
          This option lets you have fine-grained control over the informa‐
          tion output you want to see.  An individual  flag  name  may  be
          followed  by a level number, with 0 meaning to silence that out‐
          put, 1 being  the  default  output  level,  and  higher  numbers
          increasing  the  output  of  that  flag  (for those that support
          higher levels).  Use --info=help to see all the  available  flag
          names,  what they output, and what flag names are added for each
          increase in the verbose level.  Some examples:

              rsync -a --info=progress2 src/ dest/
              rsync -avv --info=stats2,misc1,flist0 src/ dest/

   --partial
          By default, rsync will delete any partially transferred file  if
          the  transfer  is  interrupted. In some circumstances it is more
          desirable to keep partially transferred files. Using the  --par‐
          tial  option  tells  rsync to keep the partial file which should
          make a subsequent transfer of the rest of the file much faster.

你可能会喜欢也可能不喜欢这个--hard-links选项,

   -H, --hard-links
          This tells rsync to look for hard-linked files in the source and
          link together the corresponding files on the destination.  With‐
          out  this option, hard-linked files in the source are treated as
          though they were separate files.

   -a, --archive
          This  is equivalent to -rlptgoD. It is a quick way of saying you
          want recursion and want to preserve almost everything  (with  -H
          being  a  notable  omission).   The  only exception to the above
          equivalence is when --files-from is specified, in which case  -r
          is not implied.

          Note that -a does not preserve hardlinks, because finding multi‐
          ply-linked files is expensive.  You must separately specify -H.

最后,阅读有关参数(源和目标)的解释,

          rsync -avz foo:src/bar /data/tmp

   This would recursively transfer all files from the directory src/bar on
   the  machine foo into the /data/tmp/bar directory on the local machine.
   The files are transferred in "archive" mode, which  ensures  that  sym‐
   bolic  links,  devices,  attributes,  permissions, ownerships, etc. are
   preserved in the transfer.  Additionally, compression will be  used  to
   reduce the size of data portions of the transfer.

          rsync -avz foo:src/bar/ /data/tmp

   A  trailing slash on the source changes this behavior to avoid creating
   an additional directory level at the destination.  You can think  of  a
   trailing / on a source as meaning "copy the contents of this directory"
   as opposed to "copy the directory by  name",  but  in  both  cases  the
   attributes  of the containing directory are transferred to the contain‐
   ing directory on the destination.  In other words, each of the  follow‐
   ing  commands copies the files in the same way, including their setting
   of the attributes of /dest/foo:

          rsync -av /src/foo /dest
          rsync -av /src/foo/ /dest/foo

相关内容