#!/bin/bash # ScriptShare Database Backup Script set -e # Load environment variables if [ -f ".env" ]; then source .env fi # Default values DB_CONTAINER=${DB_CONTAINER:-scriptshare-db} BACKUP_DIR=${BACKUP_DIR:-./backups} RETENTION_DAYS=${BACKUP_RETENTION_DAYS:-30} # Create backup directory mkdir -p "$BACKUP_DIR" # Generate backup filename with timestamp TIMESTAMP=$(date +"%Y%m%d_%H%M%S") BACKUP_FILE="$BACKUP_DIR/scriptshare_backup_$TIMESTAMP.sql" echo "๐Ÿ—„๏ธ Starting database backup..." echo "Backup file: $BACKUP_FILE" # Create database backup docker exec "$DB_CONTAINER" mysqldump \ --single-transaction \ --routines \ --triggers \ --all-databases \ -u root \ -p"${DB_ROOT_PASSWORD}" > "$BACKUP_FILE" # Compress the backup gzip "$BACKUP_FILE" BACKUP_FILE="$BACKUP_FILE.gz" echo "โœ… Database backup completed: $BACKUP_FILE" # Calculate backup size BACKUP_SIZE=$(du -h "$BACKUP_FILE" | cut -f1) echo "๐Ÿ“ฆ Backup size: $BACKUP_SIZE" # Clean up old backups echo "๐Ÿงน Cleaning up old backups (older than $RETENTION_DAYS days)..." find "$BACKUP_DIR" -name "scriptshare_backup_*.sql.gz" -mtime +$RETENTION_DAYS -delete # List remaining backups echo "๐Ÿ“‹ Current backups:" ls -lh "$BACKUP_DIR"/scriptshare_backup_*.sql.gz 2>/dev/null || echo "No backups found" echo "โœ… Backup process completed successfully!"