How to migrate a maildir imap email (vmail/postfix/dovecot) account over to Zimbra

May 27th, 2014 | By | Category: Internet, Linux / Freebsd

Compared to migrating a local pop3 account and folder structure over this one is as simple as cake.  Let’s say you have the remnants of your postfix/dovecot vmail server kicking around, you just finished setting up Zimbra and have it up and running but now you want to recreate all of your folder structure and copy your emails over.  Lucky for you it’s a lot easier than you might think.

Let’s say you have as your email address with all the emails nad folders you want to copy over and your vmail is sitting in /home/vmail.  This is what you need to do

  1. # cd /home/vmail/
  2. # ./ test
  3. rinse repeat for other users

Obviously you’re going to replace testdomain with whatever domain you have and replace ‘test’ in #2 there with the actual user name or email address prefix that you have.  You can just type ./ * if you want to xfer over all of the users.


Here’s the code for the script and a big thanks to the people over at the zimbra forums for such great advice and providing 99% of the code for the script below.  I just added the bit at the top to allow you to easily specify the user

if [[ $1 ]];

# Maildir to Zimbra import
# Drop in your user root and run as superuser.
shopt -s extglob

domain=”” # change to your domain!

for user in `ls -d1 $1/|sed s/\\\///`
echo “User $user”
find $user -maxdepth 10 -type d -name cur | awk ‘{ print length($0),$0 | “sort -n”}’ | while read line;
line=`echo $line | sed -e ‘s/^[0123456789]*[[:space:]]//’`
#echo “PARSED LINE: ” $line

folder=`echo ${line}|cut -f3 -d”/”|sed s/\\\.//`
# replace _ with a space (IMPORTANT! This makes the imported subfolders come out correctly!)
newfolder=$( echo $folder|sed ‘s/[.]/\//g’)
line2=`echo ${line/%cur/new}`
echo “FOLDER $folder”
if [ “$folder” = “cur” ]
echo “Transferring inbox…”
/opt/zimbra/bin/zmmailbox -z -m $user@$domain addMessage “Inbox” “$PWD/$user/Maildir/cur”
/opt/zimbra/bin/zmmailbox -z -m $user@$domain addMessage “Inbox” “$PWD/$user/Maildir/new”
echo “Finished transferring inbox.”
if [ “$folder” != “Sent” ] && [ “$folder” != “Drafts” ] && [ “$folder” != “Junk” ] && [ “$folder” != “Trash” ]
echo “Creating folder ‘/$newfolder’…”
/opt/zimbra/bin/zmmailbox -z -m $user@$domain createFolder /”$newfolder”
echo “Finished creating folder.”

/opt/zimbra/bin/zmmailbox -z -m $user@$domain addMessage “/$newfolder” “${PWD}/${line}”
/opt/zimbra/bin/zmmailbox -z -m $user@$domain addMessage “/$newfolder” “${PWD}/${line2}”


else echo “You didn’t enter a user”
echo “Syntax: ./ user1”
echo “PS – you can enter * instead of a user name to xfer all users over”
echo ” ”

Tags: , , , , , , , ,

Leave a Comment