# Docker Compose override for backup and monitoring configurations
# Usage: docker-compose -f docker-compose.yml -f docker-compose.backup.yml up -d

version: '3.8'

volumes:
  # PostgreSQL backup storage
  postgres_backups:
    driver: local
    driver_opts:
      type: none
      o: bind
      device: ./backups/postgres
  
  # Application logs storage
  app_logs:
    driver: local
    driver_opts:
      type: none
      o: bind
      device: ./logs

services:
  # PostgreSQL service with backup configuration
  postgres:
    volumes:
      - postgres_backups:/var/backups/postgres
      - ./docker/postgres/backup.sh:/usr/local/bin/backup.sh:ro
      - ./docker/postgres/restore.sh:/usr/local/bin/restore.sh:ro
    environment:
      - POSTGRES_BACKUP_ENABLED=true
    # Add backup cron job
    command: >
      bash -c "
        chmod +x /usr/local/bin/backup.sh /usr/local/bin/restore.sh &&
        # Install cron
        apt-get update && apt-get install -y cron &&
        # Add backup cron job (daily at 2 AM)
        echo '0 2 * * * /usr/local/bin/backup.sh >> /var/log/backup.log 2>&1' | crontab - &&
        # Start cron daemon
        cron &&
        # Start PostgreSQL
        docker-entrypoint.sh postgres
      "

  # Web service with log rotation
  web:
    volumes:
      - app_logs:/app/logs
    logging:
      driver: "json-file"
      options:
        max-size: "10m"
        max-file: "5"
        compress: "true"
    environment:
      - LOG_LEVEL=INFO
      - LOG_ROTATION_ENABLED=true

  # Celery worker with log rotation
  celery_worker:
    volumes:
      - app_logs:/app/logs
    logging:
      driver: "json-file"
      options:
        max-size: "10m"
        max-file: "5"
        compress: "true"
    environment:
      - LOG_LEVEL=INFO
      - LOG_ROTATION_ENABLED=true

  # Celery beat with log rotation
  celery_beat:
    volumes:
      - app_logs:/app/logs
      - celery_beat_data:/app/celerybeat-schedule
    logging:
      driver: "json-file"
      options:
        max-size: "10m"
        max-file: "5"
        compress: "true"
    environment:
      - LOG_LEVEL=INFO
      - LOG_ROTATION_ENABLED=true

  # Nginx with log rotation
  nginx:
    volumes:
      - app_logs:/var/log/nginx
    logging:
      driver: "json-file"
      options:
        max-size: "10m"
        max-file: "5"
        compress: "true"

  # Redis with log rotation
  redis:
    logging:
      driver: "json-file"
      options:
        max-size: "5m"
        max-file: "3"
        compress: "true"

  # Prometheus with extended retention and backup
  prometheus:
    volumes:
      - prometheus_data:/prometheus
      - ./docker/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml:ro
      - ./backups/prometheus:/var/backups/prometheus
    command:
      - '--config.file=/etc/prometheus/prometheus.yml'
      - '--storage.tsdb.path=/prometheus'
      - '--web.console.libraries=/etc/prometheus/console_libraries'
      - '--web.console.templates=/etc/prometheus/consoles'
      - '--web.enable-lifecycle'
      - '--storage.tsdb.retention.time=30d'  # Keep data for 30 days
      - '--storage.tsdb.retention.size=10GB'  # Limit storage to 10GB
    logging:
      driver: "json-file"
      options:
        max-size: "10m"
        max-file: "3"
        compress: "true"

  # Grafana with backup configuration
  grafana:
    volumes:
      - grafana_data:/var/lib/grafana
      - ./backups/grafana:/var/backups/grafana
    environment:
      - GF_LOG_LEVEL=info
      - GF_LOG_MODE=file
      - GF_PATHS_LOGS=/var/log/grafana
    logging:
      driver: "json-file"
      options:
        max-size: "10m"
        max-file: "3"
        compress: "true"

  # Log rotation service
  logrotate:
    image: alpine:latest
    container_name: adtlas_logrotate
    volumes:
      - app_logs:/var/log/app
      - ./docker/logrotate/logrotate.conf:/etc/logrotate.conf:ro
      - ./docker/logrotate/logrotate.d:/etc/logrotate.d:ro
    command: >
      sh -c "
        apk add --no-cache logrotate &&
        while true; do
          logrotate -f /etc/logrotate.conf &&
          sleep 3600
        done
      "
    restart: unless-stopped
    depends_on:
      - web
      - celery_worker
      - celery_beat

  # Backup service for automated backups
  backup_service:
    image: postgres:15-alpine
    container_name: adtlas_backup_service
    volumes:
      - postgres_backups:/var/backups/postgres
      - ./docker/postgres/backup.sh:/usr/local/bin/backup.sh:ro
    environment:
      - POSTGRES_HOST=postgres
      - POSTGRES_DB=${POSTGRES_DB}
      - POSTGRES_USER=${POSTGRES_USER}
      - POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
    command: >
      sh -c "
        apk add --no-cache dcron &&
        chmod +x /usr/local/bin/backup.sh &&
        echo '0 2 * * * /usr/local/bin/backup.sh' | crontab - &&
        crond -f
      "
    restart: unless-stopped
    depends_on:
      - postgres
    networks:
      - adtlas_network