diff --git a/Dockerfile b/Dockerfile index 1b328db1..eaaedd50 100644 --- a/Dockerfile +++ b/Dockerfile @@ -58,6 +58,7 @@ RUN set -ex; \ apk add --update --no-cache \ pwgen netcat-openbsd bash imagemagick curl acme.sh xz imagemagick file jpeg &&\ echo "23 20 * * 0 /autobackup" >> /etc/crontabs/root &&\ + echo "23 43 * * * /autobackup-s3" >> /etc/crontabs/root &&\ echo "*/10 * * * * sleep \$((\`od -vAn -N2 -tu2 < /dev/urandom\` %300)) ; /update-cloudflare-dns.sh" >> /etc/crontabs/root &&\ echo "0 0 * * 0 sleep \$((\`od -vAn -N2 -tu2 < /dev/urandom\` %14400)) ; acme.sh --renew-all --config-home /app/server/files/certs/config" >> /etc/crontabs/root COPY scripts/init-cloudflare.sh /app/ @@ -97,6 +98,6 @@ RUN ARCH=$(uname -m)\ 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 - +COPY --chmod=755 scripts/autobackup-s3 /autobackup-s3 EXPOSE 3000 3003 3004 ENTRYPOINT ["bash", "/app/docker-entrypoint.sh"] diff --git a/scripts/autobackup-s3 b/scripts/autobackup-s3 new file mode 100755 index 00000000..8ee97d94 --- /dev/null +++ b/scripts/autobackup-s3 @@ -0,0 +1,36 @@ +#!/bin/bash +MYSQL_DATABASE=${MYSQL_DATABASE:-'mailtrain'} +BACKUP_KEEP_DAYS=${BACKUP_KEEP_DAYS:-'90'} +# 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" + exit 1 +fi + +# test is mysql variables are set +if [ -z "${MYSQL_HOST}" ] || [ -z "${MYSQL_ROOT_PASSWORD}" ]; then + echo "MYSQL_HOST and MYSQL_ROOT_PASSWORD must be set" + exit 1 +fi + +# test if mc is installed +if [ -z "$(which mc)" ]; then + echo "mc is not installed" + exit 1 +fi + +# Test if mc alias s3backup exists +if [ -z "$(mc alias list | grep s3backup)" ]; then + echo "s3backup alias not found" + echo "create s3backup alias" + mc alias set s3backup ${S3_ENDPOINT} ${S3_ACCESS_KEY} ${S3_SECRET_KEY} +fi + +NOW=$(date -I) +/usr/bin/mysqldump -h $MYSQL_HOST --password="$MYSQL_ROOT_PASSWORD" --all-databases >/app/server/files/backup.sql +tar -cv /app/server/files/backup.sql /app/server/files/campaign /app/server/files/certs /app/server/files/imports /app/server/files/reports /app/server/files/template /app/server/files/uploaded | xz -9 >/app/server/files/backup.tar.xz +rm /app/server/files/backup.sql +mc cp /app/server/files/backup.tar.xz s3backup/${S3_BUCKET}/${S3_PATH}/backup-${NOW}.tar.xz + +# Remove backups older than BACKUP_KEEP_DAYS days +mc rm --recursive --force --older-than ${BACKUP_KEEP_DAYS}d s3backup/${S3_BUCKET}/${S3_PATH}