Mailcow

Automatically back up Mailcow mail data and configuration to Amazon S3.

Contents

  1. Prerequisites
  2. Configure AWS CLI
  3. Backup Script
  4. Schedule with Cron

Automate daily Mailcow backups to Amazon S3 for off-site protection.

Prerequisites

  • AWS account with an S3 bucket created
  • IAM user with s3:PutObject and s3:GetObject permissions
  • awscli installed on your server

Configure AWS CLI

sudo apt install -y awscli
aws configure
# Enter: Access Key, Secret Key, region (e.g. us-east-1), output format: json

Backup Script

#!/bin/bash
# /opt/mailcow-backup.sh
set -e
DATE=$(date +%Y-%m-%d)
BACKUP_DIR="/tmp/mailcow-${DATE}"
S3_BUCKET="s3://your-backup-bucket/mailcow"

mkdir -p "$BACKUP_DIR"
cd /opt/mailcow-dockerized
docker compose stop

tar -czf "${BACKUP_DIR}/mailcow-${DATE}.tar.gz"   mailcow.conf docker-compose.yml   data/conf data/assets/ssl

docker compose start
aws s3 cp "${BACKUP_DIR}/mailcow-${DATE}.tar.gz" "${S3_BUCKET}/"
rm -rf "$BACKUP_DIR"
echo "Backup complete: mailcow-${DATE}.tar.gz"
chmod +x /opt/mailcow-backup.sh

Schedule with Cron

sudo crontab -e
# Add:
0 2 * * * /opt/mailcow-backup.sh >> /var/log/mailcow-backup.log 2>&1