master/mariadb/tools/dbInit.sh

116 lines
4.3 KiB
Bash
Executable File

# ****************************************************************************************************
# * ______ _ _ *
# * | ____| /\ (_) | | *
# * | |__ _ _ _ __ ___ _ __ ___ __ _ _ __ / \ _ _ __ _ __ ___ _ __| |_ ___ *
# * | __|| | | | '__/ _ \| '_ \ / _ \/ _` | '_ \ / /\ \ | | '__| '_ \ / _ \| '__| __/ __| *
# * | |___| |_| | | | (_) | |_) | __/ (_| | | | | / ____ \| | | | |_) | (_) | | | |_\__ \ *
# * |______\__,_|_| \___/| .__/ \___|\__,_|_| |_| /_/ \_\_|_| | .__/ \___/|_| \__|___/ *
# * | | | | *
# * |_| |_| *
# * *
# ****************************************************************************************************
# This file is part of the European Airports Project, a free, collaborative platform of airport data
# extending beyond the official AIPs.
# Copyright (C) 2023
# Florian Meissner <florianmeissner@gmx.de>
# This program is free software: you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free Software Foundation, either
# version 3 of the License, or (at your option) any later version. This program is distributed in the
# hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details. You should have received a copy of the GNU General Public License along with this program.
# (license.md in the root folder of this project) If not, see <http://www.gnu.org/licenses/>.
# Initialize the database
# =======================
# This script will process all *.sh, *.sql, *.sql.gz, *sql.xz and *.sql.zst found in in the init
# directory recursively. They are processed in alphabetical order.
# CONFIG
LOG_FILE="/var/log/mysql/init.log"
INIT_DIR="/docker-entrypoint-initdb.d"
# Logging functions
# Inspired by MariaDB's upstream docker-entrypoint.sh
mysql_log() {
local type="$1";
shift
printf '%s [%s] [Entrypoint]: %s\n' "$(date --rfc-3339=seconds)" "$type" "$*" &>> $LOG_FILE
}
mysql_note() {
mysql_log Note "$@"
}
mysql_warn() {
mysql_log Warn "$@" >&2
}
mysql_error() {
mysql_log ERROR "$@" >&2
exit 1
}
docker_process_sql() {
user=`cat /run/secrets/rootUser`
pw=`cat /run/secrets/rootPw`
# echo "$user $pw"
# echo "$@"
mariadb \
--user="${user}" \
--password="${pw}" \
"$@"
}
# check to see if this file is being run or sourced from another script
_is_sourced() {
# https://unix.stackexchange.com/a/215279
local _top_of_stack=$(( ${#FUNCNAME[@]} - 1 ))
[ "${#FUNCNAME[@]}" -ge 2 ] \
&& [ "${FUNCNAME[0]}" = '_is_sourced' ] \
&& [ "${FUNCNAME[${_top_of_stack}]}" = 'source' ]
}
# Main program start
_main() {
for f in $(find ${INIT_DIR} -name "*.sql*" -or -name "*.sh" | sort ); do
case "$f" in
*.sh)
# https://github.com/docker-library/postgres/issues/450#issuecomment-393167936
# https://github.com/docker-library/postgres/pull/452
if [ -x "$f" ]; then
mysql_note "$0: running $f"
"$f"
else
mysql_note "$0: sourcing $f"
# ShellCheck can't follow non-constant source. Use a directive to specify location.
# shellcheck disable=SC1090
. "$f"
fi
;;
*.sql) mysql_note "$0: running $f"; docker_process_sql < "$f"; echo ;;
*.sql.gz) mysql_note "$0: running $f"; gunzip -c "$f" | docker_process_sql; echo ;;
*.sql.xz) mysql_note "$0: running $f"; xzcat "$f" | docker_process_sql; echo ;;
*.sql.zst) mysql_note "$0: running $f"; zstd -dc "$f" | docker_process_sql; echo ;;
*) mysql_warn "$0: ignoring $f" ;;
esac
done
}
if ! _is_sourced
then
_main
fi