#!/bin/bash
# Backup the management platform code and configuration

BACKUP_DATE=$(date +%Y%m%d_%H%M%S)
BACKUP_NAME="platform-backup-${BACKUP_DATE}.tar.gz"
BACKUP_DIR="/root/backups/platform-backups"

# Create backup directory if not exists
mkdir -p $BACKUP_DIR

echo "$(date): Starting platform backup..." >> /root/platform-backup.log

# Backup platform directory
tar -czf "$BACKUP_DIR/$BACKUP_NAME" /root/management-platform/ 2>/dev/null

if [ $? -eq 0 ]; then
    echo "  ✅ Platform backup created: $BACKUP_NAME" >> /root/platform-backup.log
    
    # Keep only last 7 backups
    find $BACKUP_DIR -name "platform-backup-*.tar.gz" -mtime +7 -delete
    echo "  ✅ Cleaned up old backups" >> /root/platform-backup.log
else
    echo "  ❌ Platform backup FAILED!" >> /root/platform-backup.log
fi

echo "  Done." >> /root/platform-backup.log
echo "" >> /root/platform-backup.log
