Dovecot Migration and old mails

I tried the accepted answer and it failed - the dovecot versions in play are probably too far apart and we also switched the underlying MTA. So here is a more robust solution: doveadm import

Assuming you're hosting emails for the domain hosted.tld and an account exists for the login [email protected] (or maybe simply ruth) and the backup from the previous server is in MailDir format and available inside a folder structure like /tmp/TRANSFER/domain/account/Maildir then you can import them to your new dovecot hosting with

$ doveadm import -u [email protected] maildir:/tmp/TRANSFER/hosted.tld/local.account/Maildir "" all

more generally speaking:

$ doveadm import -u LOCAL_USER FORMAT:PATH "" all

All of which can be gleened from the fine manual. It took two tries to find out simply using "" (empty destination mailbox) was what we really wanted ;-)

You may even have more luck than us without needing to transfer the files beforehand - if old and new server are running at the same time and your accounts are set up appropriately using doveadm sync.


For Maildir messages, such flags are stored in the file name. For example:

1328040798.M558634P29803.equal,S=17876,W=18294:2,FS

The letters FS after the comma mean Flagged and Seen. To mark the message as "unseen", either remove the S flag...

for msg in maildir/cur/*; do
    msgbase=${msg%,*}
    flags=${msg##*,}
    flags=${flags//S/}
    mv -v "$msg" "$msgbase,$flags"
done

...or simply throw the messages into the new folder:

mv maildir/cur/* maildir/new/

The Maildir format consists of a series of directories - matching the IMAP folder structure, within which are the emails, one file per email.

In order to copy emails from one email system to another, you can simply copy the directories and files, and ensure the permissions for those directories and files are correct at the destination.

The Maildir structure looks like this:

mail/cur/
mail/new/
mail/tmp/
mail/.personal/cur/
mail/.personal/new/
mail/.personal/tmp/

This shows the INBOX folder (cur, new, tmp) and another folder called "personal". Note the dot prefix showing that this folder is hidden, so this should be accounted for in your transfer.

The new folder contains any emails that have not been seen by a client, and the cur folder contains current emails. The tmp folder should be empty if the mail server is not operating.