---
# Playbook 07: Setup Backups
# Configure automated backup system

- name: Setup Backup System
  hosts: all
  become: yes
  
  tasks:
    - name: Create backup script
      copy:
        dest: "{{ deployment_dir }}/backup.sh"
        owner: "{{ ansible_user }}"
        group: "{{ ansible_user }}"
        mode: '0755'
        content: |
          #!/bin/bash
          # Nextcloud Stack Backup Script
          set -euo pipefail
          
          BACKUP_DIR="{{ deployment_dir }}/backups"
          DB_BACKUP_DIR="$BACKUP_DIR/database"
          VOLUME_BACKUP_DIR="$BACKUP_DIR/volumes"
          RETENTION_DAYS={{ backup_retention_days }}
          DATE=$(date +%Y%m%d_%H%M%S)
          LOG_FILE="$BACKUP_DIR/backup.log"
          
          log() {
              echo "[$(date +'%Y-%m-%d %H:%M:%S')] $1" | tee -a "$LOG_FILE"
          }
          
          log "Starting backup process..."
          
          # Database backup
          log "Backing up PostgreSQL database..."
          docker exec next-db pg_dump -U {{ db_user }} {{ db_name }} | \
              gzip > "$DB_BACKUP_DIR/nextcloud_db_$DATE.sql.gz"
          
          if [ $? -eq 0 ]; then
              log "Database backup completed: nextcloud_db_$DATE.sql.gz"
          else
              log "ERROR: Database backup failed!"
              exit 1
          fi
          
          # Weekly volume backup (Sundays only)
          if [ $(date +%u) -eq 7 ]; then
              log "Weekly volume backup (Sunday)..."
              docker exec -u www-data next php occ maintenance:mode --on
              
              tar -czf "$VOLUME_BACKUP_DIR/nextcloud_data_$DATE.tar.gz" \
                  -C /var/lib/docker/volumes/nextcloud_nextcloud_data/_data . 2>/dev/null || true
              
              tar -czf "$VOLUME_BACKUP_DIR/configs_$DATE.tar.gz" \
                  -C {{ deployment_dir }}/configs . 2>/dev/null || true
              
              docker exec -u www-data next php occ maintenance:mode --off
              log "Volume backup completed"
          fi
          
          # Cleanup old backups
          log "Cleaning up backups older than $RETENTION_DAYS days..."
          find "$DB_BACKUP_DIR" -name "*.sql.gz" -mtime +$RETENTION_DAYS -delete
          find "$VOLUME_BACKUP_DIR" -name "*.tar.gz" -mtime +$RETENTION_DAYS -delete
          
          log "Backup process completed successfully!"

    - name: Create cron job for daily backups
      cron:
        name: "Nextcloud Stack Backup"
        minute: "0"
        hour: "3"
        job: "{{ deployment_dir }}/backup.sh >> {{ deployment_dir }}/backups/backup.log 2>&1"
        user: root

    - name: Run initial test backup
      command: "{{ deployment_dir }}/backup.sh"
      register: backup_test
      ignore_errors: yes

    - name: Display backup status
      debug:
        msg: |
          ✓ Backup script created
          ✓ Cron job configured (daily 3:00 AM)
          ✓ Test backup completed
          Location: {{ deployment_dir }}/backups/
