gpt4 book ai didi

linux - bash:脚本中 rsync 的进度输出

转载 作者:太空狗 更新时间:2023-10-29 11:15:13 29 4
gpt4 key购买 nike

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

请帮助获取具有多个排除项的目录大小,在 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

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

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

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

最佳答案

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

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

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

关于linux - bash:脚本中 rsync 的进度输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16142191/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com