#!/usr/bin/env bash
set -e

APP_DIR="/var/www/html/easychair"
BACKUP_DIR="$APP_DIR/storage/backups"
DATE_TAG="$(date +%Y%m%d_%H%M%S)"
DB_NAME="shquier_easychair"
DB_USER="root"
DB_PASS="_MohdShquier@2025!"

mkdir -p "$BACKUP_DIR/db"
mkdir -p "$BACKUP_DIR/files"

echo "Creating database backup..."
mysqldump -u "$DB_USER" -p"$DB_PASS" --single-transaction --routines --triggers "$DB_NAME" > "$BACKUP_DIR/db/${DB_NAME}_${DATE_TAG}.sql"

echo "Creating storage backup..."
tar -czf "$BACKUP_DIR/files/storage_${DATE_TAG}.tar.gz" -C "$APP_DIR" storage

echo "Backup completed:"
echo "$BACKUP_DIR/db/${DB_NAME}_${DATE_TAG}.sql"
echo "$BACKUP_DIR/files/storage_${DATE_TAG}.tar.gz"
