David.M.Clark 1961 Page

December 29, 2024, 7:29 am

My How-To Articles

Article Title mbox to Maildir migration for CentOS with Dovecot (Davrom Document ID: 20171011154700)
Article Reference 17101101

Scenario:

This document shows the migration process of mbox based users to Maildir based users. It is not always convenient to migrate all users from mbox to Maildir in one step, however, it is possible to run your CentOS e-mail server with both user mailbox formats with the following tested solution.

Solution:

NOTE: This solution requires the following installed packages or utilities:

Dovecot
Procmail
mb2md-3.20.pl

This solution has been tested using SendMail as the installed MTA (Mail Transfer Agent).

Two example accounts have been used throughout this document:

davrom - an existing already migrated Maildir user
davromtoo - an mbox user that will be migrated to Maildir

Procedure:

1. Edit /etc/dovecot/10-mail.conf and ensure all references to mail_location are commented out:

# mail_location = maildir:~/Maildir
# mail_location = mbox:~/mail:INBOX=/var/mail/%u
# mail_location = mbox:/var/mail/%d/%1n/%n:INDEX=/var/indexes/%d/%1n/%n
#
#
#
#mail_location =
# mail_location = maildir:~/Maildir

2. Add the users database text file to /etc/dovecot/conf.d/auth-system.com:

##
## User databases
##

# System users (NSS, /etc/passwd, or similiar). In many systems nowadays this
# uses Name Service Switch, which is configured in /etc/nsswitch.conf.
userdb {
  # 
  driver = passwd
  # [blocking=no]
  #args = 
}

To look like this:

##
## User databases
##

# System users (NSS, /etc/passwd, or similiar). In many systems nowadays this
# uses Name Service Switch, which is configured in /etc/nsswitch.conf.
userdb {
      driver = passwd-file
      args = username_format=%n /etc/dovecot/users
}
userdb {
  # 
  driver = passwd
  # [blocking=no]
  #args = 
}

3. Now create and populate the /etc/dovecot/users file with the '/etc/passwd' styled users ensuring you record their username, user ID (uid), group ID (gid), their $HOME directory and the userdb_mail= setting to enforce either mbox or Maildir format:

davrom::501:501::/home/davrom::userdb_mail=maildir:~/Maildir
davromtoo::502:502::/home/davromtoo::userdb_mail=mbox:~/mail:INBOX=/var/mail/%u

NOTE: For the purpose of this how-to document, it is not necessary to put all existing users into the /etc/dovecot/users file - any users that are not in this file will still be routed to the older mbox format, however, IMPORTANT it is vital that you implement /etc/procmailrc as per step 4 in this document before attempting any further steps.


4. To ensure any of your newly migrated users to Maildir format have their new e-mail messages passed to their new Maildir setup, it is vital to implement the following Procmail based system-wide file. Create the /etc/procmailrc file and populate it with the following content:

#!/bin/bash
##VERBOSE=YES
##LOGFILE="/var/log/procmail.log"
SHELL="/bin/bash"
IS_MAILDIR=$HOME/Maildir

:0
* ? test -d $IS_MAILDIR 
{
MAILDIR=$HOME/Maildir/
DEFAULT=$HOME/Maildir/
}

/etc/procmailrc is essentially checking to see if the user has the Maildir directory in their $HOME directory - if it is not present, then the e-mail will be passed to their mbox based Inbox (normally /var/mail/username or /var/spool/mail/username).

You can use the "VERBOSE" and "LOGFILE" entries to keep a diagnostics of e-mail being delivered, but has been commented out in this example as the test system has now started working for Maildir format and diagnostics are no longer needed. If you choose to keep the procmail.log file options set (that being VERBOSE and LOGFILE), it is recommended that you also create a Linux based 'logrotate' setup as this file can grow very large on very busy e-mail servers.


5. Download and install the mb2md.pl utility available on the web. Suggestion is to rename the downloaded file, possibly named mb2md-3.20.pl, to /usr/bin/mb2md.pl for ease of location and usage.


6. Migrate the individual user's mailboxes setup with the mb2md.pl utility. The following steps are suggested (substitute the username 'davromtoo' to that of the user you wish to migrate):

su - davromtoo

mv mail mail_old

/usr/bin/mb2md.pl -m

/usr/bin/mb2md.pl -s mail_old -R

or:

/usr/bin/mb2md.pl -s mail_old

NOTE: If your mail_old contains actual Linux directories as e-mail folders where users may have been able to create physical Linux directories as e-mail folders, possibly through using the "/" character at the end of a folder name from e-mail clients such as Thunderbird, you will need to use a -R option in the command line rather than just the -s old_mail. So the command (as above) would be:

/usr/bin/mb2md.pl -s mail_old -R

This will ensure you include sub-directories under the mail_old directory to be imported into Maildir.

The mb2md.pl -m command migrates the mbox /var/mail/davromtoo (or /var/spool/mail/davromtoo) Inbox to the user's $HOME directory under the newly created directory called Maildir (created by the mb2md.pl script).

The /usr/bin/mb2md.pl -s mail_old or /usr/bin/mb2md.pl -s mail_old -R command(s) migrates any mbox folders stored in the mail_old directory.

The subscriptions are also imported to Maildir.


7. Edit /etc/dovecot/users and change the newly converted user entry to reflect using Maildir:

So to change davromtoo to Maildir:

davrom::501:501::/home/davrom::userdb_mail=maildir:~/Maildir
davromtoo::502:502::/home/davromtoo::userdb_mail=mbox:~/mail:INBOX=/var/mail/%u

Becomes:

davrom::501:501::/home/davrom::userdb_mail=maildir:~/Maildir
davromtoo::502:502::/home/davromtoo::userdb_mail=maildir:~/Maildir

Now test the user and your conversion will be complete for this respective user.




(c) 2024 David.M.Clark 1961