mirror of
https://github.com/jokob-sk/NetAlertX.git
synced 2025-12-07 09:36:05 -08:00
BE: ensure /db - reorder scripts #1327
Signed-off-by: jokob-sk <jokob.sk@gmail.com>
This commit is contained in:
@@ -1,35 +0,0 @@
|
|||||||
#!/bin/sh
|
|
||||||
# 02-ensure-folders.sh - ensure /config and /db exist under /data
|
|
||||||
|
|
||||||
set -eu
|
|
||||||
|
|
||||||
YELLOW=$(printf '\033[1;33m')
|
|
||||||
CYAN=$(printf '\033[1;36m')
|
|
||||||
RED=$(printf '\033[1;31m')
|
|
||||||
RESET=$(printf '\033[0m')
|
|
||||||
|
|
||||||
DATA_DIR=${NETALERTX_DATA:-/data}
|
|
||||||
TARGET_CONFIG=${NETALERTX_CONFIG:-${DATA_DIR}/config}
|
|
||||||
TARGET_DB=${NETALERTX_DB:-${DATA_DIR}/db}
|
|
||||||
|
|
||||||
ensure_folder() {
|
|
||||||
my_path="$1"
|
|
||||||
if [ ! -d "${my_path}" ]; then
|
|
||||||
>&2 printf "%s" "${CYAN}"
|
|
||||||
>&2 echo "Creating missing folder: ${my_path}"
|
|
||||||
>&2 printf "%s" "${RESET}"
|
|
||||||
mkdir -p "${my_path}" || {
|
|
||||||
>&2 printf "%s" "${RED}"
|
|
||||||
>&2 echo "❌ Failed to create folder: ${my_path}"
|
|
||||||
>&2 printf "%s" "${RESET}"
|
|
||||||
exit 1
|
|
||||||
}
|
|
||||||
chmod 700 "${my_path}" 2>/dev/null || true
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
# Ensure subfolders exist
|
|
||||||
ensure_folder "${TARGET_CONFIG}"
|
|
||||||
ensure_folder "${TARGET_DB}"
|
|
||||||
|
|
||||||
exit 0
|
|
||||||
@@ -1,32 +1,46 @@
|
|||||||
#!/bin/sh
|
#!/bin/sh
|
||||||
# This script checks if the database file exists, and if not, creates it with the initial schema.
|
# Ensures the database exists, or creates a new one on first run.
|
||||||
# It is intended to be run at the first start of the application.
|
# Intended to run only at initial startup.
|
||||||
|
|
||||||
# If ALWAYS_FRESH_INSTALL is true, remove the database to force a rebuild.
|
set -eu
|
||||||
if [ "${ALWAYS_FRESH_INSTALL}" = "true" ]; then
|
|
||||||
if [ -f "${NETALERTX_DB_FILE}" ]; then
|
YELLOW=$(printf '\033[1;33m')
|
||||||
# Provide feedback to the user.
|
CYAN=$(printf '\033[1;36m')
|
||||||
>&2 echo "INFO: ALWAYS_FRESH_INSTALL is true. Removing existing database to force a fresh installation."
|
RED=$(printf '\033[1;31m')
|
||||||
rm -f "${NETALERTX_DB_FILE}" "${NETALERTX_DB_FILE}-shm" "${NETALERTX_DB_FILE}-wal"
|
RESET=$(printf '\033[0m')
|
||||||
fi
|
|
||||||
# Otherwise, if the db exists, exit.
|
# Ensure DB folder exists
|
||||||
elif [ -f "${NETALERTX_DB_FILE}" ]; then
|
if [ ! -d "${NETALERTX_DB}" ]; then
|
||||||
|
mkdir -p "${NETALERTX_DB}" || {
|
||||||
|
>&2 echo "ERROR: Failed to create DB directory at ${NETALERTX_DB}"
|
||||||
|
exit 1
|
||||||
|
}
|
||||||
|
chmod 700 "${NETALERTX_DB}" 2>/dev/null || true
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Fresh rebuild requested
|
||||||
|
if [ "${ALWAYS_FRESH_INSTALL:-false}" = "true" ] && [ -f "${NETALERTX_DB_FILE}" ]; then
|
||||||
|
>&2 echo "INFO: ALWAYS_FRESH_INSTALL enabled — removing existing database."
|
||||||
|
rm -f "${NETALERTX_DB_FILE}" "${NETALERTX_DB_FILE}-shm" "${NETALERTX_DB_FILE}-wal"
|
||||||
|
fi
|
||||||
|
|
||||||
|
# If file exists now, nothing to do
|
||||||
|
if [ -f "${NETALERTX_DB_FILE}" ]; then
|
||||||
exit 0
|
exit 0
|
||||||
fi
|
fi
|
||||||
|
|
||||||
CYAN=$(printf '\033[1;36m')
|
|
||||||
RESET=$(printf '\033[0m')
|
|
||||||
>&2 printf "%s" "${CYAN}"
|
>&2 printf "%s" "${CYAN}"
|
||||||
>&2 cat <<EOF
|
>&2 cat <<EOF
|
||||||
══════════════════════════════════════════════════════════════════════════════
|
══════════════════════════════════════════════════════════════════════════════
|
||||||
🆕 First run detected. Building initial database schema in ${NETALERTX_DB_FILE}.
|
🆕 First run detected — building initial database at: ${NETALERTX_DB_FILE}
|
||||||
|
|
||||||
Do not interrupt this step. Once complete, consider backing up the fresh
|
Do not interrupt this step. When complete, consider backing up the fresh
|
||||||
database before onboarding sensitive networks.
|
DB before onboarding sensitive or critical networks.
|
||||||
══════════════════════════════════════════════════════════════════════════════
|
══════════════════════════════════════════════════════════════════════════════
|
||||||
EOF
|
EOF
|
||||||
>&2 printf "%s" "${RESET}"
|
>&2 printf "%s" "${RESET}"
|
||||||
|
|
||||||
|
|
||||||
# Write all text to db file until we see "end-of-database-schema"
|
# Write all text to db file until we see "end-of-database-schema"
|
||||||
sqlite3 "${NETALERTX_DB_FILE}" <<'end-of-database-schema'
|
sqlite3 "${NETALERTX_DB_FILE}" <<'end-of-database-schema'
|
||||||
CREATE TABLE Events (eve_MAC STRING (50) NOT NULL COLLATE NOCASE, eve_IP STRING (50) NOT NULL COLLATE NOCASE, eve_DateTime DATETIME NOT NULL, eve_EventType STRING (30) NOT NULL COLLATE NOCASE, eve_AdditionalInfo STRING (250) DEFAULT (''), eve_PendingAlertEmail BOOLEAN NOT NULL CHECK (eve_PendingAlertEmail IN (0, 1)) DEFAULT (1), eve_PairEventRowid INTEGER);
|
CREATE TABLE Events (eve_MAC STRING (50) NOT NULL COLLATE NOCASE, eve_IP STRING (50) NOT NULL COLLATE NOCASE, eve_DateTime DATETIME NOT NULL, eve_EventType STRING (30) NOT NULL COLLATE NOCASE, eve_AdditionalInfo STRING (250) DEFAULT (''), eve_PendingAlertEmail BOOLEAN NOT NULL CHECK (eve_PendingAlertEmail IN (0, 1)) DEFAULT (1), eve_PairEventRowid INTEGER);
|
||||||
|
|||||||
Reference in New Issue
Block a user