#!/bin/bash set -e BACKUP_TARGET="${BACKUP_TARGET:-/backups}" BACKUP_PASSWORD="${BACKUP_PASSWORD:-}" # Validate password if [ -z "$BACKUP_PASSWORD" ]; then echo "ERROR: BACKUP_PASSWORD is required" exit 1 fi # Setup restic environment export RESTIC_PASSWORD="$BACKUP_PASSWORD" export RESTIC_REPOSITORY="$BACKUP_TARGET" # Setup cloud credentials if provided [ -n "$BACKUP_S3_ACCESS_KEY" ] && export AWS_ACCESS_KEY_ID="$BACKUP_S3_ACCESS_KEY" [ -n "$BACKUP_S3_SECRET_KEY" ] && export AWS_SECRET_ACCESS_KEY="$BACKUP_S3_SECRET_KEY" [ -n "$BACKUP_B2_ACCOUNT_ID" ] && export B2_ACCOUNT_ID="$BACKUP_B2_ACCOUNT_ID" [ -n "$BACKUP_B2_ACCOUNT_KEY" ] && export B2_ACCOUNT_KEY="$BACKUP_B2_ACCOUNT_KEY" usage() { cat << EOF VibeStack Restore Tool Usage: restore.sh [OPTIONS] Options: --list List available snapshots --latest Restore from latest snapshot --snapshot ID Restore from specific snapshot --path PATH Restore only specific path --target DIR Restore to specific directory (default: /) --dry-run Show what would be restored without doing it --help Show this help message Examples: # List all snapshots restore.sh --list # Restore everything from latest snapshot restore.sh --latest # Restore specific path from latest restore.sh --latest --path /data/postgres # Restore from specific snapshot restore.sh --snapshot abc123def # Restore to different directory restore.sh --latest --target /tmp/restore # Preview restore restore.sh --latest --dry-run EOF } # Parse arguments SNAPSHOT="" RESTORE_PATH="" TARGET_DIR="/" DRY_RUN="" LIST_ONLY="" while [[ $# -gt 0 ]]; do case $1 in --list) LIST_ONLY="true" shift ;; --latest) SNAPSHOT="latest" shift ;; --snapshot) SNAPSHOT="$2" shift 2 ;; --path) RESTORE_PATH="$2" shift 2 ;; --target) TARGET_DIR="$2" shift 2 ;; --dry-run) DRY_RUN="--dry-run" shift ;; --help) usage exit 0 ;; *) echo "Unknown option: $1" usage exit 1 ;; esac done echo "=== VibeStack Restore ===" echo "Repository: $BACKUP_TARGET" echo "" # List snapshots if [ "$LIST_ONLY" = "true" ]; then echo "Available snapshots:" echo "" restic snapshots exit 0 fi # Validate snapshot if [ -z "$SNAPSHOT" ]; then echo "ERROR: Specify --latest or --snapshot ID" echo "" usage exit 1 fi # Show what we're restoring echo "Snapshot: $SNAPSHOT" [ -n "$RESTORE_PATH" ] && echo "Path: $RESTORE_PATH" echo "Target: $TARGET_DIR" [ -n "$DRY_RUN" ] && echo "Mode: DRY RUN" echo "" # Build restore command restore_args=("restore" "$SNAPSHOT" "--target" "$TARGET_DIR") [ -n "$RESTORE_PATH" ] && restore_args+=("--include" "$RESTORE_PATH") [ -n "$DRY_RUN" ] && restore_args+=("$DRY_RUN") # Confirm unless dry run if [ -z "$DRY_RUN" ]; then echo "WARNING: This will overwrite existing files!" echo "" read -p "Continue? [y/N] " -n 1 -r echo "" if [[ ! $REPLY =~ ^[Yy]$ ]]; then echo "Aborted" exit 0 fi fi echo "Restoring..." restic "${restore_args[@]}" # Post-restore for PostgreSQL if [ -z "$DRY_RUN" ] && [ -f "$TARGET_DIR/data/postgres/backup.sql" ]; then echo "" echo "PostgreSQL dump found at $TARGET_DIR/data/postgres/backup.sql" echo "To restore the database, run:" echo " psql -U vibestack -d vibestack -f $TARGET_DIR/data/postgres/backup.sql" fi echo "" echo "=== Restore Complete ==="