My own mail server: moving the mailboxes without losing a message

In November I moved the mail of several of my domains from a hosted service to my own Stalwart server on my production Kubernetes cluster. This is what had to be right before I changed the MX records, how I moved the mailboxes without losing mail, and how I make sure I am the first to know when something breaks.

· 5 min read

The mail of my domains lived in a hosted, paid service. But the website packages I offer include mail on the customer’s domain, and I did not want to sell something I do not run myself every day. So first I moved my own mailboxes, from several domains I use daily on the phone and in the browser. I offer it to customers only after using it myself every day.

Why Stalwart

Before choosing, I compared the popular self-hosted mail stacks. Most of them were made for a separate, ordinary Linux server, not for a cluster. My cluster runs on Talos Linux, which has no shell and no Docker. Stalwart is one program written in Rust that does SMTP, IMAP, spam filtering, DKIM signing and MTA-STS, and it runs in Kubernetes like any other app.

Three things decided it. The whole configuration lives in git and a separate job applies it automatically after every sync, so there are no settings clicked in a panel and written down nowhere. The mailbox index, folders and settings live in PostgreSQL with a continuous backup that can restore the state from any moment. Message bodies are stored encrypted in object storage, outside the server. What Stalwart does not have, I added next to it: Roundcube as webmail, with Polish texts and its own image proxy so a newsletter sender does not learn the reader’s IP address, and the ClamAV virus scanner.

What has to be right before anyone accepts your mail

On the internet a mail server is judged by its DNS. Mail goes out from a separate, fixed IP address whose reverse record (PTR) points to the server’s name, and that name points back to the same address. On top of that every domain gets a full set of records. Here is the set for one domain, generalized (no real names, addresses or keys):

; where to deliver mail
example.com.                     MX   10 mail.example.com.
; who may send for the domain
example.com.                     TXT  "v=spf1 ip4:<server-address> ~all"
; public keys to check DKIM signatures (separate RSA and Ed25519)
<rsa-selector>._domainkey        TXT  "v=DKIM1; k=rsa; p=<key>"
<ed25519-selector>._domainkey    TXT  "v=DKIM1; k=ed25519; p=<key>"
; what to do with mail that fails SPF and DKIM, and where to send reports
_dmarc                           TXT  "v=DMARC1; p=quarantine; rua=mailto:<reports-mailbox>"
; announces the MTA-STS policy: encrypted connection when delivering to this domain
_mta-sts                         TXT  "v=STSv1; id=<policy-version>"
; reports about TLS problems from other servers
_smtp._tls                       TXT  "v=TLSRPTv1; rua=mailto:<reports-mailbox>"

I tighten things step by step: DMARC starts at quarantine and the MTA-STS policy starts in testing mode. I move to stricter settings only when the reports from big providers show no problems for a longer time. Stalwart suggests a zone file for each domain, but I did not copy it as a whole: it advertised more services than I really offer. The same rule applies to mail client autoconfiguration: I advertise only what works.

Moving without losing a message

First the whole setup ran on a test subdomain: receiving, sending, aliases, logging in from a mail app and from the browser. Before switching the real domains I did a restore test. A script adds a message with a random attachment to a test mailbox, builds a separate copy of the database and the server from the backup, isolated so it cannot send mail or touch live data, and then compares folders, message counts and checksums of the bodies and attachments. Only when everything matches does it report success to monitoring. I run the same script before every upgrade, because a backup nobody has restored is only a hope.

I copied the mailboxes with imapsync before touching DNS. The copy never deletes anything on either side, and it can be stopped and resumed safely. I published the DKIM records, an SPF that allowed both senders and DMARC ahead of time, with a short TTL. Switching the MX records of all domains took less than half an hour. Later copy runs checked that nothing reached the old service after the switch. The whole time the old service was a fallback with a written way back: restore its records, and copy whatever had arrived on the new server back with the same tool.

I left one mailbox outside my own platform on purpose. Notices from my infrastructure providers go there. If my mail server is down, the warning about it must not land on that same server.

How I am the first to know

Monitoring covers more than “is the server up”. An alert comes when the outgoing queue grows, when deliveries start failing, when the virus scanner is down, or when the restore test has not reported success for too long. No good news is also news.

One bug taught me how to check webmail. A check with curl passed: the page and the message list data came back fine. But the list in the browser was empty, because the bug only showed up when the page’s scripts ran. Only a real browser running in the background showed it, so since then I check webmail exactly that way.

What I learned

  • First on myself, then for customers. A few weeks of daily use of my own mailboxes shows things no test will think of, and the cost of a mistake is mine.
  • Restore test before the switch, not after. A script that compares the content, not only checks that a backup exists, gives peace of mind at every upgrade.
  • Correct DNS is only the start. The reputation of a new address is built calmly, over time – the records are a condition, not a guarantee.
  • Your own mail server is a responsibility, not a one-off project. A service whose outage everyone notices at once needs alerts, backups and a way back before the first problem comes.

All posts