/ scripts / upload-snapshots
upload-snapshots
 1  #!/usr/bin/env bash
 2  
 3  set -euo pipefail
 4  
 5  # CONFIGURATION
 6  AWS_DEFAULT_REGION="auto"
 7  SOURCE_DIR="${1:-./snapshots}"
 8  
 9  # Check if a file exists in the bucket
10  object_exists() {
11    local key="$1"
12    aws s3api head-object \
13      --bucket "$BUCKET_NAME" \
14      --key "$key" \
15      --endpoint-url "$ENDPOINT" \
16      >/dev/null 2>&1
17  }
18  
19  # Get the size of a remote object in bytes
20  get_remote_size() {
21    local key="$1"
22    aws s3api head-object \
23      --bucket "$BUCKET_NAME" \
24      --key "$key" \
25      --query 'ContentLength' \
26      --output text \
27      --endpoint-url "$ENDPOINT"
28  }
29  
30  # Upload a file to the bucket
31  upload_file() {
32    local file="$1"
33    local key="$2"
34    aws s3 cp "$file" "s3://$BUCKET_NAME/$key" \
35      --endpoint-url "$ENDPOINT"
36  }
37  
38  if [ ! -d "$SOURCE_DIR" ]; then
39    echo "Error: Directory '$SOURCE_DIR' not found."
40    exit 1
41  fi
42  
43  if [ -z "${AWS_ACCESS_KEY_ID:-}" ] || [ -z "${AWS_SECRET_ACCESS_KEY:-}" ] || [ -z "${BUCKET_NAME:-}" ] || [ -z "${ENDPOINT:-}" ]; then
44    echo "Error: AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, BUCKET_NAME and ENDPOINT must be set as environment variables."
45    echo "Example:"
46    echo "  export AWS_ACCESS_KEY_ID=\"<your-access-key>\""
47    echo "  export AWS_SECRET_ACCESS_KEY=\"<your-secret-key>\""
48    echo "  export BUCKET_NAME=\"<your-bucket-name>\""
49    echo "  export ENDPOINT=\"<your-endpoint>\""
50    exit 1
51  fi
52  
53  echo "Uploading all files in '$SOURCE_DIR' (flattened) to bucket '$BUCKET_NAME'..."
54  
55  find "$SOURCE_DIR" -type f | while read -r file; do
56    filename="$(basename "$file")"
57    local_size=$(wc -c < "$file")
58  
59    if object_exists "$filename"; then
60      remote_size=$(get_remote_size "$filename")
61      if [ "$local_size" -eq "$remote_size" ]; then
62        echo "Skipping (already exists): $filename"
63        continue
64      else
65        echo "Re-uploading (size mismatch): $filename"
66      fi
67    else
68      echo "Uploading (new file): $filename"
69    fi
70  
71    upload_file "$file" "$filename"
72  done
73  
74  echo "Uploaded all snapshots!"