🛠 FULL EMAIL MIGRATION PROCESS
Step 0: Preparation
- SSH access to both servers (OLD and NEW).
- Make sure both Mailcow servers are running.
- Verify the domain and mailboxes already exist on the new server.
- Optional but recommended: backup the new server’s mailbox folder:
cp -r /opt/mailcow-dockerized/data/vmail/ampnetwork.com.np /opt/mailcow-dockerized/data/vmail/ampnetwork.com.np.bak
Step 1: Stop incoming mail temporarily (optional but safer)
- On OLD server, you can stop postfix/dovecot temporarily to avoid new emails arriving during migration:
docker compose stop postfix-mailcow dovecot-mailcow
- On NEW server, it’s okay to keep services running.
Step 2: Copy mailbox data from OLD → NEW server
Use rsync to transfer emails. On OLD server:
rsync -avz --progress /opt/mailcow-dockerized/data/vmail/ampnetwork.com.np/ user@NEW_SERVER_IP:/opt/mailcow-dockerized/data/vmail/ampnetwork.com.np/
Notes:
user@NEW_SERVER_IP→ replace with your SSH user and IP of new server.- Trailing slash
/at the end of source ensures only the contents of the domain folder are copied, not the folder itself. --progressshows transfer progress; add--partial --bwlimit=1000for very large mailboxes to limit bandwidth.
Step 3: Fix permissions on NEW server
After transfer, on the NEW server, make sure Dovecot can read the emails:
docker compose exec dovecot-mailcow chown -R vmail:vmail /var/vmail/ampnetwork.com.np
Step 4: Reindex / resync mailboxes
Now tell Dovecot to scan and register all emails. You can:
Option A: Resync all mailboxes of the domain:
docker compose exec dovecot-mailcow doveadm force-resync -u info@ampnetwork.com.np '*'
Repeat this command for each mailbox if you have multiple users.
Option B: Resync all mailboxes (if only this domain exists):
docker compose exec dovecot-mailcow doveadm force-resync -A '*'
Step 5: Verify the migration
- Log in via webmail or IMAP client (Thunderbird, Outlook).
- Check:
- All folders are present (Inbox, Sent, Drafts, Trash)
- Emails are correctly migrated
- No duplicates unless previously existed
Step 6: Resume mail flow
- If you stopped postfix/dovecot on OLD server, restart:
docker compose start postfix-mailcow dovecot-mailcow
Optional: Clean up old server mail
- Once you confirm emails are fully migrated and working on NEW server, you can optionally remove old vmail data to free space:
rm -rf /opt/mailcow-dockerized/data/vmail/ampnetwork.com.np
✅ Notes & Best Practices
- Use rsync over SSH → ensures safe transfer and resume if interrupted.
- Keep backups → especially before deleting anything.
- Permissions matter → Dovecot won’t see emails if ownership is wrong.
- Large mailboxes → consider transferring at night or in batches using mailbox subfolders.
This method migrates only the selected domain’s emails, keeps mailbox structure intact, and works for any number of mailboxes.
#!/bin/bash
# =========================================
# Mailcow domain migration script
# Migrates all mailboxes of ampnetwork.com.np
# =========================================
# =========================
# CONFIGURATION - EDIT THESE
# =========================
OLD_SERVER="old.server.ip.or.hostname"
OLD_USER="ssh_user_on_old"
NEW_SERVER="new.server.ip.or.hostname" # usually localhost if running on new server
DOMAIN="ampnetwork.com.np"
VMAIL_PATH="/opt/mailcow-dockerized/data/vmail"
MAILCOW_PATH="/opt/mailcow-dockerized"
# =========================
# 1️⃣ Backup existing vmail on NEW server (optional but recommended)
# =========================
echo "Backing up current vmail data on NEW server..."
ssh $NEW_SERVER "cp -r $VMAIL_PATH/$DOMAIN ${VMAIL_PATH}/${DOMAIN}.bak_$(date +%Y%m%d%H%M%S)"
# =========================
# 2️⃣ Sync emails from OLD to NEW server
# =========================
echo "Syncing emails from OLD to NEW server..."
rsync -avz --progress $OLD_USER@$OLD_SERVER:$VMAIL_PATH/$DOMAIN/ $VMAIL_PATH/$DOMAIN/
# =========================
# 3️⃣ Fix permissions on NEW server
# =========================
echo "Fixing permissions on NEW server..."
ssh $NEW_SERVER "docker compose -f $MAILCOW_PATH/docker-compose.yml exec dovecot-mailcow chown -R vmail:vmail /var/vmail/$DOMAIN"
# =========================
# 4️⃣ Resync mailboxes on NEW server
# =========================
echo "Resyncing mailboxes on NEW server..."
ssh $NEW_SERVER "docker compose -f $MAILCOW_PATH/docker-compose.yml exec dovecot-mailcow doveadm force-resync -A '*'"
echo "Migration completed successfully for domain $DOMAIN!"
No Comments Yet
Be the first to share your thoughts!