Automatisierung der S3-zu-GCS-Migration mit Bash-Skripten
.png)
Introduction
Cloud storage plays a crucial role in modern infrastructure, providing scalable and reliable storage solutions. Many businesses migrate from AWS S3 to Google Cloud Storage (GCS) to leverage cost benefits, integration with Google Cloud services, or optimize their cloud strategies. However, when dealing with hundreds of S3 buckets, manual migration is inefficient and time-consuming.
To streamline the process, I automated the migration using Bash scripts and Google Cloud’s Storage Transfer Service. In this blog, I’ll walk you through the steps of automating S3 to GCS migration efficiently.
Why Automate S3 to GCS Migration?
Handling over 200+ S3 buckets manually would involve:
- Repetitive tasks – Creating GCS buckets, setting permissions, and transferring data for each bucket.
- Human errors – Misconfiguration, incorrect bucket names, or missing files.
- Time-consuming process – Manual intervention would take days to complete.
By automating this process, we can:
Save time – Script execution takes a few minutes instead of hours/days.
Eliminate errors – Ensures all S3 buckets are correctly transferred.
Enable monitoring & scheduling – Automate recurring data transfers with Google’s Storage Transfer Service.
Prerequisites
Before running the scripts, ensure you have:
A Google Cloud Project with Billing enabled.
AWS IAM User with s3:ListBucket and s3:GetObject permissions.
Installed Google Cloud SDK (gcloud CLI) on your local machine.
Step 1: Creating Google Cloud Storage Buckets
Each S3 bucket requires a corresponding GCS bucket. The script below reads a list of bucket names from a file and creates them in GCP.
create_gcs_bucket.sh
#!/bin/bash
# Variables
PROJECT_ID="ccd-poc-project" # Replace with your GCP project ID
BUCKET_LIST_FILE="bucket_names.txt" # File containing bucket names
OUTPUT_FILE="created_buckets.txt"
REGION="us-central1" # Change if needed
# Check if the bucket list file exists
if [ ! -f "$BUCKET_LIST_FILE" ]; then
echo "Error: Bucket names file '$BUCKET_LIST_FILE' not found!"
exit 1
fi
# Read bucket names and create GCS buckets
while IFS= read -r BUCKET_NAME || [[ -n "$BUCKET_NAME" ]]; do
if [[ -z "$BUCKET_NAME" ]]; then
continue # Skip empty lines
fi
# Clean bucket name
BUCKET_NAME=$(echo "$BUCKET_NAME" | tr -d '\r' | tr -d '[:space:]')
echo "Creating bucket: $BUCKET_NAME"
gcloud storage buckets create "gs://$BUCKET_NAME" --location="$REGION" --project="$PROJECT_ID"
if [ $? -eq 0 ]; then
echo "gs://$BUCKET_NAME" >> "$OUTPUT_FILE"
echo "Bucket $BUCKET_NAME created successfully."
else
echo "Error: Failed to create bucket $BUCKET_NAME"
fi
done < "$BUCKET_LIST_FILE"
Explanation:
- Reads bucket names from bucket_names.txt.
- Cleans up any unnecessary whitespace.
- Creates GCS buckets with the specified region.
- Stores created bucket names in created_buckets.txt.
Step 2: Automating Data Transfer from S3 to GCS
After creating the required GCS buckets, the next step is to automate data transfer using the gcloud transfer jobs command.
s3_to_gcs_transfer.sh
#!/bin/bash
# Variables
AWS_ACCESS_KEY="YOUR_AWS_ACCESS_KEY"
AWS_SECRET_KEY="YOUR_AWS_SECRET_KEY"
PROJECT_ID="ccd-poc-project"
CREDS_FILE="aws-creds.json"
# Create AWS credentials JSON file
cat <<EOF > "$CREDS_FILE"
{
"awsAccessKeyId": "$AWS_ACCESS_KEY",
"awsSecretAccessKey": "$AWS_SECRET_KEY"
}
EOF
# Read bucket names and create transfer jobs
while IFS= read -r BUCKET_NAME; do
echo "Creating transfer job for S3 bucket: $BUCKET_NAME"
JOB_NAME=$(gcloud transfer jobs create s3://"$BUCKET_NAME" gs://"$BUCKET_NAME" \
--source-auth-method=AWS_SIGNATURE_V4 \
--source-creds-file="$CREDS_FILE" \
--schedule-repeats-every=1d \
--project="$PROJECT_ID" \
--format="value(name)")
if [[ -n "$JOB_NAME" ]]; then
echo "Übertragungsauftrag erfolgreich erstellt: $JOB_NAME"
else
echo "Fehler beim Erstellen des Übertragungsauftrags für $BUCKET_NAME"
fi
done < bucket_names.txt
# Anmeldedatei aus Sicherheitsgründen entfernen
rm "$CREDS_FILE"
echo "Alle Übertragungsaufträge erfolgreich erstellt!"
Erklärung:
- Generiert eine sichere AWS-Zugangsdaten-Datei.
- Liest Bucket-Namen ein und initiiert einen Übertragungsauftrag.
- Prüft, ob eine bestehende Übertragung läuft, bevor eine neue erstellt wird.
- Löscht die Zugangsdaten-Datei nach der Ausführung aus Sicherheitsgründen.
Schritt 3: Ausführen der Migration
Um die Skripte auszuführen, gehen Sie wie folgt vor:
- Speichern Sie die S3-Bucket-Namen in einer Datei namens bucket_names.txt.
- Führen Sie das Skript zur Erstellung von GCS-Buckets aus:
chmod +x create_gcs_bucket.sh
./create_gcs_bucket.sh
- Führen Sie das S3-zu-GCS-Übertragungsskript aus:
chmod +x s3_to_gcs_transfer.sh
./s3_to_gcs_transfer.sh
Fazit
Durch die Automatisierung der S3-zu-GCS-Migration haben wir:
Manuellen Aufwand für die Erstellung von über 200 Buckets eliminiert.
Genaue und effiziente Datenübertragungen sichergestellt.
Tägliche Synchronisierungen für inkrementelle Updates geplant.
Diese Lösung ist leicht skalierbar und kann um erweiterte Funktionen wie Logging, Monitoring und Benachrichtigungen erweitert werden.
Wenn Sie diesen Leitfaden hilfreich fanden, teilen Sie Ihre Gedanken und Erfahrungen gerne in den Kommentaren mit. Viel Erfolg beim Migrieren!


