#!/bin/bash

set -euo pipefail  # Fail on error, undefined variables, and pipe errors

# Create backup filename
FILENAME="$(date +"%Y%m%d_%H%M").zip"
BACKUP_DIR="/BACKUP/$UUID"
mkdir -p "$BACKUP_DIR"

tar -czvf "$BACKUP_DIR/$FILENAME" -C "/4server/data" "$UUID"

# Remove old backups beyond the configured slots
ls -t "$BACKUP_DIR"/[0-9]*.zip 2>/dev/null | tail -n +$((BACKUP_SLOTS + 1)) | while read -r file; do
    echo "Deleting old backup: $file"
    doas rm -f "$file"
done


