例如,能够做到
cp -R folder1 /Volume/Data
或者
some_cp folder1 [email protected]/some/path/folder1
并复制15GB的文件,然后休眠20或30秒?因此,每当该命令复制 15GB 时,它就会休眠 20、30 秒或任何指定的时间,以便硬盘驱动器可以休息,不会承受太大压力。
除了数据量之外,另一种可能是,每3分钟复制一次文件后,休息1分钟?
答案1
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
答案2
您可以获取pid
复制过程的信息并将其暂停,然后再恢复。
您也可以rsync
随时使用和停止它,当您再次启动它时,它将开始复制剩余的文件。
但我建议使用rsync
并暂停 rsync 进程。
假设rsync
pid是1234
to pause:
kill -s STOP 1234
to continue:
kill -s CONT 1234