diff --git a/Dockerfile b/Dockerfile index 0ca4a9cf..1b328db1 100644 --- a/Dockerfile +++ b/Dockerfile @@ -88,7 +88,14 @@ RUN set -ex; \ apk add --update --no-cache \ mysql-client mariadb-connector-c-dev -RUN curl -L https://dl.min.io/client/mc/release/linux-$(dpkg --print-architecture)/mc > /usr/local/bin/mc && chmod +x /usr/local/bin/mc +RUN ARCH=$(uname -m)\ + && if [ "$ARCH" = "x86_64" ]; then\ + curl -L https://dl.min.io/client/mc/release/linux-amd64/mc > /usr/local/bin/mc && chmod +x /usr/local/bin/mc;\ + elif [ "$ARCH" = "aarch64" ]; then\ + curl -L https://dl.min.io/client/mc/release/linux-arm64/mc > /usr/local/bin/mc && chmod +x /usr/local/bin/mc;\ + else\ + curl -L https://dl.min.io/client/mc/release/linux-arm/mc > /usr/local/bin/mc && chmod +x /usr/local/bin/mc;\ + fi COPY --chmod=755 scripts/init-from-s3.sh /app/init-from-s3.sh EXPOSE 3000 3003 3004 diff --git a/docker-entrypoint.sh b/docker-entrypoint.sh index 050d2217..f8513163 100644 --- a/docker-entrypoint.sh +++ b/docker-entrypoint.sh @@ -203,6 +203,9 @@ fi echo 'Info: Waiting for MySQL Server' while ! nc -z $MYSQL_HOST $MYSQL_PORT; do sleep 1; done +echo 'Info: Running s3 restore script' +/app/init-from-s3.sh + if [ "$WITH_REDIS" = "true" ]; then echo 'Info: Waiting for Redis Server' while ! nc -z $REDIS_HOST $REDIS_PORT; do sleep 1; done @@ -231,8 +234,6 @@ if [ "$WITH_LDAP" = "true" ]; then fi fi -/app/init-from-s3.sh - NODE_ENV=production node setup/docker-entrypoint-db-setup.js "$ADMIN_PASSWORD" "$ADMIN_ACCESS_TOKEN" NODE_ENV=production node index.js diff --git a/scripts/init-from-s3.sh b/scripts/init-from-s3.sh index e8f116eb..66d48ae9 100644 --- a/scripts/init-from-s3.sh +++ b/scripts/init-from-s3.sh @@ -1,5 +1,5 @@ #!/bin/bash -MAILTRAIN_DATABASE=${MAILTRIAN_DATABASE:-'mailtrain'} +MYSQL_DATABASE=${MYSQL_DATABASE:-'mailtrain'} # test if variables S3_BUCKET, S3_ACCESS_KEY, S3_SECRET_KEY, S3_ENDPOINT, S3_REGION, S3_PATH are set if [ -z "${S3_BUCKET}" ] || [ -z "${S3_ACCESS_KEY}" ] || [ -z "${S3_SECRET_KEY}" ] || [ -z "${S3_ENDPOINT}" ] || [ -z "${S3_PATH}" ]; then echo "S3_BUCKET, S3_ACCESS_KEY, S3_SECRET_KEY, S3_ENDPOINT, S3_REGION, S3_PATH are not set" @@ -13,7 +13,9 @@ if [ -z "${MYSQL_HOST}" ] || [ -z "${MYSQL_ROOT_PASSWORD}" ]; then fi # Check if database need to be restored -if [ -z "$(mysql -h $MYSQL_HOST --password="$MYSQL_ROOT_PASSWORD" -e "SHOW DATABASES LIKE '${MAILTRAIN_DATABASE}'")" -a "$INIT_FROM_S3" = "1" ]; then +mysql -h $MYSQL_HOST --password="$MYSQL_ROOT_PASSWORD" -e "USE $MYSQL_DATABASE; SELECT value FROM settings" 2>/dev/null +TEST_DB=$? +if [[ "$TEST_DB" != "0" && "$INIT_FROM_S3" == "1" ]]; then echo "Database ${MAILTRAIN_DATABASE} does not exist restoring it from s3" # test if mc is installed if [ -z "$(which mc)" ]; then @@ -36,6 +38,26 @@ if [ -z "$(mysql -h $MYSQL_HOST --password="$MYSQL_ROOT_PASSWORD" -e "SHOW DATAB rm /tmp/backup.tar.xz # Restore the mailtrain database - sed -n -e "/^-- Current Database: \`${MAILTRAIN_DATABASE}\`/,/^-- Current Database: \`/p" /app/server/files/backup.sql >/app/server/files/${MAILTRAIN_DATABASE}.sql - mysql -h $MYSQL_HOST --password="$MYSQL_ROOT_PASSWORD" /app/server/files/${MYSQL_DATABASE}.sql +-- MariaDB dump 10.19 Distrib 10.11.5-MariaDB, for Linux (aarch64) +-- +-- Host: mysql Database: +-- ------------------------------------------------------ +-- Server version 8.3.0 + +/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; +/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; +/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; +/*!40101 SET NAMES utf8mb4 */; +/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */; +/*!40103 SET TIME_ZONE='+00:00' */; +/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */; +/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; +/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; +/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */; +EOF + sed -n -e "/^-- Current Database: \`${MYSQL_DATABASE}\`/,/^-- Current Database: \`/p" /app/server/files/backup.sql >>/app/server/files/${MYSQL_DATABASE}.sql + mysql -h $MYSQL_HOST --password="$MYSQL_ROOT_PASSWORD" < /app/server/files/${MYSQL_DATABASE}.sql +else + echo "Database ${MYSQL_DATABASE} already exists" fi