#!/bin/bash

# --- Configuration ---
# The name of the directory where your docker-compose.yml resides.
# Used to name the final backup file.
BACKUP_NAME="filerun_backup_$(date +%Y%m%d_%H%M%S)"
# The directory where the compressed backup file will be saved.
BACKUP_DIR="/home/Dejan/Docker/FileRun/backup" # <--- ADJUST THIS PATH
# Directory containing your docker-compose.yml (usually the current directory)
COMPOSE_DIR=$(pwd)

# Define the local directories (volumes) to be backed up
# These MUST match the host directories in your docker-compose.yml
DB_DIR="db_data"
HTML_DIR="filerun_html"
USER_DATA_DIR="user_data"

# A temporary directory to stage the files before compression
TEMP_DIR="/tmp/filerun_backup_stage"

# --- Script Start ---

echo "--- Starting FileRun Backup Process ---"

# 1. Ensure the destination backup directory exists
mkdir -p "$BACKUP_DIR"
if [ $? -ne 0 ]; then
    echo "ERROR: Could not create backup directory $BACKUP_DIR. Exiting."
    exit 1
fi

# 2. Stop the FileRun and MariaDB services
echo "Stopping FileRun services to ensure data integrity..."
docker compose down
if [ $? -ne 0 ]; then
    echo "WARNING: Docker Compose down failed. Trying to proceed, but data integrity might be compromised."
fi

# 3. Create the staging directory and copy essential data
echo "Creating staging area and copying data..."
rm -rf "$TEMP_DIR"
mkdir -p "$TEMP_DIR/$BACKUP_NAME"

# Copy the core application files (mainly for system/data and system config)
cp -a "$COMPOSE_DIR/$HTML_DIR" "$TEMP_DIR/$BACKUP_NAME/"
echo "-> Copied Application HTML files."

# Copy the user files (PDFs, images, documents)
cp -a "$COMPOSE_DIR/$USER_DATA_DIR" "$TEMP_DIR/$BACKUP_NAME/"
echo "-> Copied User Files (documents)."

# Copy the database files (Metadata, users, shares, etc.)
cp -a "$COMPOSE_DIR/$DB_DIR" "$TEMP_DIR/$BACKUP_NAME/"
echo "-> Copied Database files."

# 4. Exclude unnecessary large files/caches/logs from the backup
echo "Cleaning up staging area (excluding caches and logs)..."

# Exclude unnecessary MariaDB files/logs within the copied DB_DIR
find "$TEMP_DIR/$BACKUP_NAME/$DB_DIR" -type f -name "*.log" -delete
find "$TEMP_DIR/$BACKUP_NAME/$DB_DIR" -type f -name "ib_logfile*" -delete
echo "-> Removed DB logs."

# Exclude FileRun cache/logs within the copied HTML_DIR
rm -rf "$TEMP_DIR/$BACKUP_NAME/$HTML_DIR/system/cache"
rm -rf "$TEMP_DIR/$BACKUP_NAME/$HTML_DIR/system/logs"
echo "-> Removed FileRun caches and logs."

# 5. Compress the staged data
echo "Compressing files into $BACKUP_NAME.tar.gz..."
tar -czf "$BACKUP_DIR/$BACKUP_NAME.tar.gz" -C "$TEMP_DIR" "$BACKUP_NAME"
if [ $? -ne 0 ]; then
    echo "ERROR: Compression failed. Exiting."
    docker compose up -d # Try to restart services on failure
    exit 1
fi

# 6. Cleanup and Restart
echo "Cleaning up staging area and temporary files..."
rm -rf "$TEMP_DIR"

echo "Starting FileRun services back up..."
docker compose up -d

echo "--- Backup Complete! ---"
echo "Backup file saved to: $BACKUP_DIR/$BACKUP_NAME.tar.gz"
echo "--------------------------"