#!/usr/bin/env bash set -eu -o pipefail verbose='y' # we test for (non)emptiness quiet='' # we test for (non)emptiness source lib.sh if [ "$#" -ne 1 ]; then abort "Usage: $0 repositoryName" fi info "BEWARE that this script can't garbage collect no longer existing snapshots yet" restic() { command restic --no-cache "$@"; } repoName="$1" checkPathComponent "$repoName" repoFile="repos/$repoName" if [ ! -f "$repoFile" ]; then abort "Repository $repoName does not exist" fi repoType="$(< "$repoFile" jq --raw-output .type)" if [ "$repoType" = "restic" ]; then repoPath="$(< "$repoFile" jq --raw-output '."restic-path"')" info "Inspecting repository $repoName" snapshots="$(restic -r "$repoPath" snapshots --json)" len="$(echo "$snapshots" | jq '. | length')" i=0 while [ "$i" -lt "$len" ]; do snapshot="$(echo "$snapshots" | jq ".[$i]")" id="$(echo "$snapshot" | jq --raw-output '.id')" checkPathComponent "$id" snapshotFile="backups/$repoName/$id" if [ -f "$snapshotFile" ]; then verbose "Snapshot $id is already known" else info "Found new snapshot $id" mkdir -p "backups/$repoName" echo "$snapshot" | jq --sort-keys --tab '. | pick(.hostname, .id, .paths, .tags, .time)' > "$snapshotFile" fi i=$(("$i"+1)) done info "Processed $len snapshots" else abort "Unsupported repository type $repoType" fi