#!/usr/bin/env bash # List recent restic snapshots from a labelled repo. Output: JSON array. # Usage: restic-snapshots.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 # Load env (idempotent — systemd already loaded it for service contexts). 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}" # Show last 30 snapshots, newest first, with the fields the UI needs. restic -r "$REPO" snapshots --json 2>/dev/null \ | jq --arg repo "$LABEL" ' sort_by(.time) | reverse | .[0:30] | map({ id: .id, short_id: (.short_id // (.id[0:8])), time: .time, hostname: .hostname, tags: (.tags // []), paths: (.paths // []), summary: (.summary // null), repo: $repo }) '