Skip to content

Commit

Permalink
upgrade autobackup-s3
Browse files Browse the repository at this point in the history
  • Loading branch information
eltorio committed Mar 1, 2024
1 parent af1f1fa commit 2021158
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
3 changes: 2 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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/
Expand Down Expand Up @@ -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"]
36 changes: 36 additions & 0 deletions scripts/autobackup-s3
Original file line number Diff line number Diff line change
@@ -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}

0 comments on commit 2021158

Please sign in to comment.