Files
Cloud-Tour-to-Libo/docker/mysql-storage-guard.sh
T

40 lines
1.4 KiB
Bash
Executable File

#!/bin/sh
set -eu
data_root="${MYSQL_DATA_PATH:-/data}"
expected_id="${MYSQL_STORAGE_ID:-}"
marker="${MYSQL_STORAGE_MARKER:-/storage-id}"
if ! printf '%s' "${expected_id}" | grep -Eq '^[A-Za-z0-9._-]{16,128}$'; then
echo "MYSQL_STORAGE_ID is missing or invalid" >&2
exit 20
fi
if [ ! -d "${data_root}" ] || [ -L "${data_root}" ]; then
echo "MySQL durable data directory is missing or is a symbolic link" >&2
exit 21
fi
if [ -L "${marker}" ] || [ ! -f "${marker}" ]; then
echo "MySQL storage identity marker is missing; refusing to initialize an empty database" >&2
exit 22
fi
actual_id="$(cat "${marker}")"
if [ "${actual_id}" != "${expected_id}" ]; then
echo "MySQL storage identity mismatch; refusing to use the mounted directory" >&2
exit 23
fi
if [ ! -w "${data_root}" ]; then
echo "MySQL durable storage is not writable by the container database user" >&2
exit 25
fi
# A marker-only directory is a valid first installation. Any other non-empty
# directory must already look like a MySQL data directory; partial or wrong
# mounts fail closed instead of being silently initialized.
first_entry="$(find "${data_root}" -mindepth 1 -maxdepth 1 -print -quit)"
if [ -n "${first_entry}" ] && { [ ! -f "${data_root}/auto.cnf" ] || [ ! -d "${data_root}/mysql" ]; }; then
echo "Mounted storage is non-empty but is not a complete MySQL data directory" >&2
exit 26
fi
echo "MySQL durable storage verified: ${expected_id}"