Skip to content

Commit

Permalink
Update autobackup for splitting mail in 10MB parts
Browse files Browse the repository at this point in the history
  • Loading branch information
aeltorio committed Jan 6, 2025
1 parent c1cb0e7 commit 0b206d0
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 25 deletions.
8 changes: 4 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@ RUN set -ex; \
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/
COPY scripts/init-letsencrypt.sh /app/
COPY scripts/update-cloudflare-dns.sh /
COPY scripts/autobackup /
COPY --chmod=755 scripts/init-cloudflare.sh /app/
COPY --chmod=755 scripts/init-letsencrypt.sh /app/
COPY --chmod=755 scripts/update-cloudflare-dns.sh /
COPY --chmod=755 scripts/autobackup /
RUN chmod ugo+x /app/init-cloudflare.sh &&\
chmod ugo+x /app/init-letsencrypt.sh &&\
chmod ugo+x /update-cloudflare-dns.sh &&\
Expand Down
101 changes: 80 additions & 21 deletions scripts/autobackup
Original file line number Diff line number Diff line change
@@ -1,38 +1,97 @@
#!/bin/bash
# Script to backup the database and files
# Run only if BACKUP_TO and BACKUP_FROM are set
if [ -z "$BACKUP_TO" ] || [ -z "$BACKUP_FROM" ]; then
echo "BACKUP_TO and BACKUP_FROM must be set"
exit 1
fi
MAILUSER=`cat /app/zone-mta/config/builtin-zonemta.json |sed -n 's/^.*\"user\":.*\"\(.*\)\",$/\1/p'| head -n1`
MAILPASSWD=`cat /app/zone-mta/config/builtin-zonemta.json |sed -n 's/^.*\"pass\":.*\"\(.*\)\"$/\1/p'| head -n1`
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
# and send them by email

# Function to send the backup by email
# $1: NOW (date)
# $2: COUNT (current part)
# $3: TOTAL (total parts)
# $4: FILE (full path)
# $5: BACKUP_FROM
# $6: BACKUP_TO
# $7: MAILUSER
# $8: MAILPASSWD
# $9: MAILSERVER
send_backup_email() {
local _NOW=$1
local _COUNT=$2
local _TOTAL=$3
local _FILE=$4
local _BACKUP_FROM=$5
local _BACKUP_TO=$6
local _MAILUSER=$7
local _MAILPASSWD=$8
local _MAILSERVER=$9
_FILE_WITHOUT_PATH=$(basename $_FILE)
echo "Sending backup email $_COUNT/$_TOTAL from '$_BACKUP_FROM' to '$_BACKUP_TO' with file '$_FILE_WITHOUT_PATH' via server '$_MAILSERVER'"
(
cat << EOF
From: "SAUVEGARDE @HCFMailer+" <$BACKUP_FROM>
To: "Backup@Mailtrain" <$BACKUP_TO>
cat << EOF
From: "SAUVEGARDE @HCFMailer+" <$_BACKUP_FROM>
To: "$_BACKUP_TO" <$_BACKUP_TO>
MIME-Version: 1.0
Subject: Sauvegarde HCFMailer+ du $NOW
Subject: Sauvegarde HCFMailer+ du $_NOW ($_COUNT/$_TOTAL)
Content-Type: multipart/mixed; boundary="-"
This is a MIME encoded message. Decode it with "munpack"
or any other MIME reading software. Mpack/munpack is available
via anonymous FTP in ftp.andrew.cmu.edu:pub/mpack/
---
Content-Type: text/plain
Content-Type: multipart/alternative; boundary="inner-boundary"
--inner-boundary
Content-Type: text/plain; charset="utf-8"
Voici la sauvegarde du $_NOW
Partie $_COUNT sur $_TOTAL
Voici la sauvegarde du $NOW
HCFMailer+ team
--inner-boundary
Content-Type: text/html; charset="utf-8"
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body style="font-family: Arial, sans-serif;">
<h2 style="color: #2c3e50;">Sauvegarde HCFMailer+</h2>
<p>Voici la sauvegarde du <strong>$_NOW</strong></p>
<p>Partie $_COUNT sur $_TOTAL</p>
<hr>
<p style="color: #7f8c8d;">HCFMailer+ team</p>
</body>
</html>
--inner-boundary--
---
Content-Type: application/octet-stream; name="backup-$NOW.tar.xz"
Content-Type: application/octet-stream; name="$_FILE_WITHOUT_PATH"
Content-Transfer-Encoding: base64
Content-Disposition: inline; filename="backup-$NOW.tar.xz"
Content-Disposition: inline; filename="$_FILE_WITHOUT_PATH"
EOF
) | (cat - && /usr/bin/openssl base64 < /app/server/files/backup.tar.xz && echo "" && echo "---")\
| /usr/sbin/sendmail -f $BACKUP_FROM -au$MAILUSER -ap$MAILPASSWD -amPLAIN -S 127.0.0.1:2525 -t --
) | (cat - && /usr/bin/openssl base64 < $_FILE && echo "" && echo "---")\
| /usr/sbin/sendmail -f $_BACKUP_FROM -au$_MAILUSER -ap$_MAILPASSWD -amPLAIN -S $_MAILSERVER -t --
# echo "Command line: /usr/sbin/sendmail -f $_BACKUP_FROM -au$_MAILUSER -ap$_MAILPASSWD -amPLAIN -S $_MAILSERVER -t --"
}

# Run only if BACKUP_TO and BACKUP_FROM are set
if [ -z "$BACKUP_TO" ] || [ -z "$BACKUP_FROM" ]; then
echo "BACKUP_TO and BACKUP_FROM must be set"
exit 1
fi
MAILUSER=`cat /app/zone-mta/config/builtin-zonemta.json |sed -n 's/^.*\"user\":.*\"\(.*\)\",$/\1/p'| head -n1`
MAILPASSWD=`cat /app/zone-mta/config/builtin-zonemta.json |sed -n 's/^.*\"pass\":.*\"\(.*\)\"$/\1/p'| head -n1`
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
split -b 10m /app/server/files/backup.tar.xz /tmp/backup.$NOW.tar.xz.
COUNT="O"
TOTAL=$(ls /tmp/backup.$NOW.tar.xz.* | wc -l | tr -d ' ')
for FILE in /tmp/backup.$NOW.tar.xz.*; do
COUNT=$((COUNT+1))
send_backup_email "$NOW" "$COUNT" "$TOTAL" "$FILE" "$BACKUP_FROM" "$BACKUP_TO" "$MAILUSER" "$MAILPASSWD" '127.0.0.1:2525'
rm $FILE
done

0 comments on commit 0b206d0

Please sign in to comment.