#!/bin/bash

# Define the local backup base directory and S3 bucket paths
LOCAL_BASE_DIR="$HOME/Downloads/velero.backups"
RESTORE_PATHS=(
  "s3://backups/backups/"
  "s3://test/backups/"
)

# Function to upload backups to S3
restore_backup_to_s3() {
  local local_dir="$1"
  local s3_path="$2"

  # Check if the local directory exists
  if [[ ! -d $local_dir ]]; then
    echo "Local directory not found: $local_dir"
    return 1
  fi

  # Sync the local directory back to the S3 path
  echo "Restoring backup from $local_dir to $s3_path"
  aws s3 sync "$local_dir" "$s3_path" --delete

  echo "Backup restored to $s3_path"
}

# Process each restore path
for s3_path in "${RESTORE_PATHS[@]}"; do
  # Determine the local directory for the current S3 path
  subdir=$(echo "$s3_path" | awk -F'/' '{print $4}')
  local_dir="$LOCAL_BASE_DIR/$subdir"

  # Restore the backup to S3
  restore_backup_to_s3 "$local_dir" "$s3_path"
done

echo "All backups restored successfully."
