bash:脚本中rsync的进度输出

时间:2013-04-22 08:05:59

标签: linux bash while-loop rsync progress

此脚本是linux live-cd安装程序的一部分。

rsync -aq / /TARGET/ exclude-from=exclude.list &>> errors.log

我想向gui报告进展情况。 gui(gtkdialog)响应任何数字0-100(echo 1; echo 2; etc ......)

在这种情况下,rsync -n(干运行)需要太长时间。

我想跑...

filesystem_size=`(get directory size) / exclude=exclude.list`
rsync -aq / /TARGET/ exclude-from=exclude.list &
while [ rsync is running ]; do
    (check size) /TARGET/
    compare to $filesystem_size
    echo $number (based on the difference of sizes)
done

请帮助获取具有多个排除的目录大小,同时循环for rsync正在运行,echo number(0-100)基于两个大小的差异。

回答以上任何一项都是很有帮助的,谢谢。

编辑:添加完成的RSYNC进度输出(似乎有足够的人在寻找这个) 在Olivier Dulac的帮助下,我完成了这项工作。

size_source=`du -bs --exclude-from=/path/to/exclude.list /source/ | sed "s/[^0-9]*//g"`

size_target=`du -bs /target/ | sed "s/[^0-9]*//g"`

rsync -aq /source/ /target/ --exclude-from=/path/to/exclude.list &

while [[ `jobs | grep "rsync"` ]]; do
  size_target_new=`du -bs /TARGET/ | sed "s/[^0-9]*//g"`
  size_progress=`expr $size_target_new - $size_target`
  expr 100 \* $size_progress / $size_source
  sleep 10
done

这会将%done打印到命令行,仅对大型传输有用。

如果rsync覆盖文件,它将抛弃进度(显示的进度低于实际进度)

exclude.list在rsync和du中读取相同内容,但是dut总是需要完整路径,而rsync假定exclude在其源内。如果复制rootfs“/”它们可以是同一个文件,否则你必须为du写入完整路径(只需将/ source /添加到文件中每行的开头。)

1 个答案:

答案 0 :(得分:2)

确定目标和来源的总大小(有排除):

filesystem_size=$(find /SOURCE -ls | fgrep -f exclude.list  -v | awk '{ TOTAL += $6} END { print int ( TOTAL / 1024 ) }')
     # the above considers you only have, in exclude.list, a list of /path/ or /path/to/files 
     # with no spaces on those files or path. If it contains patterns, change "fgrep" with "egrep". 
     # And give us examples of those patterns so we can adjust the egrep.
     # It also consider that "find ... -ls" will print the size in the 6th column.
size_target=$(du -ks /TARGET | awk '{print $1}')
#there are other ways: 
#   if /TARGET is on different filesystem than /SOURCE, 
#   and if reasonnably sure nothing else is writing on the /TARGET filesystem : 
#     you can use "df -k /TARGET | awk '{print $n}' (n= column showing the size in k)
#     to monitor the target progress.
#     But you need to take its size before starting the rsync, 
#     and then compare it with the current size
循环

while  jobs | grep 'sync' ; do ... ; done
    #It is probably wise to add in the loop a "sleep 5" or something to avoid doing too many size computations, too often.

的大小:

echo "100 * $size_target / $filesystem_size" | bc

请告诉我这些是否适合您。如果没有,请提供尽可能多的详细信息,以帮助确定您的需求。