Skip to main content

PBS Disk Size of Backups


#!/bin/bash

if [ -z "$1" ]; then
    echo "User: $0 <FILE_PATH>" >&2
    echo "Example: $0 /S3cache-1/ns/.../drive-virtio0.img.fidx" >&2
    exit 1
fi
FILE_PATH="$1"
PREFIX_FOLDER="${FILE_PATH%%/ns/*}"
TOTAL_SIZE=0
COUNT=0

while read -r line; do
    hash=$(printf '%s' "$line" | tr -d '"')
    length=$(printf '%s' "$hash" | wc -c)
    if [ "$length" -ne 64 ]; then
        continue
    fi

    path="/${PREFIX_FOLDER}/.chunks/$(printf '%s' "$hash" | cut -c1-4)/${hash}"

    if [ -f "$path" ]; then
        # Usa du per ottenere la dimensione in byte
        size=$(du -b -- "$path" 2>/dev/null | awk '{print $1}')
        if [ -n "$size" ]; then
            TOTAL_SIZE=$((TOTAL_SIZE + size))
            COUNT=$((COUNT + 1))
        fi
    else
        echo "FILE NON TROVATO: $path"
    fi
done < <(proxmox-backup-debug inspect file "$FILE_PATH")

# Formatta il totale in modo leggibile
if [ "$TOTAL_SIZE" -ge 1099511627776 ]; then
    total_human="$(awk "BEGIN {printf \"%.2f\", $TOTAL_SIZE / 1024^4}") TB"
elif [ "$TOTAL_SIZE" -ge 1073741824 ]; then
    total_human="$(awk "BEGIN {printf \"%.2f\", $TOTAL_SIZE / 1024^3}") GB"
elif [ "$TOTAL_SIZE" -ge 1048576 ]; then
    total_human="$(awk "BEGIN {printf \"%.2f\", $TOTAL_SIZE / 1024^2}") MB"
elif [ "$TOTAL_SIZE" -ge 1024 ]; then
    total_human="$(awk "BEGIN {printf \"%.2f\", $TOTAL_SIZE / 1024}") KB"
else
    total_human="$TOTAL_SIZE byte"
fi

echo ""
echo "===================================================="
echo "Riepilogo:"
echo "  File trovati:      $COUNT"
echo "  Dimensione totale: $total_human ($TOTAL_SIZE byte)"
echo "===================================================="
echo ""