From f1ab5f46a1caf42eef139d8926282e6b60f97538 Mon Sep 17 00:00:00 2001 From: Alexandre <44178713+alexbelgium@users.noreply.github.com> Date: Sat, 3 Jan 2026 10:06:16 +0100 Subject: [PATCH 1/3] Handle Zoneminder DB migration and bump version --- zoneminder/CHANGELOG.md | 4 ++ zoneminder/config.yaml | 2 +- .../rootfs/etc/cont-init.d/00-folders.sh | 5 ++- zoneminder/rootfs/etc/cont-init.d/99-run.sh | 44 +++++++++++++++++-- 4 files changed, 48 insertions(+), 7 deletions(-) diff --git a/zoneminder/CHANGELOG.md b/zoneminder/CHANGELOG.md index 0c002c21e..5d74d6370 100644 --- a/zoneminder/CHANGELOG.md +++ b/zoneminder/CHANGELOG.md @@ -1,4 +1,8 @@ +## 1.36.37-2 (03-01-2026) +- Fix MariaDB addon defaults and migrate legacy database name when detected +- Ensure MySQL data directory is created with correct ownership + ## 1.36.37 (23-12-2025) - Update to latest version from zoneminder-containers/zoneminder-base (changelog : https://github.com/zoneminder-containers/zoneminder-base/releases) - The Home Assistant project has deprecated support for the armv7, armhf and i386 architectures. Support wil be fully dropped in the upcoming Home Assistant 2025.12 release diff --git a/zoneminder/config.yaml b/zoneminder/config.yaml index e7ac722af..b3e1ab463 100644 --- a/zoneminder/config.yaml +++ b/zoneminder/config.yaml @@ -95,5 +95,5 @@ services: slug: zoneminder udev: true url: https://github.com/alexbelgium/hassio-addons -version: "1.36.37" +version: "1.36.37-2" webui: "[PROTO:ssl]://[HOST]:[PORT:80]/zm" diff --git a/zoneminder/rootfs/etc/cont-init.d/00-folders.sh b/zoneminder/rootfs/etc/cont-init.d/00-folders.sh index f8c9cf508..925af42b3 100755 --- a/zoneminder/rootfs/etc/cont-init.d/00-folders.sh +++ b/zoneminder/rootfs/etc/cont-init.d/00-folders.sh @@ -19,11 +19,12 @@ if [ ! -d "$CONFIGSOURCE" ]; then mkdir "$CONFIGSOURCE"; fi if [ ! -d "$CONFIGSOURCE"/events ]; then mkdir "$CONFIGSOURCE"/events; fi if [ ! -d "$CONFIGSOURCE"/sounds ]; then mkdir "$CONFIGSOURCE"/sounds; fi if [ ! -d "$IMAGESOURCE" ]; then mkdir "$IMAGESOURCE"; fi +if [ ! -d "$CONFIGSOURCE"/mysql ]; then mkdir "$CONFIGSOURCE"/mysql; fi # Make sure permissions are right echo "... checking permissions" -chown -R "$(id -u):$(id -g)" "$CONFIGSOURCE" -chown -R "$(id -u):$(id -g)" "$IMAGESOURCE" +chown -R "$(id -u):$(id -g)" "$CONFIGSOURCE"/events "$CONFIGSOURCE"/sounds "$IMAGESOURCE" +chown -R mysql:mysql "$CONFIGSOURCE"/mysql # Make symlinks echo "... making symlinks" diff --git a/zoneminder/rootfs/etc/cont-init.d/99-run.sh b/zoneminder/rootfs/etc/cont-init.d/99-run.sh index 764ab4244..e6fd6d17e 100755 --- a/zoneminder/rootfs/etc/cont-init.d/99-run.sh +++ b/zoneminder/rootfs/etc/cont-init.d/99-run.sh @@ -35,7 +35,7 @@ case "$(bashio::config "DB_CONNECTION")" in DB_CONNECTION=mysql ZM_DB_HOST=$(bashio::services "mysql" "host") ZM_DB_PORT=$(bashio::services "mysql" "port") - ZM_DB_NAME=firefly + ZM_DB_NAME=zm ZM_DB_USER=$(bashio::services "mysql" "username") ZM_DB_PASS=$(bashio::services "mysql" "password") export DB_CONNECTION @@ -46,15 +46,51 @@ case "$(bashio::config "DB_CONNECTION")" in export ZM_DB_USER && bashio::log.blue "ZM_DB_USER=$ZM_DB_USER" export ZM_DB_PASS && bashio::log.blue "ZM_DB_PASS=$ZM_DB_PASS" - bashio::log.warning "Firefly-iii is using the Maria DB addon" + bashio::log.warning "Zoneminder is using the MariaDB addon" bashio::log.warning "Please ensure this is included in your backups" bashio::log.warning "Uninstalling the MariaDB addon will remove any data" - bashio::log.info "Creating database for Firefly-iii if required" + bashio::log.info "Creating database for Zoneminder if required" mysql \ -u "${ZM_DB_USER}" -p"${ZM_DB_PASS}" \ -h "${ZM_DB_HOST}" -P "${ZM_DB_PORT}" \ - -e "CREATE DATABASE IF NOT EXISTS \`firefly\` ;" + -e "CREATE DATABASE IF NOT EXISTS \`${ZM_DB_NAME}\` ;" + + legacy_db=$(mysql \ + -u "${ZM_DB_USER}" -p"${ZM_DB_PASS}" \ + -h "${ZM_DB_HOST}" -P "${ZM_DB_PORT}" \ + --batch --skip-column-names \ + -e "SHOW DATABASES LIKE 'firefly';" || true) + if [ -n "$legacy_db" ]; then + target_db=$(mysql \ + -u "${ZM_DB_USER}" -p"${ZM_DB_PASS}" \ + -h "${ZM_DB_HOST}" -P "${ZM_DB_PORT}" \ + --batch --skip-column-names \ + -e "SHOW DATABASES LIKE '${ZM_DB_NAME}';" || true) + if [ -z "$target_db" ]; then + bashio::log.warning "Detected legacy database 'firefly'. Attempting migration to '${ZM_DB_NAME}'." + mysql \ + -u "${ZM_DB_USER}" -p"${ZM_DB_PASS}" \ + -h "${ZM_DB_HOST}" -P "${ZM_DB_PORT}" \ + -e "CREATE DATABASE IF NOT EXISTS \`${ZM_DB_NAME}\` ;" + if command -v mysqldump >/dev/null 2>&1; then + if mysqldump \ + -u "${ZM_DB_USER}" -p"${ZM_DB_PASS}" \ + -h "${ZM_DB_HOST}" -P "${ZM_DB_PORT}" \ + --routines --events --triggers \ + firefly | mysql \ + -u "${ZM_DB_USER}" -p"${ZM_DB_PASS}" \ + -h "${ZM_DB_HOST}" -P "${ZM_DB_PORT}" \ + "${ZM_DB_NAME}"; then + bashio::log.info "Legacy database migration completed." + else + bashio::log.warning "Legacy database migration failed; please migrate manually." + fi + else + bashio::log.warning "mysqldump not available; please migrate manually." + fi + fi + fi ;; # Use remote From ae34ee80cb7a207e66f7caaacd253a61bf8a817f Mon Sep 17 00:00:00 2001 From: Alexandre <44178713+alexbelgium@users.noreply.github.com> Date: Sat, 3 Jan 2026 11:46:58 +0100 Subject: [PATCH 2/3] Change shebang to use env for bashio --- zoneminder/rootfs/etc/cont-init.d/99-run.sh | 196 +++++++++++++------- 1 file changed, 129 insertions(+), 67 deletions(-) diff --git a/zoneminder/rootfs/etc/cont-init.d/99-run.sh b/zoneminder/rootfs/etc/cont-init.d/99-run.sh index e6fd6d17e..c56d148a9 100755 --- a/zoneminder/rootfs/etc/cont-init.d/99-run.sh +++ b/zoneminder/rootfs/etc/cont-init.d/99-run.sh @@ -1,6 +1,6 @@ #!/usr/bin/env bashio # shellcheck shell=bash -set -e +set -euo pipefail # hadolint ignore=SC2155 ################# @@ -8,10 +8,11 @@ set -e ################# CONFIGSOURCE="/config/addons_config/zoneminder" -if [ ! -f "$CONFIGSOURCE"/zm.conf ]; then +mkdir -p "$CONFIGSOURCE" - # Copy conf files - cp /etc/zm/zm.conf "$CONFIGSOURCE" +if [ ! -f "$CONFIGSOURCE/zm.conf" ]; then + # Copy conf file on first run + cp -f /etc/zm/zm.conf "$CONFIGSOURCE/zm.conf" fi ################### @@ -19,95 +20,157 @@ fi ################### bashio::log.info "Defining database" -case "$(bashio::config "DB_CONNECTION")" in - # Use MariaDB +case "$(bashio::config "DB_CONNECTION")" in mariadb_addon) - bashio::log.info "Using MariaDB addon. Requirements : running MariaDB addon. Detecting values..." + bashio::log.info "Using MariaDB addon. Detecting values..." + if ! bashio::services.available 'mysql'; then - bashio::log.fatal \ - "Local database access should be provided by the MariaDB addon" - bashio::exit.nok \ - "Please ensure it is installed and started" + bashio::log.fatal "Local database access should be provided by the MariaDB addon" + bashio::exit.nok "Please ensure it is installed and started" fi - # Use values - DB_CONNECTION=mysql - ZM_DB_HOST=$(bashio::services "mysql" "host") - ZM_DB_PORT=$(bashio::services "mysql" "port") - ZM_DB_NAME=zm - ZM_DB_USER=$(bashio::services "mysql" "username") - ZM_DB_PASS=$(bashio::services "mysql" "password") - export DB_CONNECTION - export remoteDB=1 - export ZM_DB_HOST && bashio::log.blue "ZM_DB_HOST=$ZM_DB_HOST" - export ZM_DB_PORT && bashio::log.blue "ZM_DB_PORT=$ZM_DB_PORT" - export ZM_DB_NAME && bashio::log.blue "ZM_DB_NAME=$ZM_DB_NAME" - export ZM_DB_USER && bashio::log.blue "ZM_DB_USER=$ZM_DB_USER" - export ZM_DB_PASS && bashio::log.blue "ZM_DB_PASS=$ZM_DB_PASS" + # Use values from MariaDB service + DB_CONNECTION="mysql" + remoteDB="1" + ZM_DB_HOST="$(bashio::services "mysql" "host")" + ZM_DB_PORT="$(bashio::services "mysql" "port")" + ZM_DB_NAME="zm" + ZM_DB_USER="$(bashio::services "mysql" "username")" + ZM_DB_PASS="$(bashio::services "mysql" "password")" + export DB_CONNECTION remoteDB ZM_DB_HOST ZM_DB_PORT ZM_DB_NAME ZM_DB_USER ZM_DB_PASS - bashio::log.warning "Zoneminder is using the MariaDB addon" + # DO NOT log passwords + bashio::log.blue "ZM_DB_HOST=${ZM_DB_HOST}" + bashio::log.blue "ZM_DB_PORT=${ZM_DB_PORT}" + bashio::log.blue "ZM_DB_NAME=${ZM_DB_NAME}" + bashio::log.blue "ZM_DB_USER=${ZM_DB_USER}" + + bashio::log.warning "ZoneMinder is using the MariaDB addon" bashio::log.warning "Please ensure this is included in your backups" bashio::log.warning "Uninstalling the MariaDB addon will remove any data" - bashio::log.info "Creating database for Zoneminder if required" - mysql \ - -u "${ZM_DB_USER}" -p"${ZM_DB_PASS}" \ - -h "${ZM_DB_HOST}" -P "${ZM_DB_PORT}" \ - -e "CREATE DATABASE IF NOT EXISTS \`${ZM_DB_NAME}\` ;" + # Common mysql invocation (batch + no headers) + mysql_base=( + mysql + -u "${ZM_DB_USER}" + -p"${ZM_DB_PASS}" + -h "${ZM_DB_HOST}" + -P "${ZM_DB_PORT}" + --batch + --skip-column-names + ) - legacy_db=$(mysql \ - -u "${ZM_DB_USER}" -p"${ZM_DB_PASS}" \ - -h "${ZM_DB_HOST}" -P "${ZM_DB_PORT}" \ - --batch --skip-column-names \ - -e "SHOW DATABASES LIKE 'firefly';" || true) + is_likely_zoneminder_db() { + # Returns 0 if DB looks like ZoneMinder, 1 otherwise + local db="$1" + + # Strict requirement: these two tables are very characteristic for ZM + local required_count + required_count="$("${mysql_base[@]}" -e \ + "SELECT COUNT(*) FROM information_schema.tables + WHERE table_schema='${db}' + AND LOWER(table_name) IN ('config','monitors');" \ + 2>/dev/null || echo 0)" + + # Firefly III-ish signature tables (heuristic) + local firefly_count + firefly_count="$("${mysql_base[@]}" -e \ + "SELECT COUNT(*) FROM information_schema.tables + WHERE table_schema='${db}' + AND LOWER(table_name) IN ( + 'accounts','transactions','transaction_journals','categories', + 'budgets','bills','tags','piggy_banks','rules','rule_groups' + );" \ + 2>/dev/null || echo 0)" + + # Must have BOTH required ZM tables and none of the Firefly signature tables + [ "${required_count:-0}" -ge 2 ] && [ "${firefly_count:-0}" -eq 0 ] + } + + bashio::log.info "Creating database for ZoneMinder if required" + "${mysql_base[@]}" -e "CREATE DATABASE IF NOT EXISTS \`${ZM_DB_NAME}\`;" + + # --- Legacy fix: previous buggy addon used DB name 'firefly' --- + LEGACY_DB_NAME="firefly" + + legacy_db="$("${mysql_base[@]}" -e "SHOW DATABASES LIKE '${LEGACY_DB_NAME}';" || true)" if [ -n "$legacy_db" ]; then - target_db=$(mysql \ - -u "${ZM_DB_USER}" -p"${ZM_DB_PASS}" \ - -h "${ZM_DB_HOST}" -P "${ZM_DB_PORT}" \ - --batch --skip-column-names \ - -e "SHOW DATABASES LIKE '${ZM_DB_NAME}';" || true) - if [ -z "$target_db" ]; then - bashio::log.warning "Detected legacy database 'firefly'. Attempting migration to '${ZM_DB_NAME}'." - mysql \ - -u "${ZM_DB_USER}" -p"${ZM_DB_PASS}" \ - -h "${ZM_DB_HOST}" -P "${ZM_DB_PORT}" \ - -e "CREATE DATABASE IF NOT EXISTS \`${ZM_DB_NAME}\` ;" - if command -v mysqldump >/dev/null 2>&1; then - if mysqldump \ - -u "${ZM_DB_USER}" -p"${ZM_DB_PASS}" \ - -h "${ZM_DB_HOST}" -P "${ZM_DB_PORT}" \ - --routines --events --triggers \ - firefly | mysql \ + # First: verify legacy DB looks like ZoneMinder, not an actual Firefly DB + if ! is_likely_zoneminder_db "$LEGACY_DB_NAME"; then + bashio::log.warning "Legacy database '${LEGACY_DB_NAME}' exists but does NOT look like ZoneMinder. Skipping migration to avoid touching a real Firefly database." + else + # Second: migrate only if target appears empty and legacy has data + target_tables="$("${mysql_base[@]}" -e \ + "SELECT COUNT(*) FROM information_schema.tables WHERE table_schema='${ZM_DB_NAME}';" \ + 2>/dev/null || echo 0)" + legacy_tables="$("${mysql_base[@]}" -e \ + "SELECT COUNT(*) FROM information_schema.tables WHERE table_schema='${LEGACY_DB_NAME}';" \ + 2>/dev/null || echo 0)" + + if [ "${target_tables:-0}" -eq 0 ] && [ "${legacy_tables:-0}" -gt 0 ]; then + bashio::log.warning "Detected legacy ZoneMinder DB named '${LEGACY_DB_NAME}'. Migrating to '${ZM_DB_NAME}'..." + + dump_bin="" + if command -v mysqldump >/dev/null 2>&1; then + dump_bin="mysqldump" + elif command -v mariadb-dump >/dev/null 2>&1; then + dump_bin="mariadb-dump" + fi + + if [ -z "$dump_bin" ]; then + bashio::log.warning "mysqldump/mariadb-dump not available; please migrate manually." + else + if "$dump_bin" \ -u "${ZM_DB_USER}" -p"${ZM_DB_PASS}" \ -h "${ZM_DB_HOST}" -P "${ZM_DB_PORT}" \ - "${ZM_DB_NAME}"; then - bashio::log.info "Legacy database migration completed." - else - bashio::log.warning "Legacy database migration failed; please migrate manually." + --routines --events --triggers \ + "${LEGACY_DB_NAME}" | \ + mysql \ + -u "${ZM_DB_USER}" -p"${ZM_DB_PASS}" \ + -h "${ZM_DB_HOST}" -P "${ZM_DB_PORT}" \ + "${ZM_DB_NAME}"; then + bashio::log.info "Legacy database migration completed." + bashio::log.warning "Optional: you may now drop '${LEGACY_DB_NAME}' manually if desired." + else + bashio::log.warning "Legacy database migration failed; please migrate manually." + fi fi else - bashio::log.warning "mysqldump not available; please migrate manually." + bashio::log.info "Legacy DB '${LEGACY_DB_NAME}' found but migration skipped (target not empty or legacy empty)." fi fi fi ;; - # Use remote external) - bashio::log.info "Using remote database. Requirement : filling all addon options fields, and making sure the database already exists" - for conditions in "ZM_DB_HOST" "ZM_DB_PORT" "ZM_DB_NAME" "ZM_DB_USER" "ZM_DB_PASS"; do - if ! bashio::config.has_value "$conditions"; then - bashio::exit.nok "Remote database has been specified but $conditions is not defined in addon options" + bashio::log.info "Using remote database. Requirement: filling all addon options fields, and making sure the database already exists" + + for key in "ZM_DB_HOST" "ZM_DB_PORT" "ZM_DB_NAME" "ZM_DB_USER" "ZM_DB_PASS"; do + if ! bashio::config.has_value "$key"; then + bashio::exit.nok "Remote database has been specified but $key is not defined in addon options" fi done + + DB_CONNECTION="mysql" + remoteDB="1" + ZM_DB_HOST="$(bashio::config "ZM_DB_HOST")" + ZM_DB_PORT="$(bashio::config "ZM_DB_PORT")" + ZM_DB_NAME="$(bashio::config "ZM_DB_NAME")" + ZM_DB_USER="$(bashio::config "ZM_DB_USER")" + ZM_DB_PASS="$(bashio::config "ZM_DB_PASS")" + export DB_CONNECTION remoteDB ZM_DB_HOST ZM_DB_PORT ZM_DB_NAME ZM_DB_USER ZM_DB_PASS + + bashio::log.blue "ZM_DB_HOST=${ZM_DB_HOST}" + bashio::log.blue "ZM_DB_PORT=${ZM_DB_PORT}" + bashio::log.blue "ZM_DB_NAME=${ZM_DB_NAME}" + bashio::log.blue "ZM_DB_USER=${ZM_DB_USER}" + # DO NOT log passwords ;; - # Use remote *) bashio::log.info "Using internal database" ;; - esac ############## @@ -115,5 +178,4 @@ esac ############## bashio::log.info "Please wait while the app is loading !" - -./usr/local/bin/entrypoint.sh +/./usr/local/bin/entrypoint.sh From 3e5e62b63b5781534026a11b9d71c1132000c193 Mon Sep 17 00:00:00 2001 From: Alexandre <44178713+alexbelgium@users.noreply.github.com> Date: Sat, 3 Jan 2026 12:38:38 +0100 Subject: [PATCH 3/3] Refactor 99-run.sh for better database handling Updated the script to use 'with-contenv' for bashio and improved database migration logic. --- zoneminder/rootfs/etc/cont-init.d/99-run.sh | 145 ++++++++++++-------- 1 file changed, 89 insertions(+), 56 deletions(-) diff --git a/zoneminder/rootfs/etc/cont-init.d/99-run.sh b/zoneminder/rootfs/etc/cont-init.d/99-run.sh index c56d148a9..701fb6a03 100755 --- a/zoneminder/rootfs/etc/cont-init.d/99-run.sh +++ b/zoneminder/rootfs/etc/cont-init.d/99-run.sh @@ -1,4 +1,4 @@ -#!/usr/bin/env bashio +#!/usr/bin/with-contenv bashio # shellcheck shell=bash set -euo pipefail # hadolint ignore=SC2155 @@ -11,7 +11,6 @@ CONFIGSOURCE="/config/addons_config/zoneminder" mkdir -p "$CONFIGSOURCE" if [ ! -f "$CONFIGSOURCE/zm.conf" ]; then - # Copy conf file on first run cp -f /etc/zm/zm.conf "$CONFIGSOURCE/zm.conf" fi @@ -30,7 +29,6 @@ case "$(bashio::config "DB_CONNECTION")" in bashio::exit.nok "Please ensure it is installed and started" fi - # Use values from MariaDB service DB_CONNECTION="mysql" remoteDB="1" ZM_DB_HOST="$(bashio::services "mysql" "host")" @@ -61,21 +59,42 @@ case "$(bashio::config "DB_CONNECTION")" in --skip-column-names ) + db_exists() { + local db="$1" + local out + out="$("${mysql_base[@]}" -e \ + "SELECT schema_name FROM information_schema.schemata WHERE schema_name='${db}';" \ + 2>/dev/null || true)" + [ -n "$out" ] + } + + table_count() { + local db="$1" + # If schema doesn't exist, count should be 0 + "${mysql_base[@]}" -e \ + "SELECT COUNT(*) FROM information_schema.tables WHERE table_schema='${db}';" \ + 2>/dev/null || echo 0 + } + is_likely_zoneminder_db() { # Returns 0 if DB looks like ZoneMinder, 1 otherwise local db="$1" - # Strict requirement: these two tables are very characteristic for ZM - local required_count - required_count="$("${mysql_base[@]}" -e \ + if ! db_exists "$db"; then + return 1 + fi + + # Strong ZoneMinder signature: Config + Monitors (required) + local zm_required + zm_required="$("${mysql_base[@]}" -e \ "SELECT COUNT(*) FROM information_schema.tables WHERE table_schema='${db}' AND LOWER(table_name) IN ('config','monitors');" \ 2>/dev/null || echo 0)" - # Firefly III-ish signature tables (heuristic) - local firefly_count - firefly_count="$("${mysql_base[@]}" -e \ + # Firefly-ish signature tables (heuristic blacklist) + local ff_sig + ff_sig="$("${mysql_base[@]}" -e \ "SELECT COUNT(*) FROM information_schema.tables WHERE table_schema='${db}' AND LOWER(table_name) IN ( @@ -84,63 +103,77 @@ case "$(bashio::config "DB_CONNECTION")" in );" \ 2>/dev/null || echo 0)" - # Must have BOTH required ZM tables and none of the Firefly signature tables - [ "${required_count:-0}" -ge 2 ] && [ "${firefly_count:-0}" -eq 0 ] + [ "${zm_required:-0}" -ge 2 ] && [ "${ff_sig:-0}" -eq 0 ] } - bashio::log.info "Creating database for ZoneMinder if required" - "${mysql_base[@]}" -e "CREATE DATABASE IF NOT EXISTS \`${ZM_DB_NAME}\`;" + create_db_if_missing() { + local db="$1" + "${mysql_base[@]}" -e "CREATE DATABASE IF NOT EXISTS \`${db}\`;" >/dev/null + } # --- Legacy fix: previous buggy addon used DB name 'firefly' --- LEGACY_DB_NAME="firefly" + need_migration="0" - legacy_db="$("${mysql_base[@]}" -e "SHOW DATABASES LIKE '${LEGACY_DB_NAME}';" || true)" - if [ -n "$legacy_db" ]; then - # First: verify legacy DB looks like ZoneMinder, not an actual Firefly DB - if ! is_likely_zoneminder_db "$LEGACY_DB_NAME"; then - bashio::log.warning "Legacy database '${LEGACY_DB_NAME}' exists but does NOT look like ZoneMinder. Skipping migration to avoid touching a real Firefly database." - else - # Second: migrate only if target appears empty and legacy has data - target_tables="$("${mysql_base[@]}" -e \ - "SELECT COUNT(*) FROM information_schema.tables WHERE table_schema='${ZM_DB_NAME}';" \ - 2>/dev/null || echo 0)" - legacy_tables="$("${mysql_base[@]}" -e \ - "SELECT COUNT(*) FROM information_schema.tables WHERE table_schema='${LEGACY_DB_NAME}';" \ - 2>/dev/null || echo 0)" + if db_exists "$LEGACY_DB_NAME"; then + if is_likely_zoneminder_db "$LEGACY_DB_NAME"; then + legacy_tables="$(table_count "$LEGACY_DB_NAME")" + target_tables="0" + if db_exists "$ZM_DB_NAME"; then + target_tables="$(table_count "$ZM_DB_NAME")" + fi - if [ "${target_tables:-0}" -eq 0 ] && [ "${legacy_tables:-0}" -gt 0 ]; then - bashio::log.warning "Detected legacy ZoneMinder DB named '${LEGACY_DB_NAME}'. Migrating to '${ZM_DB_NAME}'..." - - dump_bin="" - if command -v mysqldump >/dev/null 2>&1; then - dump_bin="mysqldump" - elif command -v mariadb-dump >/dev/null 2>&1; then - dump_bin="mariadb-dump" - fi - - if [ -z "$dump_bin" ]; then - bashio::log.warning "mysqldump/mariadb-dump not available; please migrate manually." - else - if "$dump_bin" \ - -u "${ZM_DB_USER}" -p"${ZM_DB_PASS}" \ - -h "${ZM_DB_HOST}" -P "${ZM_DB_PORT}" \ - --routines --events --triggers \ - "${LEGACY_DB_NAME}" | \ - mysql \ - -u "${ZM_DB_USER}" -p"${ZM_DB_PASS}" \ - -h "${ZM_DB_HOST}" -P "${ZM_DB_PORT}" \ - "${ZM_DB_NAME}"; then - bashio::log.info "Legacy database migration completed." - bashio::log.warning "Optional: you may now drop '${LEGACY_DB_NAME}' manually if desired." - else - bashio::log.warning "Legacy database migration failed; please migrate manually." - fi - fi + # Only migrate if: + # - legacy has data + # - target has 0 tables (either missing or empty) + if [ "${legacy_tables:-0}" -gt 0 ] && [ "${target_tables:-0}" -eq 0 ]; then + need_migration="1" else - bashio::log.info "Legacy DB '${LEGACY_DB_NAME}' found but migration skipped (target not empty or legacy empty)." + bashio::log.info "Legacy DB '${LEGACY_DB_NAME}' detected but migration skipped (target not empty or legacy empty)." + fi + else + bashio::log.warning "Database '${LEGACY_DB_NAME}' exists but does NOT look like ZoneMinder. Skipping migration to avoid touching a real Firefly database." + fi + fi + + # IMPORTANT: do NOT pre-create target DB before deciding on migration. + # Create target only when needed (migration) and finally ensure it exists for normal start. + + if [ "$need_migration" = "1" ]; then + bashio::log.warning "Detected legacy ZoneMinder DB named '${LEGACY_DB_NAME}'. Migrating to '${ZM_DB_NAME}'..." + + create_db_if_missing "$ZM_DB_NAME" + + dump_bin="" + if command -v mysqldump >/dev/null 2>&1; then + dump_bin="mysqldump" + elif command -v mariadb-dump >/dev/null 2>&1; then + dump_bin="mariadb-dump" + fi + + if [ -z "$dump_bin" ]; then + bashio::log.warning "mysqldump/mariadb-dump not available; please migrate manually." + else + if "$dump_bin" \ + -u "${ZM_DB_USER}" -p"${ZM_DB_PASS}" \ + -h "${ZM_DB_HOST}" -P "${ZM_DB_PORT}" \ + --routines --events --triggers \ + "${LEGACY_DB_NAME}" | \ + mysql \ + -u "${ZM_DB_USER}" -p"${ZM_DB_PASS}" \ + -h "${ZM_DB_HOST}" -P "${ZM_DB_PORT}" \ + "${ZM_DB_NAME}"; then + bashio::log.info "Legacy database migration completed." + bashio::log.warning "Optional: you may now drop '${LEGACY_DB_NAME}' manually if desired." + else + bashio::log.warning "Legacy database migration failed; please migrate manually." fi fi fi + + # Ensure target DB exists for ZoneMinder startup (after migration decision) + bashio::log.info "Ensuring ZoneMinder database '${ZM_DB_NAME}' exists" + create_db_if_missing "$ZM_DB_NAME" ;; external) @@ -178,4 +211,4 @@ esac ############## bashio::log.info "Please wait while the app is loading !" -/./usr/local/bin/entrypoint.sh +exec /usr/local/bin/entrypoint.sh