diff --git a/webapps_migration/migrate_superx.conf b/webapps_migration/migrate_superx.conf new file mode 100644 index 0000000..04e011c --- /dev/null +++ b/webapps_migration/migrate_superx.conf @@ -0,0 +1,76 @@ +# migrate_superx.conf +# Konfiguration fuer die Migration einer bestehenden SuperX-Installation +# in eine neue Webapp-Struktur. +# +# Zielmodell: +# /home/superx/webapps/superx +# +# Optionaler Tomcat-Symlink: +# /var/lib/tomcat10/webapps/superx -> /home/superx/webapps/superx +# +# Beide Dateien muessen im selben Verzeichnis liegen: +# migrate_superx.conf +# migrate_superx_webapp.sh +# +# Aufruf: +# ./migrate_superx_webapp.sh +# +# Produktiv mit Rechte-/Owner-Anpassung meist: +# sudo ./migrate_superx_webapp.sh + +SQL_ENV="/home/superx/db/bin/SQL_ENV" + +# full = Webapp kopieren + db kopieren/verschieben +# db_only = nur db in bestehende/konfigurierte Webapp kopieren/verschieben +MIGRATION_MODE="full" + +# copy = db per rsync kopieren +# move = db per mv verschieben, Ziel-db darf noch nicht existieren +DB_TRANSFER_MODE="copy" + +# Standardziel: Webapp unter /home statt direkt unter /var/lib/tomcat*/webapps +# Bei MIGRATION_MODE=db_only kann TARGET_WEBAPP="auto" gesetzt werden, +# dann wird WEBAPP aus der geladenen SQL_ENV verwendet. +TARGET_WEBAPP="/home/superx/webapps/superx" + +# Benutzer/Gruppe fuer die Zielstruktur. +# Ersetzt die vorherigen tomcat-/superx-spezifischen Variablennamen. +WEBAPP_USER="superx" +WEBAPP_GROUP="tomcat" + +TOMCAT_SERVICE="tomcat10" +STOP_TOMCAT="false" +START_TOMCAT="false" + +# Symlink aus Tomcat-webapps auf TARGET_WEBAPP setzen. +CREATE_TOMCAT_SYMLINK="false" +TOMCAT_WEBAPPS_DIR="/var/lib/tomcat10/webapps" +TOMCAT_CONTEXT_NAME="superx" + +# Wenn unter TOMCAT_WEBAPPS_DIR/TOMCAT_CONTEXT_NAME bereits ein Symlink liegt, +# kann dieser ersetzt werden. Echte Dateien/Verzeichnisse werden nie geloescht. +REPLACE_EXISTING_SYMLINK="false" + +DELETE_TARGET="false" +ALLOW_EXISTING_TARGET="true" +ALLOW_EXISTING_TARGET_DB="true" +DRY_RUN="false" + +# WEBAPP_USER automatisch zur WEBAPP_GROUP hinzufuegen, falls noetig. +# Benoetigt root, wenn die Mitgliedschaft fehlt. +ADD_WEBAPP_USER_TO_GROUP="true" +CREATE_GROUP_IF_MISSING="false" + +UPDATE_SQL_ENV="true" +SET_RIGHTS="true" + +# auto = root setzt chown; gleicher User mit passender Gruppe ueberspringt chown +# true = chown immer versuchen +# false = kein chown +SET_OWNER="auto" +SET_CHMOD="true" + +# Sicherheitspruefung gegen erneute Migration bereits migrierter Strukturen. +FORCE_ALREADY_MIGRATED="false" + +VERBOSE="true" diff --git a/webapps_migration/migrate_superx_webapp.sh b/webapps_migration/migrate_superx_webapp.sh index 3115677..c0ffa91 100644 --- a/webapps_migration/migrate_superx_webapp.sh +++ b/webapps_migration/migrate_superx_webapp.sh @@ -9,7 +9,7 @@ set -euo pipefail SCRIPT_NAME="$(basename "$0")" -SCRIPT_VERSION="1.5" +SCRIPT_VERSION="1.6" SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" CONFIG_FILE="$SCRIPT_DIR/migrate_superx.conf" @@ -160,22 +160,53 @@ load_old_sql_env() { SOURCE_DB_REAL="$(canon_path "$SOURCE_DB")" TARGET_DB_REAL="$(canon_path "$TARGET_DB")" TARGET_WEBAPP_REAL="$(canon_path "$TARGET_WEBAPP")" + TARGET_WEBAPP_PARENT="$(dirname "$TARGET_WEBAPP")" + TARGET_WEBAPP_PARENT_REAL="$(canon_path "$TARGET_WEBAPP_PARENT")" TOMCAT_SYMLINK_PATH="$TOMCAT_WEBAPPS_DIR/$TOMCAT_CONTEXT_NAME" } normalize_owner_mode() { EFFECTIVE_SET_OWNER="$SET_OWNER" - if ! is_true "$SET_RIGHTS" || is_false "$SET_OWNER"; then EFFECTIVE_SET_OWNER="false"; return 0; fi + EFFECTIVE_SET_GROUP="false" + + if ! is_true "$SET_RIGHTS"; then + EFFECTIVE_SET_OWNER="false" + EFFECTIVE_SET_GROUP="false" + return 0 + fi + + if is_false "$SET_OWNER"; then + EFFECTIVE_SET_OWNER="false" + if current_user_matches_target; then + EFFECTIVE_SET_GROUP="true" + fi + return 0 + fi + if is_auto "$SET_OWNER"; then - if is_root; then EFFECTIVE_SET_OWNER="true"; return 0; fi + if is_root; then + EFFECTIVE_SET_OWNER="true" + EFFECTIVE_SET_GROUP="false" + return 0 + fi + if current_user_matches_target; then EFFECTIVE_SET_OWNER="false" - log "SET_OWNER=auto: chown wird uebersprungen, weil laufender User '$WEBAPP_USER' ist und zur Gruppe '$WEBAPP_GROUP' gehoert." + EFFECTIVE_SET_GROUP="true" + log "SET_OWNER=auto: chown wird uebersprungen, weil laufender User '$WEBAPP_USER' ist und zur Gruppe '$WEBAPP_GROUP' gehoert. Gruppe wird bei Bedarf per chgrp gesetzt." return 0 fi - EFFECTIVE_SET_OWNER="true"; return 0 + + EFFECTIVE_SET_OWNER="true" + EFFECTIVE_SET_GROUP="false" + return 0 + fi + + if is_true "$SET_OWNER"; then + EFFECTIVE_SET_OWNER="true" + EFFECTIVE_SET_GROUP="false" + return 0 fi - if is_true "$SET_OWNER"; then EFFECTIVE_SET_OWNER="true"; return 0; fi } root_needed_reasons() { @@ -246,6 +277,7 @@ Rechte: SET_RIGHTS = $SET_RIGHTS SET_OWNER = $SET_OWNER EFFECTIVE_SET_OWNER = $EFFECTIVE_SET_OWNER + EFFECTIVE_SET_GROUP = $EFFECTIVE_SET_GROUP SET_CHMOD = $SET_CHMOD ======================================== @@ -284,8 +316,19 @@ validate_target() { fi } -stop_tomcat_if_requested() { is_true "$STOP_TOMCAT" && run "systemctl stop '$TOMCAT_SERVICE'"; } -start_tomcat_if_requested() { is_true "$START_TOMCAT" && run "systemctl start '$TOMCAT_SERVICE'"; } +stop_tomcat_if_requested() { + if is_true "$STOP_TOMCAT"; then + command -v systemctl >/dev/null 2>&1 || fail "systemctl nicht gefunden, kann Tomcat nicht stoppen." + run "systemctl stop '$TOMCAT_SERVICE'" + fi +} + +start_tomcat_if_requested() { + if is_true "$START_TOMCAT"; then + command -v systemctl >/dev/null 2>&1 || fail "systemctl nicht gefunden, kann Tomcat nicht starten." + run "systemctl start '$TOMCAT_SERVICE'" + fi +} copy_webapp_if_requested() { [ "$MIGRATION_MODE" != "full" ] && { log "MIGRATION_MODE=db_only: Webapp wird nicht kopiert."; return 0; } @@ -359,14 +402,60 @@ PY } set_rights() { - is_true "$SET_RIGHTS" || { log "Rechte setzen ist deaktiviert."; return 0; } + if ! is_true "$SET_RIGHTS"; then + log "Rechte setzen ist deaktiviert." + return 0 + fi + [ -d "$TARGET_WEBAPP" ] || fail "TARGET_WEBAPP fehlt: $TARGET_WEBAPP" - if is_true "$EFFECTIVE_SET_OWNER"; then run "chown -R '$WEBAPP_USER:$WEBAPP_GROUP' '$TARGET_WEBAPP'"; else log "Owner/Gruppe werden nicht gesetzt."; fi + + log "Setze Rechte fuer Ziel-Webapp und Elternverzeichnis..." + + if is_true "$EFFECTIVE_SET_OWNER"; then + log "Setze Owner/Gruppe rekursiv fuer TARGET_WEBAPP..." + run "chown -R '$WEBAPP_USER:$WEBAPP_GROUP' '$TARGET_WEBAPP'" + + if [ -d "$TARGET_WEBAPP_PARENT" ]; then + log "Setze Owner/Gruppe fuer TARGET_WEBAPP_PARENT..." + run "chown '$WEBAPP_USER:$WEBAPP_GROUP' '$TARGET_WEBAPP_PARENT'" + fi + else + log "Owner wird nicht gesetzt." + + if is_true "$EFFECTIVE_SET_GROUP"; then + log "Setze Gruppe rekursiv per chgrp fuer TARGET_WEBAPP..." + run "chgrp -R '$WEBAPP_GROUP' '$TARGET_WEBAPP'" + + if [ -d "$TARGET_WEBAPP_PARENT" ]; then + log "Setze Gruppe fuer TARGET_WEBAPP_PARENT..." + run "chgrp '$WEBAPP_GROUP' '$TARGET_WEBAPP_PARENT'" + fi + fi + fi + if is_true "$SET_CHMOD"; then + log "Setze Rechte fuer TARGET_WEBAPP_PARENT..." + if [ -d "$TARGET_WEBAPP_PARENT" ]; then + run "chmod 2775 '$TARGET_WEBAPP_PARENT'" + fi + + log "Setze Verzeichnisrechte mit setgid-Bit..." run "find '$TARGET_WEBAPP' -type d -exec chmod 2775 {} +" + + log "Setze Standard-Dateirechte..." run "find '$TARGET_WEBAPP' -type f -exec chmod 664 {} +" - [ -d "$TARGET_DB/bin" ] && run "find '$TARGET_DB/bin' -type f -exec chmod 775 {} +" + + log "Setze ausfuehrbare Rechte fuer db/bin..." + if [ -d "$TARGET_DB/bin" ]; then + run "find '$TARGET_DB/bin' -type f -exec chmod 775 {} +" + else + warn "db/bin im Ziel nicht gefunden: $TARGET_DB/bin" + fi + + log "Setze ausfuehrbare Rechte fuer *.x und *.sh..." run "find '$TARGET_WEBAPP' -type f \\( -name '*.x' -o -name '*.sh' \\) -exec chmod 775 {} +" + else + log "chmod ist deaktiviert." fi } @@ -384,6 +473,7 @@ final_notes() { Migration abgeschlossen. Pruefen: + ls -ld "$TARGET_WEBAPP_PARENT" ls -ld "$TARGET_WEBAPP" ls -ld "$TARGET_DB" ls -l "$TARGET_DB/bin/SQL_ENV"