#!/bin/bash set -euo pipefail BASE="/opt" CURRENT="/opt/iventoy" PERSIST="/srv/iventoy" ISO_DIR="${PERSIST}/iso" CONFIG_DIR="${PERSIST}/config" CONFIG_FILE="${CONFIG_DIR}/config.dat" SERVICE="iventoy" TMP="/tmp/iventoy-update" GITHUB_REPO="ventoy/PXE" MAX_BACKUPS=10 echo "==========================================" echo " iVentoy Update" echo "==========================================" echo # -------------------------------------------------- # Root check # -------------------------------------------------- if [ "$(id -u)" -ne 0 ]; then echo "ERROR: Dieses Script muss als root laufen." exit 1 fi # -------------------------------------------------- # Dependencies # -------------------------------------------------- apt-get update -qq apt-get install -y -qq curl tar >/dev/null # -------------------------------------------------- # Persistent directories # -------------------------------------------------- mkdir -p "$ISO_DIR" mkdir -p "$CONFIG_DIR" # -------------------------------------------------- # Latest Version ermitteln # -------------------------------------------------- echo "Ermittle aktuelle iVentoy-Version..." LATEST_TAG=$( curl -fsSL \ -o /dev/null \ -w '%{url_effective}' \ "https://github.com/${GITHUB_REPO}/releases/latest" \ | awk -F/ '{print $NF}' ) VERSION="${LATEST_TAG#v}" if [ -z "$VERSION" ]; then echo "ERROR: Konnte aktuelle Version nicht ermitteln." exit 1 fi TARGET="${BASE}/iventoy-${VERSION}" echo "Neueste Version: $VERSION" # -------------------------------------------------- # Prüfen ob bereits aktuell # -------------------------------------------------- if [ -L "$CURRENT" ]; then CURRENT_TARGET=$(readlink -f "$CURRENT" || true) if [ "$CURRENT_TARGET" = "$TARGET" ]; then echo echo "==========================================" echo " iVentoy ist bereits aktuell" echo "==========================================" echo echo "Aktive Version: $VERSION" echo "Installation: $CURRENT -> $TARGET" echo exit 0 fi fi # -------------------------------------------------- # Bestehende Config übernehmen # -------------------------------------------------- if [ ! -f "$CONFIG_FILE" ]; then if [ -f "${CURRENT}/data/config.dat" ]; then echo echo "Übernehme bestehende iVentoy Config..." cp -a "${CURRENT}/data/config.dat" "$CONFIG_FILE" else echo echo "WARNUNG: Keine bestehende config.dat gefunden." fi fi # -------------------------------------------------- # Config Backup # -------------------------------------------------- if [ -f "$CONFIG_FILE" ]; then BACKUP="${CONFIG_FILE}.bak-$(date +%Y%m%d-%H%M%S)" echo echo "Sichere Config:" echo " $BACKUP" cp -a "$CONFIG_FILE" "$BACKUP" # -------------------------------------------------- # Backup Rotation # -------------------------------------------------- echo "Behalte maximal ${MAX_BACKUPS} Config-Backups..." find "$CONFIG_DIR" \ -maxdepth 1 \ -type f \ -name 'config.dat.bak-*' \ -printf '%T@ %p\n' \ | sort -nr \ | tail -n +"$((MAX_BACKUPS + 1))" \ | cut -d' ' -f2- \ | xargs -r rm -f fi # -------------------------------------------------- # Download / Entpacken # -------------------------------------------------- if [ -d "$TARGET" ]; then echo echo "Version $VERSION ist bereits vorhanden:" echo " $TARGET" else rm -rf "$TMP" mkdir -p "$TMP" ARCHIVE="iventoy-${VERSION}-linux-x86_64-free.tar.gz" URL="https://github.com/${GITHUB_REPO}/releases/download/v${VERSION}/${ARCHIVE}" echo echo "Lade herunter:" echo " $URL" curl -fL "$URL" -o "${TMP}/${ARCHIVE}" echo echo "Entpacke iVentoy..." tar -xzf "${TMP}/${ARCHIVE}" -C "$TMP" EXTRACTED="${TMP}/iventoy-${VERSION}" if [ ! -d "$EXTRACTED" ]; then echo "ERROR: Erwartetes Verzeichnis wurde nicht gefunden:" echo " $EXTRACTED" exit 1 fi mv "$EXTRACTED" "$TARGET" fi # -------------------------------------------------- # ISO Symlink # -------------------------------------------------- echo echo "Setze ISO-Symlink..." rm -rf "${TARGET}/iso" ln -s "$ISO_DIR" "${TARGET}/iso" # -------------------------------------------------- # Config Symlink # -------------------------------------------------- if [ -f "$CONFIG_FILE" ]; then echo "Setze Config-Symlink..." rm -f "${TARGET}/data/config.dat" ln -s "$CONFIG_FILE" "${TARGET}/data/config.dat" fi # -------------------------------------------------- # Service stoppen # -------------------------------------------------- echo echo "Stoppe iVentoy..." systemctl stop "$SERVICE" 2>/dev/null || true sleep 2 # -------------------------------------------------- # Alte direkte Installation sichern # -------------------------------------------------- if [ -e "$CURRENT" ] && [ ! -L "$CURRENT" ]; then OLD="${BASE}/iventoy-old-$(date +%Y%m%d-%H%M%S)" echo echo "Bestehende Installation wird verschoben:" echo " $CURRENT -> $OLD" mv "$CURRENT" "$OLD" fi # -------------------------------------------------- # Current Symlink atomar setzen # -------------------------------------------------- echo echo "Aktiviere iVentoy $VERSION..." ln -sfn "$TARGET" "${CURRENT}.new" mv -Tf "${CURRENT}.new" "$CURRENT" # -------------------------------------------------- # Service starten # -------------------------------------------------- echo echo "Starte iVentoy..." systemctl start "$SERVICE" sleep 3 # -------------------------------------------------- # Health Check # -------------------------------------------------- FAILED=0 if ! systemctl is-active --quiet "$SERVICE"; then echo "ERROR: systemd Service läuft nicht." FAILED=1 fi if ! ss -lntH | awk '{print $4}' | grep -Eq '[:.]26000$'; then echo "ERROR: TCP 26000 lauscht nicht." FAILED=1 fi if ! ss -lntH | awk '{print $4}' | grep -Eq '[:.]16000$'; then echo "ERROR: TCP 16000 lauscht nicht." FAILED=1 fi if ! ss -lnuH | awk '{print $4}' | grep -Eq '[:.]69$'; then echo "ERROR: UDP 69 lauscht nicht." FAILED=1 fi if ! ss -lnuH | awk '{print $4}' | grep -Eq '[:.]4011$'; then echo "ERROR: UDP 4011 lauscht nicht." FAILED=1 fi if ! ss -lnuH | awk '{print $4}' | grep -Eq '[:.]67$'; then echo "ERROR: UDP 67 lauscht nicht." FAILED=1 fi if [ "$FAILED" -ne 0 ]; then echo echo "==========================================" echo " UPDATE FEHLGESCHLAGEN" echo "==========================================" echo if [ -f "${CURRENT}/log/log.txt" ]; then echo "Letzte iVentoy Log-Einträge:" tail -n 40 "${CURRENT}/log/log.txt" || true fi exit 1 fi # -------------------------------------------------- # Cleanup # -------------------------------------------------- rm -rf "$TMP" echo echo "==========================================" echo " Update erfolgreich" echo "==========================================" echo echo "Aktive Version:" echo " $VERSION" echo echo "Installation:" echo " $CURRENT -> $TARGET" echo echo "Config:" echo " ${CURRENT}/data/config.dat -> $CONFIG_FILE" echo echo "ISO:" echo " ${CURRENT}/iso -> $ISO_DIR" echo echo "Config-Backups:" echo " maximal $MAX_BACKUPS" echo