addon-bookstack/bookstack/rootfs/etc/cont-init.d/31-mysql.sh
Paul Sinclair d6902e9d25
🎉 Inital Commit
2019-04-14 18:21:05 -04:00

46 lines
No EOL
1.5 KiB
Bash

#!/usr/bin/with-contenv bashio
# ==============================================================================
# Community Hass.io Add-ons: Bookstack
# This file initialises the MySQL database
# ==============================================================================
# Initialize the database data directory.
if ! bashio::fs.directory_exists "/data/mysql/mysql"; then
bashio::log.info "Initializing database..."
s6-setuidgid mysql mysql_install_db --datadir=/data/mysql \
# Start MySQL.
/usr/bin/mysqld_safe --datadir=/data/mysql \
--tmpdir=/tmp/ --user=mysql &
rc="$?"
pid="$!"
if [ "$rc" -ne 0 ]; then
bashio::exit.nok "Failed to start the database."
fi
# Wait until it is ready.
for _ in $(seq 1 30); do
if echo 'SELECT 1' | mysql &> /dev/null; then
break
fi
sleep 1
done
# Secure the installation.
printf '\nn\n\n\n\n\n' | /usr/bin/mysql_secure_installation
# Create the database.
bashio::log.info "Creating Database..."
echo "CREATE DATABASE IF NOT EXISTS \`bookstack\` ;" | mysql
# Create the user.
bashio::log.info "Creating User..."
echo "CREATE USER 'bookstack'@'localhost' IDENTIFIED BY 'bookstack' ;" | mysql
echo "GRANT ALL ON \`bookstack\`.* TO 'bookstack'@'localhost' ;" | mysql
# Stop the MySQL server
childpid=$(pgrep -P "$pid")
if ! kill -s TERM "$childpid" || ! wait "$pid"; then
bashio::exit.nok "Initialization of database failed."
fi
fi