Skip to content

Commit

Permalink
Add mysql database upgrade script. Tidy docker file
Browse files Browse the repository at this point in the history
  • Loading branch information
jimyhuang committed Apr 1, 2023
1 parent c179019 commit 31e75fa
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 23 deletions.
26 changes: 12 additions & 14 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,22 @@ ENV \
COMPOSER_HOME=/root/.composer \
PATH=/root/.composer/vendor/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

#mariadb
WORKDIR /etc/apt/sources.list.d
# basic packages install
RUN \
apt-get update && \
apt-get install -y apt-transport-https wget gnupg && \
apt-get install -y rsyslog apt-transport-https wget gnupg gcc make autoconf libc-dev pkg-config google-perftools qpdf curl vim git-core supervisor procps

# add PHP sury
WORKDIR /etc/apt/sources.list.d
RUN \
echo "deb https://packages.sury.org/php/ bullseye main" > phpsury.list && \
echo "deb-src https://packages.sury.org/php/ bullseye main" >> phpsury.list && \
wget https://packages.sury.org/php/apt.gpg && apt-key add apt.gpg && rm -f apt.gpg && \
apt-get update && \
apt-get install -y wget mariadb-server mariadb-backup gcc make autoconf libc-dev pkg-config google-perftools qpdf
apt-get update

RUN apt-get install -y supervisor procps
#mariadb
RUN \
apt-get install -y wget mariadb-server mariadb-backup mariadb-client

# wkhtmltopdf
WORKDIR /tmp
Expand All @@ -26,12 +30,10 @@ RUN \
dpkg -i wkhtmltox.deb && \
rm -f wkhtmltox.deb


WORKDIR /
RUN \
apt-get update && \
apt-get install -y \
rsyslog \
php8.1 \
php8.1-curl \
php8.1-imap \
Expand All @@ -45,14 +47,11 @@ RUN \
php8.1-zip \
php8.1-bz2 \
php8.1-ssh2 \
php8.1-yaml \
curl \
vim \
git-core
php8.1-yaml

RUN \
curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer && \
composer global require drush/drush:8.3.3 && \
composer global require drush/drush:8.4.12 && \
cd /root/.composer && \
find . | grep .git | xargs rm -rf && \
composer clearcache
Expand Down Expand Up @@ -93,4 +92,3 @@ RUN \
WORKDIR /var/www/html
ENV TERM=xterm
CMD ["/usr/bin/supervisord"]

118 changes: 109 additions & 9 deletions container/mysql/mysql-init.sh
Original file line number Diff line number Diff line change
@@ -1,46 +1,146 @@
#!/bin/bash
set -eo pipefail

# Get config
# logging functions
mariadb_log() {
local type="$1"; shift
printf '%s [%s] [Entrypoint]: %s\n' "$(date --rfc-3339=seconds)" "$type" "$*"
}
mariadb_note() {
mariadb_log Note "$@"
}
mariadb_warn() {
mariadb_log Warn "$@" >&2
}
mariadb_error() {
mariadb_log ERROR "$@" >&2
exit 1
}

mariadb_do_upgrade() {
mariadb_note "[Perform Mariadb System Table Upgrade]"
mariadb_note "Waiting temporary process start up ..."
mysqld \
--skip-networking \
--wsrep_on=OFF \
--expire-logs-days=0 \
--loose-innodb_buffer_pool_load_at_startup=0 \
--basedir=/usr \
--datadir=/var/lib/mysql \
--plugin-dir=/usr/lib/mysql/plugin \
--user=mysql \
--log-error=/var/www/html/log/mysql.log &
MARIADB_PID=$!

for i in {30..0}; do
if mysql -uroot -p$INIT_PASSWD --database=mysql <<<'SELECT 1' &> /dev/null; then
break
fi
sleep 1
done
mariadb_note "Temporary process started."
mariadb_backup_system_table

mariadb_note "Upgrading system tables ..."
mysql_upgrade -uroot -p$INIT_PASSWD --upgrade-system-tables
mariadb_note "Upgrading completed."
kill "$MARIADB_PID"
wait "$MARIADB_PID"
}

mariadb_backup_system_table() {
mariadb_note "[Perform Mariadb System Table Backup]"
local oldfullversion="unknown_version"
local backup_db="system_mysql_backup_unknown_version.sql.gz"

if [ -r /var/lib/mysql/mysql_upgrade_info ]; then
read -r -d '' oldfullversion < /var/lib/mysql/mysql_upgrade_info || true
if [ -n "$oldfullversion" ]; then
backup_db="system_mysql_backup_${oldfullversion}.sql.gz"
fi
fi

mariadb_note "Backing up system database to $backup_db ..."
if ! mysqldump --skip-lock-tables --replace --databases mysql -uroot -p$INIT_PASSWD mysql | gzip -9 > /var/lib/mysql/${backup_db}; then
mariadb_error "Unable backup system database for upgrade from $oldfullversion."
fi
mariadb_note "Backup completed."
}

mariadb_version() {
local maria_version="$(mariadb --version | awk '{ print $5 }')"
maria_version="${maria_version%%[-+~]*}"
echo -n "${maria_version}-MariaDB"
}

mariadb_upgrade_is_needed() {
# 0 is true, 1 is false
if [ ! -d "/var/lib/mysql/mysql" ]; then
return 1
fi

MARIADB_VERSION=$(mariadb_version)
if [ ! -f /var/lib/mysql/mysql_upgrade_info ]; then
mariadb_note "MariaDB upgrade information missing, trying to upgrade system table to ${MARIADB_VERSION} anyway."
return 0
fi

IFS='.-' read -ra new_version <<< $MARIADB_VERSION
IFS='.-' read -ra old_version < /var/lib/mysql/mysql_upgrade_info || true
if [[ ${#new_version[@]} -lt 2 ]] || [[ ${#old_version[@]} -lt 2 ]] \
|| [[ ${old_version[0]} -lt ${new_version[0]} ]] \
|| [[ ${old_version[0]} -eq ${new_version[0]} && ${old_version[1]} -lt ${new_version[1]} ]]; then
mariadb_note "MariaDB upgrade may required from ${old_version[0]}.${old_version[1]} to ${new_version[0]}.${new_version[1]}"
return 0
fi

mariadb_note "MariaDB upgrade not required. Old version: ${old_version[0]}.${old_version[1]}, New version: ${new_version[0]}.${new_version[1]}"
return 1
}

# Config log and run
if [ ! -d "/var/run/mysqld" ]; then
mkdir -p /var/run/mysqld
chown mysql:mysql /var/run/mysqld
echo "" > /var/www/html/log/mysql.log
chown mysql:mysql /var/www/html/log/mysql.log
fi

if [ ! -d "/var/lib/mysql/mysql" ]; then
mkdir -p /var/lib/mysql

echo 'Initializing database'
mariadb_note 'Initializing database..'
mysql_install_db --datadir="/var/lib/mysql"
echo 'Database initialized'
mariadb_note 'Database initialized'

"mysqld" --skip-networking &
pid="$!"

mysql=( mysql --protocol=socket -uroot )

mariadb_note 'MariaDB init process in progress...'
for i in {30..0}; do
if echo 'SELECT 1' | "${mysql[@]}" &> /dev/null; then
break
fi
echo 'MySQL init process in progress...'
sleep 1
done
if [ "$i" = 0 ]; then
echo >&2 'MySQL init process failed.'
exit 1
mariadb_error 'MariaDB init process failed.'
fi

if ! kill -s TERM "$pid" || ! wait "$pid"; then
echo >&2 'MySQL init process failed.'
exit 1
mariadb_error 'MariaDB init process failed.'
fi

echo
echo 'MySQL init process done. Ready for start up.'
mariadb_note 'MariaDB init process done. Ready for start up.'
echo
elif mariadb_upgrade_is_needed; then
mariadb_do_upgrade
else
mariadb_note 'MariaDB ready for start up'
fi

mariadb_note 'Perform MariadB process start using libtcmalloc_minimal ...'
exec env LD_PRELOAD=/usr/lib/x86_64-linux-gnu/libtcmalloc_minimal.so.4 "mysqld" --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib/mysql/plugin --user=mysql --log-error=/var/www/html/log/mysql.log

0 comments on commit 31e75fa

Please sign in to comment.