When the new email server is ready we can move users mailboxes across to it. While I could just let them do it themselves with copy/paste in the email client, that's a bit rude and will actually take longer. Dovecot has some tools that make moving between servers really easy and for the Live server users shouldn't even notice.
In Ubuntu 24.04 Mail Server I built a new mail server using Ubuntu 24.04 LTS, Postfix and Dovecot to replace the existing server which is based on Ubuntu 20.04 LTS which is due to go end-of-life in 3 months time.
The new sandpit server has been tested and I can successfully receive mail and send it to users on other servers, which is good. The sandpit also fails to send messages to external internet accounts which in this case is also good as I don't want a sandpit server doing anything in public.
The email server has two main components:
- Postfix for receiving and routing messages from mail clients
- Dovecot for storing delivered messages for clients
We've already proven outbound routing from Postfix so we don't need to change anything there. If we were migrating from a different mail system, or had a huge number of users, then we may need to configure a more intelligent email routing scheme as an interim solution. I did that for the original migration from Exchange and this time it's proving to be a lot simpler.
For small groups like those who inhabit my GeekLab server a simple backup & sync will be sufficient and this is built in to Dovecot. The process goes something like this:
- Configure source and target servers
- Perform an initial backup from source to target for pilot users
- Enable periodic mailbox sync for pilot users and validate
- Enable periodic mailbox sync for remaining users
- Redirect inbound messages to target server
- Update clients
- Stop mailbox sync
- Decommission old server
This works very nicely at lab scale and even for larger installation the same overall process still holds it just gets to be bigger scale and possibly moving people across in groups.
Moving Mailboxes
Dovecot provides tooling for moving mailboxes between servers. If you really want you can configure a full multi-node high-availability solution with mailboxes and config running on two (or more) servers, but that's overkill, needs additional components and possibly even a specific initial setup that you have to migrate into anyway. And some of that now appears to have been moved behind a paywall and is no longer available to the public with Dovecot now being split between Dovecot Community Edition and Dovecot Pro.
Using doveadm we can do a backup of mailboxes from one server to another which creates a duplicate of the source mailbox on the target server. If there's already messages for that mailbox on the target they will be overwritten, which during preparation and testing is exactly what we want. For the main migration we'll move to a sync method instead so we don't keep overwriting messages on the new server as that would probably be bad. Unless you hate your users...
There's a few different options we can use. I found the simplest is to use a TCP connection between source and target as this avoids having to muck about with SSH keys and on an internal network this should be sufficient. If you were to be doing this between two different networks or even across an internet connection then you should look at using "TCPS" - a Doveadm TCP connection with TLS encryption, SSH based approaches or at the very least using an encrypted SSH tunnel or VPN.
Preparation
On the target host create a doveadm service with the following in /etc/dovecot/conf.d/10-doveadm.conf
## Doveadm config for mailbox migration - target
##
inet_listener {
port = 12354
}
}
On the source host create a configuration with just the password in /etc/dovecot/conf.d/10-doveadm.conf
## Doveadm config for mailbox migration - source
##
Restart Dovecot on both to make sure it takes effect. You'll also need to be sure any firewalls between them allow 12354/tcp, or whatever port you chose.
Run Backup
Note: In real life there are no spaces either side of the @ of the username, but it seems that browsers helpfully convert email addresses into mailto: links and change the colour to dark blue which makes then hard to read on a black background. Please correct this in your head as you're reading and blame Goozilla.
For testing or for a server with just a few mailboxes - where the definition of few is how many as you can get done in the agreed downtime - it may be sufficient to use the backup function to create a copy of the mailbox on the new server. Simply call the dovadm backup command once for each user you wish to copy across.
user@source:~$ sudo doveadm -Dv backup -u dovetest1 @ domain.example.com tcp:target.domain.example.com:12354
Without the -Dv you won't see any output, with -Dv you'll get a torrent of info which I find more reassuring (at least to start with) as it shows every folder and message that's being processed.
If you want to move a whole group of users you can also prepare a list of usernames and then processes the entire list in one go. Create a file somewhere convenient, such as /etc/dovecot/migrate/backupusers.txt and add usernames to the file as one per line and finish the file with a blank line otherwise the last user gets ignored. And if you've only got one user in the file doesn't do anything while claiming success which is really annoying.
dovetest1 @ domain.example.com
dovetest2 @ domain.example.com
You can then run the doveadm sync command to process the whole list on one go
user@source:~$ sudo doveadm backup -F /etc/dovecot/migrate/backupusers.txt tcp:target.domain.example.com:12354
You can even have several files and call each one individually if you want to move your users across in waves.
Once the initial backup is complete it's safe for users to connect to the new server. While they'll be able to send messages to other servers and uses who've also been migrated they won't be able to receive any until you've completed the inbound redirection. For small servers it'll be better to wait until the initial backups have been completed for all users before anyone connects:
- It gives you a change to confirm the server has sufficient storage, and that backup works in a reasonable time with the server fully loaded.
- All the mailboxes will be initialised so new mail that's delivered will get the correct IDs - apparently there can be issues if mailboxes are created fresh by email delivery
- If user mailboxes are created as a result of email delivery from somebody who has already migrated, they'll be wiped out when you do their backup which is likely to cause annoyance
If you have a large server and you can't "pre-load" everyone then you'll need to do a staged migration and configure split routing so that mail is routed to either old or new server on a user-by-user basis as needed. This is more complicated and since I don't have to go there it's beyond the scope of this article.
Run sync
While users can connect as soon as their first backup has completed, as noted above it's better to wait until the initial backup has been completed for all users and all other tests are complete. Should anything interesting happen you can rerun the backup as many times as needed until you're sure.
user@source:~$ sudo doveadm -Dv sync -u dovetest1 @ domain.example.com tcp:target.domain.example.com:12354
Using sync mode the old and new mailboxes for the user are kept in sync. And like the backup command you can also use the -F option and pass it a list of users to sync, either the same list you used for the initial backup or a separate lists by wave such as /etc/migrate/syncusers1.txt. For the duration of the migration it's worth putting this into script that is called via a systemd timer (or cron job) to run the sync process on a regular schedule.
user@source:~$ sudo doveadm sync -F /etc/dovecot/migrate/syncusers1.txt tcp:target.domain.example.com:12354
Note: For the sandpit server I just did manual sync since there's only 4 unimportant users. For the real server I will be running the sync via schedule for a few days at list so I'll add the config for the systemd service and timer units once I've proven them.
The sync works both ways which means users can connect to either old or new server to access and process their messages and they'll eventually become consistent with each other.
If you have an empty mail store on the target server you can just run the sync straight off and ignore the backup and this works quite nicely. If you have some user mailboxes already on the target, perhaps because they connected prematurely so it created a new one for them, or maybe as left-overs from testing and then you do a sync you can end up with duplicated or unwanted messages or in the worst case if you go and delete messages on the target using a mail client, those deletions will also be synchronised back to source which could well prove embarrassing.
Inbound Redirect
Postfix doesn't store messages so we've nothing to migrate beyond making sure the queues are processed and are empty before we decommission. If we wanted to be extra kind we could update the configuration of the old server to relay all messages destined to itself to the new server instead. This would let us shut down the mailbox sync step and the source Dovecot sooner.
Internally I use an MX record in the DNS for each sub-domain to identify the mail host to use. To switch email delivery ("relay") from other servers to the new one it's just a matter of updating the MX record with the new server name and then waiting for that change to propagate. On my GeekLab that took about 15 minutes.
For servers that collect external mail we also need to switch over the collection to the new host - that will be covered in Inbound email using getmail as we need to transfer the getmail state too. In the mean time we can let the doveadm sync handle the delivery into the mailbox of the new server.
Client Reconnect
Non-Production Servers
For Dev and sandpit domains the mail server uses its real name so the reconfiguration is entirely manual and each mail client will need its configuration updated individually to use the new server.
Even my official test mail server is only used by myself, but I will configure a scheduled dovecot sync to prove the config ahead of doing it on the live server.
The configuration change is of course specific to each mail client, but should typically just be a change of host name for inbound (IMAP) and outbound (SMTP/S) connections and maybe re-enter the password. All other details should be the same and the mailbox sync should also mean no changes in messages, save for any new arrivals that have yet to synchronise.
Live Server
Disclosure: This article is a work in progress. I have migrated the sandpit server but until I complete the migration of a full test server I'm not going to go into detail. I'm confident this will work exactly as planned but there's plenty of opportunity for surprises. After all there's DNS involved here and that always seems to cause issues.
The Live server (and the test one which has an identical setup) are a little more complicated, but should also be simpler as a result. Yes, this looks weird when written down but it it's true, from a certain point of view.
I use a split-DNS on the internet router with a "fake" DNS A record to point to the actual mail server for internal users. This is for two reasons:
- When I'm out the mail client on my phone / laptop can use the same fully-qualified name as when I'm at home.
- If I ever need to move the mail server I just need to update this fake address record to the new IP and clients will then connect to the new server.
To make this work the TLS certificate on the server needs to be configured with the real name of the server as it's subject, and the fake name must also be added as an additional Subject Alternate Name (SAN) to the certificate as well. This will allow mail clients to complete the TLS handshake and establish a secure connection to read and send messages.
This is just a case of logging into the router and updating the configuration, which is of course router specific and how you've configured your internal DNS. It's also something that is not typically supported on most "free" residential routers, but then if you're running a GeekLab you've probably got a decent router or at least workrounds for limitations.
Of course any clients that ignore this and connect directly on its real hostname will still need to be manually updated.
The live server is also externally accessible by users too, via an HAproxy reverse proxy (and Fail2Ban) to keep the worst of the idiots away from it. This will also need to be updated to use the new server, either as a straight cutover or adding the new server as a second host on the backend and allowing it to load balance and then removing the old one later during decommissioning.