also looking at this
Harden encrypted mail storage, recovery, and forwarding
#8
50 files
+1701
-9001
| @@ -11,6 +11,8 @@ BACKUP_DIR="${BACKUP_DIR:-/opt/mail/backups}" | ||
| 11 | 11 | RETAIN_DAYS="${RETAIN_DAYS:-7}" |
| 12 | 12 | SWAP_GB="${SWAP_GB:-2}" |
| 13 | 13 | FAIL2BAN_IGNORE_IPS="${FAIL2BAN_IGNORE_IPS:-127.0.0.1/8 ::1}" |
| 14 | SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" | |
| 15 | REPO_ROOT="$(cd "${SCRIPT_DIR}/../../.." && pwd)" | |
| 14 | 16 | |
| 15 | 17 | echo "== memory safety ==" |
| 16 | 18 | # The production box is shared with Bun applications. Give short deployment |
| @@ -79,26 +81,25 @@ systemctl enable --now fail2ban >/dev/null 2>&1 || true | ||
| 79 | 81 | systemctl restart fail2ban || true |
| 80 | 82 | echo "fail2ban: $(systemctl is-active fail2ban)" |
| 81 | 83 | |
| 84 | echo "== encrypted storage ==" | |
| 85 | if ! command -v cryptsetup >/dev/null 2>&1; then | |
| 86 | export DEBIAN_FRONTEND=noninteractive | |
| 87 | apt-get update -qq && apt-get install -y -qq cryptsetup >/dev/null | |
| 88 | fi | |
| 89 | chmod +x "${REPO_ROOT}/scripts/setup-encrypted-storage.sh" | |
| 90 | MAIL_ROOT="${MAIL_DIR}" "${REPO_ROOT}/scripts/setup-encrypted-storage.sh" | |
| 91 | ||
| 82 | 92 | echo "== backups ==" |
| 83 | 93 | mkdir -p "$BACKUP_DIR" |
| 84 | cat > "$MAIL_DIR/backup.sh" <<BK | |
| 85 | #!/usr/bin/env bash | |
| 86 | set -euo pipefail | |
| 87 | TS=\$(date +%Y%m%d-%H%M%S) | |
| 88 | OUT="$BACKUP_DIR/mail-backup-\$TS.tgz" | |
| 89 | # mailboxes + databases + DKIM key + config (exclude the backups dir itself) | |
| 90 | tar czf "\$OUT" \\ | |
| 91 | -C "$MAIL_DIR" mail smtp.db caldav.db dkim 2>/dev/null || true | |
| 92 | [ -f /etc/mail/mail.env ] && tar rf "\${OUT%.tgz}.env.tar" -C /etc/mail mail.env 2>/dev/null || true | |
| 93 | # prune old backups | |
| 94 | find "$BACKUP_DIR" -name 'mail-backup-*.tgz' -mtime +$RETAIN_DAYS -delete 2>/dev/null || true | |
| 95 | echo "backup written: \$OUT (\$(du -h "\$OUT" 2>/dev/null | cut -f1))" | |
| 96 | BK | |
| 97 | chmod +x "$MAIL_DIR/backup.sh" | |
| 94 | install -m 0755 "${REPO_ROOT}/scripts/mail-backup.sh" "$MAIL_DIR/backup.sh" | |
| 95 | install -m 0755 "${REPO_ROOT}/scripts/mail-forward-compile" /usr/local/sbin/mail-forward-compile | |
| 96 | install -m 0755 "${REPO_ROOT}/scripts/maildir-migrate.sh" /usr/local/sbin/maildir-migrate | |
| 98 | 97 | |
| 99 | 98 | cat > /etc/systemd/system/mail-backup.service <<'U' |
| 100 | 99 | [Unit] |
| 101 | 100 | Description=Mail server backup |
| 101 | Requires=mail-storage.service | |
| 102 | After=mail-storage.service | |
| 102 | 103 | [Service] |
| 103 | 104 | Type=oneshot |
| 104 | 105 | ExecStart=/opt/mail/backup.sh |