首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >bash:脚本中rsync的进度输出

bash:脚本中rsync的进度输出
EN

Stack Overflow用户
提问于 2013-04-22 16:05:59
回答 1查看 2.9K关注 0票数 3

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

代码语言:javascript
复制
rsync -aq / /TARGET/ exclude-from=exclude.list &>> errors.log

我想向一位gui报告进展情况。gui (etc对话框)可以响应任何数字0-100 (回声1;回声2;等等...)

在这种情况下,rsync -n (预演)花费的时间太长。

我想跑...

代码语言:javascript
复制
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

请帮助获取带有多个excludes的目录大小,而在rsync运行时,loop for根据两个大小的差异,回显编号(0-100)。

回答上面的任何一个问题都是非常有帮助的,谢谢。

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

代码语言:javascript
复制
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中读取相同的内容,但du始终需要完整路径,而rsync假设排除在其源文件中。如果复制rootfs "/“,它们可以是同一个文件,否则您必须写出du的完整路径(只需将/source/添加到文件中每行的开头即可)。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-04-22 16:16:36

要确定目标和源的总大小(包含排除项),请执行以下操作:

代码语言:javascript
复制
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

对于循环:

代码语言:javascript
复制
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.

对于大小:

代码语言:javascript
复制
echo "100 * $size_target / $filesystem_size" | bc

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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16142191

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档