Make database user / pwd detection more robust
This commit is contained in:
+61
-11
@@ -35,6 +35,13 @@ DB_NAME="${DB_NAME:-piwigo}"
|
|||||||
DB_USER="${DB_USER:-piwigo}"
|
DB_USER="${DB_USER:-piwigo}"
|
||||||
DB_PASS="${DB_PASS:-}" # Required! Get from app config or secret
|
DB_PASS="${DB_PASS:-}" # Required! Get from app config or secret
|
||||||
|
|
||||||
|
# Database container name (optional - auto-detected if not set)
|
||||||
|
DB_CONTAINER="${DB_CONTAINER:-}"
|
||||||
|
|
||||||
|
# Database password env var name in container (optional - auto-detected if not set)
|
||||||
|
# Common names: MYSQL_PASSWORD, MYSQL_ROOT_PASSWORD, MARIADB_PASSWORD, MARIADB_ROOT_PASSWORD
|
||||||
|
DB_PASSWORD_ENV="${DB_PASSWORD_ENV:-}"
|
||||||
|
|
||||||
# ---- Piwigo Data Paths (Host filesystem paths) ----
|
# ---- Piwigo Data Paths (Host filesystem paths) ----
|
||||||
# These are the dataset paths you configured in the Piwigo app settings
|
# These are the dataset paths you configured in the Piwigo app settings
|
||||||
# Example: /mnt/tank/apps/piwigo/uploads
|
# Example: /mnt/tank/apps/piwigo/uploads
|
||||||
@@ -215,7 +222,10 @@ validate_database() {
|
|||||||
if ! command -v docker >/dev/null 2>&1; then
|
if ! command -v docker >/dev/null 2>&1; then
|
||||||
die "Neither mariadb-dump/mysqldump nor docker found. Cannot dump database."
|
die "Neither mariadb-dump/mysqldump nor docker found. Cannot dump database."
|
||||||
fi
|
fi
|
||||||
local db_container=$(docker ps --format '{{.Names}}' | grep -iE '(mariadb|mysql|piwigo.*db)' | head -1 || true)
|
local db_container="${DB_CONTAINER}"
|
||||||
|
if [[ -z "${db_container}" ]]; then
|
||||||
|
db_container=$(docker ps --format '{{.Names}}' | grep -iE '(mariadb|mysql|piwigo.*db)' | head -1 || true)
|
||||||
|
fi
|
||||||
if [[ -z "${db_container}" ]]; then
|
if [[ -z "${db_container}" ]]; then
|
||||||
die "No MariaDB/MySQL container found. Cannot dump database."
|
die "No MariaDB/MySQL container found. Cannot dump database."
|
||||||
fi
|
fi
|
||||||
@@ -237,10 +247,12 @@ find_db_password() {
|
|||||||
"/mnt/apps/piwigo/.env"
|
"/mnt/apps/piwigo/.env"
|
||||||
"/etc/piwigo/db_password"
|
"/etc/piwigo/db_password"
|
||||||
"/root/.piwigo_db_password"
|
"/root/.piwigo_db_password"
|
||||||
|
"${PIWIGO_CONFIG_PATH}/.env"
|
||||||
|
"${PIWIGO_CONFIG_PATH}/db_password"
|
||||||
)
|
)
|
||||||
|
|
||||||
for file in "${possible_files[@]}"; do
|
for file in "${possible_files[@]}"; do
|
||||||
if [[ -f "${file}" ]]; then
|
if [[ -n "${file}" ]] && [[ -f "${file}" ]]; then
|
||||||
# Try to extract password from file
|
# Try to extract password from file
|
||||||
local pass=$(grep -iE '(password|passwd)' "${file}" 2>/dev/null | head -1 | sed -E 's/.*[=:]\s*//' | sed -E 's/["\'\'']//g' | xargs)
|
local pass=$(grep -iE '(password|passwd)' "${file}" 2>/dev/null | head -1 | sed -E 's/.*[=:]\s*//' | sed -E 's/["\'\'']//g' | xargs)
|
||||||
if [[ -n "${pass}" ]]; then
|
if [[ -n "${pass}" ]]; then
|
||||||
@@ -253,16 +265,48 @@ find_db_password() {
|
|||||||
|
|
||||||
# Check if there's a MariaDB container running that we can get password from
|
# Check if there's a MariaDB container running that we can get password from
|
||||||
if command -v docker >/dev/null 2>&1; then
|
if command -v docker >/dev/null 2>&1; then
|
||||||
local mariadb_container=$(docker ps --format '{{.Names}}' | grep -iE '(mariadb|mysql|piwigo.*db)' | head -1 || true)
|
local mariadb_container="${DB_CONTAINER}"
|
||||||
|
if [[ -z "${mariadb_container}" ]]; then
|
||||||
|
mariadb_container=$(docker ps --format '{{.Names}}' | grep -iE '(mariadb|mysql|piwigo.*db)' | head -1 || true)
|
||||||
|
fi
|
||||||
if [[ -n "${mariadb_container}" ]]; then
|
if [[ -n "${mariadb_container}" ]]; then
|
||||||
info "Found potential MariaDB container: ${mariadb_container}"
|
info "Found potential MariaDB container: ${mariadb_container}"
|
||||||
# Try to get password from container env
|
# Try to get password from container env - check multiple possible env var names
|
||||||
local pass=$(docker inspect "${mariadb_container}" 2>/dev/null | grep -iE '(MYSQL_PASSWORD|MARIADB_PASSWORD)' | head -1 | sed -E 's/.*"([^"]+)".*/\1/' || true)
|
local password_env_vars=(
|
||||||
if [[ -n "${pass}" ]]; then
|
"MYSQL_PASSWORD"
|
||||||
DB_PASS="${pass}"
|
"MYSQL_ROOT_PASSWORD"
|
||||||
info "Found database password from container env"
|
"MARIADB_PASSWORD"
|
||||||
return 0
|
"MARIADB_ROOT_PASSWORD"
|
||||||
|
"MYSQL_ROOT_PASSWORD_FILE"
|
||||||
|
"MARIADB_ROOT_PASSWORD_FILE"
|
||||||
|
)
|
||||||
|
|
||||||
|
# If user specified a specific env var name, check that first
|
||||||
|
if [[ -n "${DB_PASSWORD_ENV}" ]]; then
|
||||||
|
password_env_vars=("${DB_PASSWORD_ENV}" "${password_env_vars[@]}")
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
for env_var in "${password_env_vars[@]}"; do
|
||||||
|
local pass=$(docker inspect "${mariadb_container}" 2>/dev/null | grep -iE "\"${env_var}\"" | head -1 | sed -E 's/.*"([^"]+)".*/\1/' || true)
|
||||||
|
if [[ -n "${pass}" ]]; then
|
||||||
|
DB_PASS="${pass}"
|
||||||
|
info "Found database password from container env var: ${env_var}"
|
||||||
|
return 0
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
|
||||||
|
# Also check for _FILE variants (Docker secrets)
|
||||||
|
for env_var in MYSQL_PASSWORD_FILE MYSQL_ROOT_PASSWORD_FILE MARIADB_PASSWORD_FILE MARIADB_ROOT_PASSWORD_FILE; do
|
||||||
|
local file_path=$(docker inspect "${mariadb_container}" 2>/dev/null | grep -iE "\"${env_var}\"" | head -1 | sed -E 's/.*"([^"]+)".*/\1/' || true)
|
||||||
|
if [[ -n "${file_path}" ]] && [[ -f "${file_path}" ]]; then
|
||||||
|
local pass=$(cat "${file_path}" 2>/dev/null | xargs)
|
||||||
|
if [[ -n "${pass}" ]]; then
|
||||||
|
DB_PASS="${pass}"
|
||||||
|
info "Found database password from container secret file: ${env_var}"
|
||||||
|
return 0
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
done
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
@@ -316,7 +360,10 @@ backup_database() {
|
|||||||
if ! command -v docker >/dev/null 2>&1; then
|
if ! command -v docker >/dev/null 2>&1; then
|
||||||
die "Neither mariadb-dump/mysqldump nor docker found. Cannot dump database."
|
die "Neither mariadb-dump/mysqldump nor docker found. Cannot dump database."
|
||||||
fi
|
fi
|
||||||
local db_container=$(docker ps --format '{{.Names}}' | grep -iE '(mariadb|mysql|piwigo.*db)' | head -1 || true)
|
local db_container="${DB_CONTAINER}"
|
||||||
|
if [[ -z "${db_container}" ]]; then
|
||||||
|
db_container=$(docker ps --format '{{.Names}}' | grep -iE '(mariadb|mysql|piwigo.*db)' | head -1 || true)
|
||||||
|
fi
|
||||||
if [[ -z "${db_container}" ]]; then
|
if [[ -z "${db_container}" ]]; then
|
||||||
die "No MariaDB/MySQL container found. Cannot dump database."
|
die "No MariaDB/MySQL container found. Cannot dump database."
|
||||||
fi
|
fi
|
||||||
@@ -344,7 +391,10 @@ backup_database() {
|
|||||||
${dump_cmd} "${conn_params[@]}" --no-data "${DB_NAME}" | gzip -${COMPRESSION_LEVEL} > "${BACKUP_DIR}/database/piwigo_schema_${TIMESTAMP}.sql.gz"
|
${dump_cmd} "${conn_params[@]}" --no-data "${DB_NAME}" | gzip -${COMPRESSION_LEVEL} > "${BACKUP_DIR}/database/piwigo_schema_${TIMESTAMP}.sql.gz"
|
||||||
unset MYSQL_PWD
|
unset MYSQL_PWD
|
||||||
else
|
else
|
||||||
local db_container=$(docker ps --format '{{.Names}}' | grep -iE '(mariadb|mysql|piwigo.*db)' | head -1 || true)
|
local db_container="${DB_CONTAINER}"
|
||||||
|
if [[ -z "${db_container}" ]]; then
|
||||||
|
db_container=$(docker ps --format '{{.Names}}' | grep -iE '(mariadb|mysql|piwigo.*db)' | head -1 || true)
|
||||||
|
fi
|
||||||
if [[ -n "${db_container}" ]]; then
|
if [[ -n "${db_container}" ]]; then
|
||||||
docker exec "${db_container}" mariadb-dump -u "${DB_USER}" -p"${DB_PASS}" \
|
docker exec "${db_container}" mariadb-dump -u "${DB_USER}" -p"${DB_PASS}" \
|
||||||
--no-data --single-transaction --routines --triggers --events \
|
--no-data --single-transaction --routines --triggers --events \
|
||||||
|
|||||||
Reference in New Issue
Block a user