【问题标题】:bash: progress output for rsync in a scriptbash:脚本中 rsync 的进度输出
【发布时间】:2013-04-15 01:33:02
【问题描述】:

此脚本是 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

请帮助获取具有多个排除项的目录大小,while 循环 for while rsync 正在运行,回显数 (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

这将打印完成百分比到命令行,仅对大传输有用。

如果 rsync 覆盖文件,它会丢掉进度(显示的进度比实际完成的少)

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

【问题讨论】:

  • 你不能只使用选项--progress 吗?
  • rsync --progress 每个文件的输出,不是全部。

标签: linux bash while-loop rsync progress


【解决方案1】:

确定目标和源的总大小(不包括):

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

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

【讨论】:

  • 这是我做的一个测试。 filesystem_size=$(find / -ls | fgrep -f /tmp/exclude.list -v | awk '{ TOTAL += $6} END { print TOTAL /1024 }') rsync -aq / /TARGET/ --exclude-from =/tmp/exclude.list & while 工作 | grep "同步";做 size_target=$(du -ks /TARGET | awk '{print $1}') $size_target ];然后 size_target="1"; fi expr 100 * $size_target / $filesystem_size sleep 5 done #expr: non-integer argument echo $filesystem_size = 624.165 除了一些小问题,你已经大大解决了我的问题:)
  • @user2225483 很高兴它有帮助:)。抱歉,来自 awk 语句的非整数参数:我现在用“int(...)”更正它以强制结果为整数。
猜你喜欢
  • 1970-01-01
  • 2010-12-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多