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 test@testdomain.com 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/testdomain.com
  2. # ./xfer2zimbra.sh 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 ./xfer2zimbra.sh * 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

#!/bin/bash
if [[ $1 ]];

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

domain=”ironbellystudios.com” # change to your domain!

for user in `ls -d1 $1/|sed s/\\\///`
do
echo
echo “User $user”
echo
#
#
find $user -maxdepth 10 -type d -name cur | awk ‘{ print length($0),$0 | “sort -n”}’ | while read line;
do
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” ]
then
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.”
else
if [ “$folder” != “Sent” ] && [ “$folder” != “Drafts” ] && [ “$folder” != “Junk” ] && [ “$folder” != “Trash” ]
then
echo “Creating folder ‘/$newfolder’…”
/opt/zimbra/bin/zmmailbox -z -m $user@$domain createFolder /”$newfolder”
echo “Finished creating folder.”
fi

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

fi
done

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

Tags: , , , , , , , ,

Leave a Comment