#!/bin/bash
# Automated Backup Script for QBPOS Help Website
# Runs daily and keeps 7 days of backups

BACKUP_DIR="/home/pts/backups/qbpos_help"
SOURCE_DIR="/home/pts/Documents/QBPOS_Help_Web/QB_Help_Web"
DATE=$(date +%Y%m%d_%H%M%S)
BACKUP_FILE="qbpos_help_${DATE}.tar.gz"

# Create backup directory if it doesn't exist
mkdir -p "$BACKUP_DIR"

# Create compressed backup
echo "Creating backup: $BACKUP_FILE"
tar -czf "$BACKUP_DIR/$BACKUP_FILE" -C "$(dirname $SOURCE_DIR)" "$(basename $SOURCE_DIR)" 2>/dev/null

# Check if backup was successful
if [ $? -eq 0 ]; then
    echo "✓ Backup created successfully: $BACKUP_DIR/$BACKUP_FILE"
    echo "  Size: $(du -h $BACKUP_DIR/$BACKUP_FILE | cut -f1)"
else
    echo "✗ Backup failed!"
    exit 1
fi

# Delete backups older than 7 days
find "$BACKUP_DIR" -name "qbpos_help_*.tar.gz" -mtime +7 -delete
echo "✓ Cleaned up old backups (keeping last 7 days)"

# Show current backups
echo ""
echo "Current backups:"
ls -lh "$BACKUP_DIR" | grep "qbpos_help_" | tail -5
