Privacy Policy
Snippets index

  Search file in a Restic repo

This script searches for a specific filename (or substring) across all snapshots in a Restic repository. It retrieves the list of snapshots in JSON format, sorts them in descending chronological order (newest first), and then scans each snapshot for paths matching the provided search term (case‑insensitive).

For each snapshot where a match is found, the script prints:

  • the snapshot ID,
  • the snapshot timestamp,
  • and all matching file paths within that snapshot.

Use case:

  • Quickly locate a specific file’s history or presence across multiple Restic backups.
  • Determine in which snapshots a file exists and when it was backed up.

file search_restic_file.bash

#!/bin/bash

if ! command -v jq &> /dev/null; then
    echo "❌ jq is required but not installed. Install it with: apt install jq"
    exit 1
fi

if [ -z "$1" ]; then
    echo "Usage: $0 filename"
    exit 1
fi

SEARCH_TERM="$1"
echo "🔎 Searching for \"$SEARCH_TERM\" in Restic snapshots (newest first)..."

# Get JSON once
JSON=$(restic snapshots --json) || { echo "❌ Failed to get snapshots"; exit 1; }

# Extract sorted short_id and time together
# Output format: "<short_id> <time>"
SORTED=$(echo "$JSON" | jq -r 'sort_by(.time) | reverse | .[] | "\(.short_id) \(.time)"')

if [ -z "$SORTED" ]; then
    echo "❌ No snapshots found."
    exit 1
fi

# Loop through each line: first column is ID, rest is time
while read -r ID TIME; do
    [ -z "$ID" ] && continue
    MATCHES=$(restic ls "$ID" 2>/dev/null | grep -iF "$SEARCH_TERM")
    if [ -n "$MATCHES" ]; then
        echo "✅ Found in snapshot $ID (time: $TIME):"
        echo "$MATCHES"
        echo "--------------------------------"
    fi
done <<< "$SORTED"