name: database_backup
id: org.themobileprof.database_backup
version: 1.0.0
description: Backup MySQL/PostgreSQL databases with compression
tags: [devops-ops, composite, database, backup]
provides:
  - database_backed_up
requires:
  - check_command_exists
  - check_disk_space
  - archive_directory
size_kb: 3

flows:
  main:
    start: validate_input
    steps:
      validate_input:
        type: action
        message: "Validating backup configuration..."
        command: |
          DB_TYPE="{{.db_type}}"
          DB_NAME="{{.database}}"
          DB_USER="{{.user}}"
          BACKUP_DIR="{{.backup_dir}}"
          
          if [ -z "$DB_TYPE" ]; then
            echo "❌ Must specify db_type (mysql or postgresql)"
            echo ""
            echo "💡 Example: db_type=mysql database=mydb user=root"
            exit 1
          fi
          
          if [ -z "$DB_NAME" ]; then
            echo "❌ Must specify database name"
            exit 1
          fi
          
          # Validate DB type
          case "$DB_TYPE" in
            mysql|postgresql|postgres)
              ;;
            *)
              echo "❌ Unsupported database type: $DB_TYPE"
              echo "Supported: mysql, postgresql"
              exit 1
              ;;
          esac
          
          # Default backup directory
          if [ -z "$BACKUP_DIR" ]; then
            BACKUP_DIR="$HOME/backups/databases"
          fi
          
          # Expand tilde
          if [[ "$BACKUP_DIR" == ~* ]]; then
            BACKUP_DIR="${BACKUP_DIR/#\~/$HOME}"
          fi
          
          # Create backup directory
          mkdir -p "$BACKUP_DIR"
          
          echo "💾 Database Backup"
          echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
          echo "Type:      $DB_TYPE"
          echo "Database:  $DB_NAME"
          echo "Backup to: $BACKUP_DIR"
          echo ""
          
          echo "$DB_TYPE" > /tmp/clipilot_db_type.txt
          echo "$DB_NAME" > /tmp/clipilot_db_name.txt
          echo "$DB_USER" > /tmp/clipilot_db_user.txt
          echo "$BACKUP_DIR" > /tmp/clipilot_db_backup_dir.txt
        next: branch_db_type
      
      branch_db_type:
        type: branch
        message: "Selecting database type..."
        based_on: last_output
        branches:
          mysql:
            next: backup_mysql
          postgresql:
            next: backup_postgresql
          postgres:
            next: backup_postgresql
          default:
            next: end
      
      backup_mysql:
        type: action
        message: "Backing up MySQL database..."
        command: |
          DB_NAME=$(cat /tmp/clipilot_db_name.txt)
          DB_USER=$(cat /tmp/clipilot_db_user.txt)
          BACKUP_DIR=$(cat /tmp/clipilot_db_backup_dir.txt)
          DB_PASSWORD="{{.password}}"
          DB_HOST="{{.host}}"
          
          # Check if mysqldump is available
          if ! command -v mysqldump >/dev/null 2>&1; then
            echo "❌ mysqldump not installed"
            echo ""
            echo "💡 Install: sudo apt install mysql-client"
            rm -f /tmp/clipilot_db_*.txt
            exit 1
          fi
          
          # Defaults
          if [ -z "$DB_HOST" ]; then
            DB_HOST="localhost"
          fi
          
          if [ -z "$DB_USER" ]; then
            DB_USER="root"
          fi
          
          # Generate backup filename
          TIMESTAMP=$(date +%Y%m%d_%H%M%S)
          BACKUP_FILE="$BACKUP_DIR/${DB_NAME}_${TIMESTAMP}.sql"
          
          echo "Creating MySQL dump..."
          echo ""
          
          # Build mysqldump command
          DUMP_CMD="mysqldump -h $DB_HOST -u $DB_USER"
          
          if [ -n "$DB_PASSWORD" ]; then
            DUMP_CMD="$DUMP_CMD -p$DB_PASSWORD"
          fi
          
          DUMP_CMD="$DUMP_CMD --single-transaction --quick --lock-tables=false $DB_NAME"
          
          # Execute backup
          $DUMP_CMD > "$BACKUP_FILE" 2>&1
          
          if [ $? -eq 0 ]; then
            # Compress backup
            echo "Compressing backup..."
            gzip "$BACKUP_FILE"
            BACKUP_FILE="${BACKUP_FILE}.gz"
            
            echo ""
            echo "✅ Backup completed successfully"
            echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
            echo "File:  $BACKUP_FILE"
            
            BACKUP_SIZE=$(du -h "$BACKUP_FILE" | cut -f1)
            echo "Size:  $BACKUP_SIZE"
            echo ""
            echo "💡 To restore:"
            echo "   gunzip < $BACKUP_FILE | mysql -u $DB_USER -p $DB_NAME"
          else
            echo "❌ Backup failed"
            rm -f "$BACKUP_FILE"
            exit 1
          fi
          
          rm -f /tmp/clipilot_db_*.txt
        next: end
      
      backup_postgresql:
        type: action
        message: "Backing up PostgreSQL database..."
        command: |
          DB_NAME=$(cat /tmp/clipilot_db_name.txt)
          DB_USER=$(cat /tmp/clipilot_db_user.txt)
          BACKUP_DIR=$(cat /tmp/clipilot_db_backup_dir.txt)
          DB_HOST="{{.host}}"
          
          # Check if pg_dump is available
          if ! command -v pg_dump >/dev/null 2>&1; then
            echo "❌ pg_dump not installed"
            echo ""
            echo "💡 Install: sudo apt install postgresql-client"
            rm -f /tmp/clipilot_db_*.txt
            exit 1
          fi
          
          # Defaults
          if [ -z "$DB_HOST" ]; then
            DB_HOST="localhost"
          fi
          
          if [ -z "$DB_USER" ]; then
            DB_USER="postgres"
          fi
          
          # Generate backup filename
          TIMESTAMP=$(date +%Y%m%d_%H%M%S)
          BACKUP_FILE="$BACKUP_DIR/${DB_NAME}_${TIMESTAMP}.sql"
          
          echo "Creating PostgreSQL dump..."
          echo ""
          
          # Execute backup
          pg_dump -h "$DB_HOST" -U "$DB_USER" -d "$DB_NAME" -F p -f "$BACKUP_FILE"
          
          if [ $? -eq 0 ]; then
            # Compress backup
            echo "Compressing backup..."
            gzip "$BACKUP_FILE"
            BACKUP_FILE="${BACKUP_FILE}.gz"
            
            echo ""
            echo "✅ Backup completed successfully"
            echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
            echo "File:  $BACKUP_FILE"
            
            BACKUP_SIZE=$(du -h "$BACKUP_FILE" | cut -f1)
            echo "Size:  $BACKUP_SIZE"
            echo ""
            echo "💡 To restore:"
            echo "   gunzip < $BACKUP_FILE | psql -U $DB_USER -d $DB_NAME"
          else
            echo "❌ Backup failed"
            rm -f "$BACKUP_FILE"
            exit 1
          fi
          
          rm -f /tmp/clipilot_db_*.txt
        next: end
      
      end:
        type: terminal
        message: "Database backup complete"
