Files
Frank John Begornia 6b0c271c1e
All checks were successful
Deploy MinIO Production / deploy (push) Successful in 32s
Fix backup script to load credentials from .env
2026-01-06 13:20:30 +08:00

150 lines
5.1 KiB
YAML
Raw Permalink Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
name: Backup MinIO Buckets
on:
schedule:
# Run daily at 2 AM
- cron: '0 2 * * *'
workflow_dispatch:
jobs:
backup:
runs-on: ubuntu-latest
container:
image: catthehacker/ubuntu:act-latest
steps:
# 1⃣ Setup SSH
- name: Setup SSH
shell: sh
env:
DEPLOY_SSH_KEY: ${{ secrets.DEPLOY_SSH_KEY }}
DEPLOY_HOST: ${{ secrets.DEPLOY_HOST }}
run: |
mkdir -p ~/.ssh
chmod 700 ~/.ssh
echo "$DEPLOY_SSH_KEY" > ~/.ssh/id_ed25519
chmod 600 ~/.ssh/id_ed25519
ssh-keyscan -H "$DEPLOY_HOST" >> ~/.ssh/known_hosts
# 2⃣ Run backup on server
- name: Backup MinIO buckets
shell: sh
env:
DEPLOY_USER: ${{ secrets.DEPLOY_USER }}
DEPLOY_HOST: ${{ secrets.DEPLOY_HOST }}
run: |
ssh -i ~/.ssh/id_ed25519 $DEPLOY_USER@$DEPLOY_HOST << 'EOF'
set -e
BACKUP_BASE="/var/backups/minio"
BACKUP_DIR="$BACKUP_BASE/$(date +%Y%m%d_%H%M%S)"
CONTAINER_NAME="crew-minio-prod"
DEPLOY_DIR="/var/www/apps/minio-storage"
echo "📦 Starting MinIO backup to $BACKUP_DIR"
sudo mkdir -p "$BACKUP_DIR"
sudo chown $USER:$USER "$BACKUP_DIR"
# Check if container is running
if ! docker ps --format '{{.Names}}' | grep -q "$CONTAINER_NAME"; then
echo "❌ MinIO container is not running"
exit 1
fi
# Load credentials from .env file
echo "🔑 Loading MinIO credentials"
if [ ! -f "$DEPLOY_DIR/.env" ]; then
echo "❌ .env file not found at $DEPLOY_DIR/.env"
exit 1
fi
# Source .env and export variables
set -a
source "$DEPLOY_DIR/.env"
set +a
if [ -z "$MINIO_ROOT_USER" ] || [ -z "$MINIO_ROOT_PASSWORD" ]; then
echo "❌ MINIO_ROOT_USER or MINIO_ROOT_PASSWORD not set in .env"
exit 1
fi
echo "✓ Credentials loaded (User: $MINIO_ROOT_USER)"
echo "🔧 Installing MinIO client if needed"
docker exec $CONTAINER_NAME sh -c "
if ! command -v mc &> /dev/null; then
curl -sSL https://dl.min.io/client/mc/release/linux-amd64/mc -o /usr/local/bin/mc
chmod +x /usr/local/bin/mc
fi
" || echo "mc already installed"
echo "⚙️ Configuring MinIO client"
docker exec $CONTAINER_NAME mc alias set backup http://localhost:9000 \
"$MINIO_ROOT_USER" "$MINIO_ROOT_PASSWORD"
# Backup each bucket
BUCKETS="crewsportswear merchbay merchbay-admin crew-admin email-reports"
for BUCKET in $BUCKETS; do
echo "💾 Backing up bucket: $BUCKET"
# Create bucket directory
sudo mkdir -p "$BACKUP_DIR/$BUCKET"
sudo chown $USER:$USER "$BACKUP_DIR/$BUCKET"
# Export bucket to container temp
docker exec $CONTAINER_NAME mc mirror --overwrite backup/$BUCKET /tmp/backup_$BUCKET/
# Copy from container to host
docker cp $CONTAINER_NAME:/tmp/backup_$BUCKET/. "$BACKUP_DIR/$BUCKET/"
# Cleanup container temp
docker exec $CONTAINER_NAME rm -rf /tmp/backup_$BUCKET
# Count files
FILE_COUNT=$(find "$BACKUP_DIR/$BUCKET" -type f | wc -l)
echo " ✓ Backed up $FILE_COUNT files from $BUCKET"
done
echo "📊 Backup statistics"
du -sh "$BACKUP_DIR"
du -sh "$BACKUP_DIR"/*
echo "🗜️ Compressing backup"
cd "$BACKUP_BASE"
tar -czf "minio_backup_$(date +%Y%m%d_%H%M%S).tar.gz" "$(basename $BACKUP_DIR)"
echo "🧹 Removing uncompressed backup"
rm -rf "$BACKUP_DIR"
echo "🗑️ Cleaning old backups (keeping last 7 days)"
find "$BACKUP_BASE" -name "minio_backup_*.tar.gz" -type f -mtime +7 -delete
echo "📋 Current backups:"
ls -lh "$BACKUP_BASE"/minio_backup_*.tar.gz 2>/dev/null || echo "No compressed backups found"
echo "✅ Backup completed successfully!"
EOF
# 3⃣ Verify backup
- name: Verify backup
shell: sh
env:
DEPLOY_USER: ${{ secrets.DEPLOY_USER }}
DEPLOY_HOST: ${{ secrets.DEPLOY_HOST }}
run: |
ssh -i ~/.ssh/id_ed25519 $DEPLOY_USER@$DEPLOY_HOST << 'EOF'
LATEST_BACKUP=$(ls -t /var/backups/minio/minio_backup_*.tar.gz 2>/dev/null | head -1)
if [ -f "$LATEST_BACKUP" ]; then
echo "✅ Latest backup verified:"
ls -lh "$LATEST_BACKUP"
BACKUP_SIZE=$(du -sh "$LATEST_BACKUP" | awk '{print $1}')
echo "📦 Backup size: $BACKUP_SIZE"
else
echo "❌ No backup file found!"
exit 1
fi
EOF