Recently I’ve had to migrate some mail servers from old aging SMI E-Smith boxes to newer CentOS boxes running a mixture of Qmail + Vpopmail + Dovecot. Now this write up is not going to cover the inital qmail/vpopmail/dovecot setup so, if that’s what you’re looking for… Sorry you haven’t found it.
The primary problem is that the older E-Smith boxes store mail in the following manner:
/home/e-smith/files/user/USERNAME/Maildir
*AND*
/home/e-smith/files/user/USERNAME/Mail
All your folders and such are kept in the ‘Mail’ folder while all your new mail / inbox is kept in the ‘Maildir’ folder. Why they did this… I don’t know.
Anyways, here are the scripts:
maildir_to_dovecot.sh – This script rsyncs the old data over and then renames it to .Folder.subfolder.etc.
#!/bin/sh
##########################################################
#
# Acquire inputs
#
##########################################################
mailbox=$1
oldhost=”$2″
domain=”$3″
##########################################################
#
# If we don’t have all arguments bail out(tm)
#
##########################################################
if [ -z “$1” ]; then
echo “Usage: $0 user mail.host.tld host.tld”
exit
fi
##########################################################
#
# Rsync the old data, add bwlimit = 150 for pulling from a T1
# This leaves some headroom for them.
#
##########################################################
rsync -av rsync://$oldhost/home/e-smith/files/users/$mailbox /home/vpopmail/domains/$domain/$mailbox
##########################################################
#
# Change to the rsync’d directory and remove all symlinks
#
##########################################################
cd /home/vpopmail/domains/$domain/$mailbox
cd $mailbox
ls -aFd \.* | grep \/ | awk -F\/ ‘{print “rm -rf \””$1″\””}’ | sh
TMP=/tmp/$$.tmp
#
# Look for directories this deep or shallower:
c=20
#
# Recursively start there and go up:
while [ $c -gt 0 ]; do
#
# Find all directories at level $c that aren’t /cur, /new, or /tmp, or /.something
#
find . -nowarn -type d -not -name cur -not -name new -not -name tmp -mindepth $c -not -name ‘.*’ > $TMP
#
# Process each line…
#
while read i; do
#
# Convert every starting dot to nothing
# Convert every “/” to a “.”
j=`echo $i | sed -e ‘s/ /\\ /’ | sed -e ‘s:^\./::’ | sed -e ‘s:/:.:g’`
# Move old to new
mv -v “$i” “.$j”
done < $TMP
#
# Up one level and repeat until level 0
#
c=`expr $c – 1`
done
rm -f $TMP
##########################################################
#
# Change jump out of the rsync’d directory and move everything
# to the new Maildir
#
##########################################################
cd ..
ls -AF $mailbox | grep \/ | awk -F\/ ‘{print “mv \”‘$mailbox’/”$1″\” Maildir/”}’ | sh
##########################################################
#
# Create subscriptions in the new maildir for IMAP
#
##########################################################
cd Maildir
ls -AFd .* | grep \/ | sed -e “s/\(.\)\(.*\)\//\2/g” > subscriptions
##########################################################
#
# Jump out and set permissions recursively
#
##########################################################
cd ..
chown -R vpopmail:vchkpw *
Now, we’ve moved everything over and rebuilt the directory structure along with the subscriptions file. The issue here now is that all folders appear as a subfolder of ‘Mail’
To fix that I quickly wrote up fixmaildir.sh which is the following:
mv .Maildir/cur/* cur/
mv .Maildir/new/* new/
TMP=/tmp/$$.tmp
find . -nowarn -type d -not -name cur -not -name new -not -name tmp -not -name .Mail -not -name .Maildir -mindepth 1 -name '.Mail*' > $TMP
#
# Process each line...
#
while read i; do
#
# Convert every starting dot to nothing
# Convert every "/" to a "."
j=`echo $i | sed -e 's/.Mail//g'`
# Move old to new
mv -v "$i" "$j"
done < $TMP
rm -rf .Maildir
rm -rf .Mail
ls -AFd .* | grep \/ | sed -e "s/\(.\)\(.*\)\//\2/g" > subscriptions
This will move the folders out from under Mail and to the top level. This should be executed from *inside* the users ‘Maildir’ (/home/vpopmail/domains/DOMAIN.TLD/USER/Maildir).
Done!