Search Site:








Edit - To Do - AllRecentChanges

Recent Changes Printable View Page History Edit Page

This page is part of the EmailServer article.

Mbox to Maildir migration

When upgrading a mail server, you may be faced with a rather 'interesting' problem: migrating old user account in the classic monolithic mbox format to the more tolerant and performing maildir.

Mbox is a way to store emails in a single file. It's old, compatible with most mail applications, but as your mail store grows, it becomes harder to manage emails contained within.
Things start to become shaky if your mailbox is large, with the risk of corruption or connection time-out becoming likely.

Maildir on the other hand is newer and not supported by all systems. Basically it's just a way to store emails as files within a folder hierarchy. Its advantages are mostly better performance and tolerance in case of failure since you're more likely to lose just one email instead of your entire mailbox.

Postfix (MTA) and Dovecot (POP3 and IMAP server) both support the maildir format and can integrate with each other seamlessly.


It's nevertheless a great pain when you have to migrate existing mailboxes to the new format, on a new server for instance.

There is apparently only one tool that allows this type of thing: it's a small Perl script maintained by Philip Mak that is available as Perfect_maildir.

Just download the single file script and you are ready to convert, unless you're getting a long error message and in that case you may need to download the Date::Parse Perl module.

To install it, just enter:

# perl -MCPAN -e 'install Date::Parse'

If this is the first time you install a module, you will be asked a series of question. Just keep pressing Enter until you're asked about your geographic area. Make your selection of mirrors to download from and Perl will do the rest. Next time, you won't be asked anything.

If you have a handful of accounts to migrate, doing it by hand may be tedious, but it can still be faster than going through setting up an automated process.

I found useful to use my mail client to create IMAP accounts for every user I needed to migrate, then use their original mailbox layout to re-create the directories they had.

You can get the old mailbox layout by viewing the .mailboxlist file in the user's home.
Once each directory had been re-created (using your mail client), use to migrate the email for each folder, for instance:

# /mail/michael/email < /oldmail/var/mail/michael
# /mail/michael/email/.Drafts < \
# chown michael.michael -R /mail/michael/email

Would first move the inbox and then the old Draft mboxes to the new maildirs and ensure that the files belong to the user; this is particularly important if you're doing the migration as root.

If you need to migrate a lot of accounts, then you should use the migration tools below. They make use of, so you'll need that as well. I've included a copy of the script on this site just in case.

Just in case it may be useful to you, here is a small script I used to move each original mbox inbox to the new maildir one:

my $n = $ARGV[0];
$n =~ s/^\s+//;
$n =~ s/\s+$//;
die "No mailbox for $n\n"  \
    unless (-e "/oldmail/spool/$n" && -e "/mail/$n");
`mkdir /mail/$n/email/cur` unless -e "/mail/$n/email/cur";
`mkdir /mail/$n/email/new` unless -e "/mail/$n/email/new";
`mkdir /mail/$n/email/tmp` unless -e "/mail/$n/email/tmp";
print ` /mail/$n/email < /oldmail/spool/$n`;
`chown $n.$n -R /mail/$n`;

You will need to modify the hard-coded paths to suit your configuration. In mine, I used the following:

  • /mail/<username>/email is the destination maildir folder.
  • /oldmail/spool/<username> is the old inbox mbox.

If you're setting up a new machine and your inboxes are on the old one, you can make files available to the new system through NFS and mounting the exported directories locally or simply copying using scp.


If your old email is sitting in a POP3 account at an ISP, you can use fetchmail to retrieve it.

Fetchmail is easy to use and I won't go into the details except that you could use a /root/.fetchmailrc file containing one line per account for each user. Note that this file must be chmod 600 for security reasons or fetchmail won't touch it:

poll protocol pop3 user roger to roger password 123456 keep

The keep at the end will leave the mail on the old account just in case.

Another way, useful if you are using IMAP on the old account and have created IMAP folders, is to have both old and new account in your client email for each user and drag-drop files from one to the other after having recreated the same directory structure manually.


< AlternateAccess | EmailServer | TroubleShooting >

Leave your comments below
Enter your comment (no links allowed): Author:

Edit Page - Page History - Printable View - Recent Changes - WikiHelp - Search - RSS -
Page last modified on Monday 04 May 2009, at 05:48 GMT+8 - Viewed 3978 times