#!/bin/bash # Configuration CONTAINER_NAME="ppanel-db" DB_USER="vmanroot" DB_PASSWORD="vmanrootpassword" # Replace with actual password DB_NAME="ppanel" # Explicitly specify the database name BACKUP_DIR="/root/db_backups" UPLOADER_PATH="/root/uploader-linux-amd64" # Path to your go uploader binary RETENTION_DAYS=7 # Create backup directory if not exists mkdir -p "$BACKUP_DIR" # Generate timestamp TIMESTAMP=$(date +"%Y%m%d_%H%M%S") FILENAME="mysql_backup_${TIMESTAMP}.sql" FILEPATH="${BACKUP_DIR}/${FILENAME}" GZ_FILEPATH="${FILEPATH}.gz" # 1. Dump MySQL database from Docker container echo "[$(date)] Starting MySQL backup from container ${CONTAINER_NAME}..." # Check if container is running if [ ! "$(docker ps -q -f name=${CONTAINER_NAME})" ]; then echo "Error: Container ${CONTAINER_NAME} is not running!" exit 1 fi # Execute dump docker exec "$CONTAINER_NAME" /usr/bin/mysqldump -u "$DB_USER" -p"$DB_PASSWORD" --databases "$DB_NAME" --no-tablespaces > "$FILEPATH" # Check if file size is too small (e.g., < 1KB), which usually indicates an empty dump or error FILE_SIZE=$(stat -c%s "$FILEPATH" 2>/dev/null || stat -f%z "$FILEPATH") if [ "$FILE_SIZE" -lt 1024 ]; then echo "Error: Backup file is too small ($FILE_SIZE bytes). Dump might have failed." cat "$FILEPATH" # Print content to log for debugging exit 1 fi if [ $? -eq 0 ]; then echo "[$(date)] Database dump successful: ${FILEPATH}" # 2. Compress the backup gzip "$FILEPATH" echo "[$(date)] Compression successful: ${GZ_FILEPATH}" # 3. Upload to MinIO using the Go uploader if [ -f "$UPLOADER_PATH" ]; then echo "[$(date)] Uploading to object storage..." chmod +x "$UPLOADER_PATH" "$UPLOADER_PATH" -file "$GZ_FILEPATH" -bucket backup if [ $? -eq 0 ]; then echo "[$(date)] Upload successful." else echo "[$(date)] Upload failed." fi else echo "Warning: Uploader binary not found at $UPLOADER_PATH. Skipping upload." fi # 4. Clean up old local backups (optional) find "$BACKUP_DIR" -name "mysql_backup_*.sql.gz" -mtime +$RETENTION_DAYS -delete echo "[$(date)] Cleaned up local backups older than $RETENTION_DAYS days." else echo "Error: Database dump failed!" # Clean up empty file if dump failed if [ -f "$FILEPATH" ]; then rm "$FILEPATH" fi exit 1 fi echo "[$(date)] Backup process completed."