Skip to content

Commit

Permalink
better s3 init
Browse files Browse the repository at this point in the history
  • Loading branch information
eltorio committed Mar 1, 2024
1 parent 1023c7c commit af1f1fa
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 7 deletions.
9 changes: 8 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions docker-entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
30 changes: 26 additions & 4 deletions scripts/init-from-s3.sh
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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
Expand All @@ -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/mailtrain.sql
cat << EOF > /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

0 comments on commit af1f1fa

Please sign in to comment.