inception/srcs/requirements/mariadb/run.sh
2025-06-26 10:58:07 +02:00

68 lines
1.4 KiB
Bash
Executable file

#!/bin/sh
set -e
chmod -R +rwX /db
sql_quote()
{
echo "SELECT QUOTE(FROM_BASE64('$( echo -n "$1" | base64 )'));" | mariadb -u root -N
}
rm -f /db/.dockerhealthcheck
echo
# install database if doesn't exist
if ! [ -f /db/.installed ]
then
echo "database doesn't exist."
echo
echo ">>> clean..."
rm -rf -- $(find /db -mindepth 1 -maxdepth 1)
echo ">>> creating database..."
chmod -R 777 /db
mariadb-install-db --user=db --datadir=/db
echo
echo ">>> running mariadbd..."
DATABASE_PWD="$(cat /run/secrets/database-pwd)"
DATABASE_PWD_ROOT="$(cat /run/secrets/database-pwd-root)"
# run mariadbd and wait for it to be working
mariadbd -u root &
while ! /ismariadbrunning.sh && kill -0 $!
do sleep 1; done
if ! kill -0 $!
then
echo
echo "failed when running mariadbd :("
exit 1
fi
echo
echo ">>> configure..."
echo "
FLUSH PRIVILEGES;
CREATE DATABASE wp;
GRANT ALL PRIVILEGES ON wp.* TO db IDENTIFIED BY $( sql_quote "$DATABASE_PWD" );
ALTER USER root@localhost IDENTIFIED BY $( sql_quote "$DATABASE_PWD_ROOT" );
FLUSH PRIVILEGES;
" | mariadb -u root
kill $!
echo
echo ">>> kill mariadbd..."
>/db/.installed echo "if this file exists, that means the database is entirely installed."
echo
echo "database created!"
echo
fi
echo "to recreate a new database, remove the db volume of this compose."
echo
# run mariadb
touch /db/.dockerhealthcheck
mariadbd -u root