Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion proxy-manager/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"webui": "http://[HOST]:[PORT:81]",
"startup": "application",
"arch": ["aarch64", "amd64", "armhf", "armv7", "i386"],
"services": ["mysql:need"],
"ports": {
"80": 80,
"81": 81,
Expand All @@ -20,7 +21,7 @@
"boot": "auto",
"hassio_api": true,
"hassio_role": "default",
"map": ["ssl:rw"],
"map": ["ssl:rw", "backup:rw"],
"options": {},
"schema": {
"log_level": "list(trace|debug|info|notice|warning|error|fatal)?"
Expand Down
70 changes: 40 additions & 30 deletions proxy-manager/rootfs/etc/cont-init.d/mysql.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,45 @@
# Home Assistant Community Add-on: Nginx Proxy Manager
# This file init the MySQL database
# ==============================================================================
declare host
declare password
declare port
declare username
declare backup

# Initialize the database data directory.
if ! bashio::fs.directory_exists "/data/mysql"; then
mkdir /data/mysql
chown mysql:mysql /data/mysql
# Require MySQL service to be available
if ! bashio::services.available "mysql"; then
bashio::log.error \
"This add-on now requires the MariaDB core add-on 2.0 or newer!"
bashio::exit.nok \
"Make sure the MariaDB add-on is installed and running"
fi

if ! bashio::fs.directory_exists "/data/mysql/mysql"; then
host=$(bashio::services "mysql" "host")
password=$(bashio::services "mysql" "password")
port=$(bashio::services "mysql" "port")
username=$(bashio::services "mysql" "username")

bashio::log.info "Initializing database..."
# Create database if not exists
echo "CREATE DATABASE IF NOT EXISTS nginxproxymanager;" \
| mysql -h "${host}" -P "${port}" -u "${username}" -p"${password}"

s6-setuidgid mysql mysql_install_db --datadir=/data/mysql
# Check if older MySQL folder exists
if bashio::fs.directory_exists "/data/mysql"; then
bashio::log.info "Found previous MySQL database, starting migration..."

backup="/backup/nginx-proxy-manager-$(date +%F).sql"
bashio::log.info "A backup of your database will be stored in: ${backup}"

# Start internal MySQL server temperorary
bashio::log.info "Initializing previous database..."

# Start MySQL.
s6-setuidgid mysql /usr/bin/mysqld --datadir /data/mysql --tmpdir /tmp/ &
rc="$?"
pid="$!"
if [ "$rc" -ne 0 ]; then
bashio::exit.nok "Failed to start the database."
bashio::exit.nok "Failed to start the previous database."
fi

# Wait until it is ready.
Expand All @@ -32,20 +52,6 @@ if ! bashio::fs.directory_exists "/data/mysql/mysql"; then
sleep 1
done

# Secure the installation.
mysql <<-EOSQL
SET @@SESSION.SQL_LOG_BIN=0;

DELETE FROM
mysql.user
WHERE
user NOT IN ('mysql.sys', 'mysqlxsys', 'root', 'mysql')
OR host NOT IN ('localhost');

DROP DATABASE IF EXISTS test;
FLUSH PRIVILEGES;
EOSQL

# Check data integrity and fix corruptions.
mysqlcheck \
--no-defaults \
Expand Down Expand Up @@ -74,15 +80,19 @@ EOSQL
> /dev/null \
|| true

# Create the database.
echo "CREATE DATABASE IF NOT EXISTS \`nginxproxymanager\` ;" | mysql
# Dump current database into backups folder
bashio::log.info "Backing up previous database..."

# Create the user.
echo "CREATE USER 'nginxproxymanager'@'%' IDENTIFIED BY 'nginxproxymanager' ;" | mysql
echo "GRANT ALL ON \`nginxproxymanager\`.* TO 'nginxproxymanager'@'%' ;" | mysql
mysqldump --single-transaction --quick --no-create-db --lock-tables nginxproxymanager \
> "${backup}"

# Stop the MySQL server
if ! kill -s TERM "$pid" || ! wait "$pid"; then
bashio::exit.nok "Initialization of database failed."
fi
kill -s TERM "$pid" || true

# Load up database to the core mariadb
mysql -h "${host}" -P "${port}" -u "${username}" -p"${password}" \
nginxproxymanager < "${backup}"

# Delete local MySQL data folder
rm -f -r /data/mysql
fi
23 changes: 23 additions & 0 deletions proxy-manager/rootfs/etc/cont-init.d/npm.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
# Home Assistant Community Add-on: Nginx Proxy Manager
# This file applies patches so the add-on becomes compatible
# ==============================================================================
declare mysql_host
declare mysql_password
declare mysql_port
declare mysql_username
declare query

# Redirect log output to the Hass.io add-on log
sed -i 's#/data/logs/error.log#/proc/1/fd/1#g' /etc/nginx/nginx.conf
sed -i 's#/data/logs/default.log#/proc/1/fd/1#g' /etc/nginx/nginx.conf
Expand Down Expand Up @@ -111,3 +117,20 @@ then
-out /data/nginx/dummycert.pem \
|| bashio::exit.nok "Could not generate dummy certificate"
fi

# Set up database connection
mysql_host=$(bashio::services "mysql" "host")
mysql_password=$(bashio::services "mysql" "password")
mysql_port=$(bashio::services "mysql" "port")
mysql_username=$(bashio::services "mysql" "username")

query=".database.engine = \"mysql\"
| .database.host = \"${mysql_host}\"
| .database.name = \"nginxproxymanager\"
| .database.user = \"${mysql_username}\"
| .database.password = \"${mysql_password}\"
| .database.port = ${mysql_port}"

# shellcheck disable=SC2094
cat <<< "$(jq "${query}" /data/manager/production.json)" \
> /data/manager/production.json
3 changes: 0 additions & 3 deletions proxy-manager/rootfs/etc/services.d/manager/run
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@
# ==============================================================================
declare -a options

# Wait for MySQL to become available
bashio::net.wait_for 3306

bashio::log.info "Starting the Manager..."

options+=(--abort_on_uncaught_exception)
Expand Down
9 changes: 0 additions & 9 deletions proxy-manager/rootfs/etc/services.d/mysql/finish

This file was deleted.

13 changes: 0 additions & 13 deletions proxy-manager/rootfs/etc/services.d/mysql/run

This file was deleted.