From c6dcb9567ca002a0b688a446947496f5b75bc057 Mon Sep 17 00:00:00 2001 From: Louis Date: Fri, 13 Nov 2026 13:37:30 +0100 Subject: [PATCH] Fix greenfield DB restore: force UTF-8 database, drop REASSIGN OWNED - Create the database with UTF8 encoding (template0, C.UTF-8) so the ICU collation natural_sort works and core tables get created - Remove REASSIGN OWNED BY postgres (fails on system objects) - Show error count and first pg_restore errors instead of the full log - Install systemd units right after the installation files Co-Authored-By: Claude Opus 5.5 --- nettmanage.py | 31 ++++++++++++++----------------- 1 file changed, 14 insertions(+), 17 deletions(-) diff --git a/nettmanage.py b/nettmanage.py index cd9acc4..7242a05 100644 --- a/nettmanage.py +++ b/nettmanage.py @@ -570,15 +570,6 @@ def restore_installation_files(extract_dir, meta, stamp, keep_current_media): return {"mode": "directory", "old_path": old_path, "new_path": netbox_path} -def postgres_query(sql, database="postgres"): - """Führt eine SQL-Abfrage als PostgreSQL-Superuser (lokal) aus.""" - return run_checked( - ["runuser", "-u", "postgres", "--", "psql", "-d", database, "-v", "ON_ERROR_STOP=1", "-tAc", sql], - "psql", - cwd="/", - ).stdout.strip() - - def restore_database_as_superuser(dump_file): """Restore über den lokalen postgres-Superuser (Neuinstallation). @@ -587,7 +578,14 @@ def restore_database_as_superuser(dump_file): user, name = CONFIG["db_user"], CONFIG["db_name"] log_info("Lösche und erstelle Datenbank neu (als postgres)...") run_checked(["runuser", "-u", "postgres", "--", "dropdb", "--if-exists", name], "dropdb", cwd="/") - run_checked(["runuser", "-u", "postgres", "--", "createdb", "-O", user, name], "createdb", cwd="/") + # Explizit UTF-8: Frische Cluster laufen oft mit SQL_ASCII, womit NetBox' ICU-Collation + # "natural_sort" nicht funktioniert und zentrale Tabellen nicht angelegt werden. + run_checked( + ["runuser", "-u", "postgres", "--", "createdb", "-O", user, "-E", "UTF8", "-T", "template0", + "--lc-collate=C.UTF-8", "--lc-ctype=C.UTF-8", name], + "createdb", + cwd="/", + ) log_info("Importiere Dump...") # Dump per stdin übergeben, da das Temp-Verzeichnis für postgres nicht lesbar ist. @@ -603,9 +601,9 @@ def restore_database_as_superuser(dump_file): check=False, ) if proc.returncode != 0: - log_warn(f"pg_restore wurde mit Code {proc.returncode} beendet:\n{proc.stderr.strip()}") - # Sicherheitshalber alles an db_user übertragen. - postgres_query(f"REASSIGN OWNED BY postgres TO {sql_identifier(user)}", database=name) + errors = [line for line in proc.stderr.splitlines() if "error:" in line or "ERROR:" in line] + log_warn(f"pg_restore wurde mit Code {proc.returncode} beendet ({len(errors)} Fehlerzeilen).") + log_warn("Die ersten Fehler (meist die eigentliche Ursache):\n" + "\n".join(errors[:10])) def verify_restored_database(): @@ -1002,6 +1000,9 @@ def execute_restore(components=None, greenfield=None): log_success(f"Installation wiederhergestellt unter: {file_switch['new_path']}") if file_switch.get("old_path"): log_info(f" -> Bisherige Installation verbleibt unter: {file_switch['old_path']}") + if greenfield: + log_info("Schritt 3a+: Richte systemd-Dienste ein...") + greenfield_install_services() if "db" in selected: log_info("Schritt 3b: Stelle PostgreSQL-Datenbank wieder her...") @@ -1014,10 +1015,6 @@ def execute_restore(components=None, greenfield=None): log_info("Schritt 3c: Stelle Media-Dateien wieder her...") restore_media(os.path.join(tmp_extract_dir, "media")) - if greenfield: - log_info("Schritt 3d: Richte systemd-Dienste ein...") - greenfield_install_services() - if "db" in selected or "files" in selected: log_info("Schritt 4: Führe NetBox upgrade.sh aus (venv, Migrationen, Static Files)...") run_netbox_upgrade_script()