Ensim migration does not properly copy mail folders

I have a bug report with a suggested (implemented) solution.

When migrating an Ensim site to Virtualmin, while all mail accounts themselves are successfully migrated, mail folders are not.

Ensim uses UW IMAP, which uses mbox folders, stored in the mail/ subdirectory of the virtual user's home directory (in /home/virtual//home//mail/). Virtualmin uses Dovecot IMAP, which uses Maildir folders, in the Maildir/ subdirectory of the virtual user's home directory (/home//homes//Maildir/).

The mail/ directory, complete with mboxes, is copied, but is treated simply as a regularly directory in the user's home.

The good news is that there is already a free utility out there that converts mbox to Maildir. I wrote a bash script around this utility to convert all folders for all users of a site, which then also fixes permissions, and adds IMAP subscriptions for the converted folders.

The converter utility, mb2md, is available at http://batleth.sapienti-sat.org/projects/mb2md/ and requires the TimeDate Perl library -- in CentOS this is easily installed with
> sudo yum install perl-TimeDate

My shell script, which takes care of everything else, follows (where "owner" is the owner of the site on the Virtualmin server, and "domain.org" is the domain that has been migrated, and mb2md is in root's path, and this script is run as root):

--- code ---

#!/bin/bash

# for all directories
for i in /home/owner/homes/*; do
if [ -d $i ]; then
echo $i

# convert mbox to maildir
mb2md -s /home/owner/homes/$i/mail/ -d /home/owner/homes/$i/Maildir/

# copy subscriptions
# Note: The .mailboxlist file from Ensim lists folders as mail/folder (sometimes mail//folder).
# Mbox folders can have full-stops (.) in their names. In Maildir folders these should be
# replaced with underscores.
# This series of sed calls replaces double-slashes with single-slashes, replaces full-stops
# with underscores, and then removes the leading mail/, leaving only the folder names
# as they result from mb2md, and appends these to the subscriptions file in Maildir/
cat /home/owner/homes/$i/.mailboxlist | sed 's/\/\//\//' | sed 's/\./_/' | sed 's/mail\///' >> /home/owner/homes/$i/Maildir/subscriptions

# fix permissions
# all of the files in Maildir should be owned by the virtual user, and in the group of the site owner
chown -R $i@domain.org /home/owner/homes/$i/Maildir/.*
chgrp -R owner /home/owner/homes/$i/Maildir/.*

chown -R $i@domain.org /home/owner/homes/$i/Maildir/subscriptions
chgrp -R owner /home/owner/homes/$i/Maildir/subscriptions
fi
done

--- /code ---

The above shell script is Copyright 2009 NETEK Software Engineering (http://www.netek.com.au/). I am happy to contribute this back to Virtualmin under whichever version of GPL Virtualmin is distributed under.

It may be useful to build this into the Ensim migration tool (and possibly into other migration tools, if other hosting systems use mbox mail folders).

You may also wish to add

rm -rf /home/owners/homes/$i/mail/

as the antepenultimate line, to remove the old mbox mail folders, which are no longer used. I didn't, in my case, because I wanted to keep them as a backup, in case something went wrong with my conversion.

Best,
Johannes.

Status: 
Closed (fixed)