#!/usr/bin/env bash # Repo stats: combines restic stats in two modes plus snapshot count. # Output: JSON object with restore_size_bytes, raw_data_bytes, dedup_ratio. # Usage: restic-stats.sh nas|b2 set -uo pipefail LABEL="${1:-}" if [ "$LABEL" != "nas" ] && [ "$LABEL" != "b2" ]; then echo '{"error":"label must be nas or b2"}' >&2 exit 2 fi if [ -z "${RESTIC_REPO_NAS:-}" ] && [ -r /etc/restic-backup.env ]; then set -a; . /etc/restic-backup.env; set +a fi case "$LABEL" in nas) REPO="${RESTIC_REPO_NAS:?RESTIC_REPO_NAS not set}" ;; b2) REPO="${RESTIC_REPO_B2:?RESTIC_REPO_B2 not set}" ;; esac export RESTIC_PASSWORD_FILE="${RESTIC_PASSWORD_FILE:-/etc/restic-backup.password}" # restore-size: total bytes if every file in every snapshot were re-extracted. restore_json=$(restic -r "$REPO" stats --mode restore-size --json 2>/dev/null || echo '{}') # raw-data: total unique blob bytes after dedup + compression. raw_json=$(restic -r "$REPO" stats --mode raw-data --json 2>/dev/null || echo '{}') # Snapshot count for the same repo. snap_count=$(restic -r "$REPO" snapshots --json 2>/dev/null | jq 'length // 0') jq -n \ --arg repo "$LABEL" \ --argjson restore "$restore_json" \ --argjson raw "$raw_json" \ --argjson snap_count "${snap_count:-0}" \ ' { repo: $repo, snapshots_count: $snap_count, restore_size_bytes: ($restore.total_size // null), restore_size_files: ($restore.total_file_count // null), raw_data_bytes: ($raw.total_size // null), raw_blob_count: ($raw.total_blob_count // null), dedup_ratio: ( if ($restore.total_size != null) and ($raw.total_size != null) and ($raw.total_size > 0) then (($restore.total_size | tonumber) / ($raw.total_size | tonumber)) else null end ) }'