Skip to content

Commit fd5b39f

Browse files
committed
Show existing backup count and total size before keep prompt
1 parent 436d07f commit fd5b39f

1 file changed

Lines changed: 55 additions & 1 deletion

File tree

scripts/utm-timevault.sh

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,57 @@ human_size_path() {
232232
fi
233233
}
234234

235+
human_size_bytes() {
236+
local bytes="${1:-0}"
237+
case "$bytes" in
238+
''|*[!0-9]*) bytes=0 ;;
239+
esac
240+
awk -v b="$bytes" 'BEGIN {
241+
split("B KiB MiB GiB TiB PiB", u, " ")
242+
i = 1
243+
while (b >= 1024 && i < 6) {
244+
b = b / 1024
245+
i++
246+
}
247+
if (i == 1) {
248+
printf "%.0f%s", b, u[i]
249+
} else {
250+
printf "%.2f%s", b, u[i]
251+
}
252+
}'
253+
}
254+
255+
path_size_bytes() {
256+
local p="$1"
257+
if [ -d "$p" ]; then
258+
dir_size_bytes "$p"
259+
else
260+
file_size_bytes "$p"
261+
fi
262+
}
263+
264+
vm_backup_inventory_stats() {
265+
local vm="$1"
266+
local files f count total size_bytes
267+
files="$(collect_vm_backups_sorted "$vm" || true)"
268+
count=0
269+
total=0
270+
271+
if [ -n "$files" ]; then
272+
while IFS= read -r f; do
273+
[ -n "$f" ] || continue
274+
count=$((count+1))
275+
size_bytes="$(path_size_bytes "$f")"
276+
case "$size_bytes" in
277+
''|*[!0-9]*) size_bytes=0 ;;
278+
esac
279+
total=$((total + size_bytes))
280+
done <<< "$files"
281+
fi
282+
283+
printf "%s\t%s\n" "$count" "$total"
284+
}
285+
235286
rsync_progress_option() {
236287
if rsync --help 2>/dev/null | grep -q -- '--info'; then
237288
echo "--info=progress2"
@@ -992,7 +1043,7 @@ do_restore_for_vm_and_source() {
9921043
}
9931044

9941045
do_backup_interactive() {
995-
local vm keep_in keep rc
1046+
local vm keep_in keep rc existing_count existing_total_bytes
9961047

9971048
if vm="$(pick_vm_interactive)"; then
9981049
:
@@ -1005,6 +1056,9 @@ do_backup_interactive() {
10051056
return "$rc"
10061057
fi
10071058

1059+
IFS=$'\t' read -r existing_count existing_total_bytes < <(vm_backup_inventory_stats "$vm")
1060+
info "Existing backups for '$vm': ${existing_count:-0} (total: $(human_size_bytes "${existing_total_bytes:-0}"))."
1061+
10081062
keep="$KEEP_DEFAULT"
10091063
if ! read -r -p "How many backups to keep? [$KEEP_DEFAULT]: " keep_in; then
10101064
info "Cancelled."

0 commit comments

Comments
 (0)