#!/bin/bash

# Check if the user is root
if [ "$(id -u)" -eq 0 ]; then
    echo "This script must not be run as the root user."
    exit 1
fi

# Namen des bestehenden OctoPrint-Containers
EXISTING_CONTAINER="octoprint-template"

# Verzeichnis, in dem das Backup gespeichert wird
BACKUP_DIR="./template"

# Pfad zum Datenverzeichnis in den Containern
DATA_DIR="/octoprint"

# Erstelle ein Backup des Volumes des bestehenden Containers
docker exec $EXISTING_CONTAINER tar cvf $CONTAINER_BACKUP_DIR/octoprint_backup.tar $DATA_DIR

# Kopiere das Backup vom bestehenden Container in das lokale Dateisystem
docker cp $EXISTING_CONTAINER:$CONTAINER_BACKUP_DIR/octoprint_backup.tar $BACKUP_DIR/octoprint_backup.tar

# Entpacke das Backup
tar xvf $BACKUP_DIR/octoprint_backup.tar -C $BACKUP_DIR/

# rm tar
rm template/octoprint_backup.tar 

# execute python script
python3.9 generator.py