#!/bin/sh
# Snapshot the current state of an HA Docker volume.
# Usage: snapshot.sh <volume_name> [output_dir]
#
# Saves a tar.gz of the entire volume to output_dir (default: ./snapshots/).
# Use reset.sh to restore.

VOLUME="${1:?Usage: snapshot.sh <volume_name> [output_dir]}"
OUTPUT_DIR="${2:-./snapshots}"
SNAPSHOT_FILE="$OUTPUT_DIR/$VOLUME-$(date +%Y%m%d-%H%M%S).tar.gz"

mkdir -p "$OUTPUT_DIR"
printf 'Snapshotting volume %s -> %s\n' "$VOLUME" "$SNAPSHOT_FILE"

docker run --rm \
    -v "$VOLUME":/data:ro \
    alpine \
    tar czf - -C / data \
    > "$SNAPSHOT_FILE"

printf 'Snapshot saved: %s\n' "$SNAPSHOT_FILE"
