Migration from Maildir File Format to Zimbra SUCCESS!

Looking to migrate to ZCS? Ask here. Got a great tip or script that helped you migrate? Post it here.
Tekify
Posts: 4
Joined: Sat Sep 13, 2014 12:42 am

Migration from Maildir File Format to Zimbra SUCCESS!

Post by Tekify »

After searching high and low to find a way to import mail from an old server to Zimbra, I've settled on this Maildir to Zimbra method. I began with Imapsync, but found it to be unreliable. It would complete, but Zimbra would still be missing lots of emails.
This solution is a modified version of a script found elsewhere on these forums. I made a few upgrades so that:

- Subfolders import correctly! The original scripts would make each folder show up at the root level with this syntax: "Folder1.Subfolder1.Sub-Subfolder1" instead of the intended hierarchical format.

- It runs successfully! I've moved GB's worth of email over already, and it hasn't had an issue so far.
I've found this to be the most reliable solution so far. I suggest you give it a try if you're looking to migrate mail.
Here's what you'll need to do:

1. Begin by adding the users to Zimbra in advance. The user accounts *must* match what you're importing. The only exception is if you import the mail below, and rename the folders to whatever account name you set up in this step.

2. Start with a "scratchpad" folder on your Zimbra system. This is a workplace where the Maildir files can sit while you're transferring them.

3. Copy the user mail folders from your old server. The result should be multiple folders inside this work area, such as user1, user2, user3, etc.

4. Make a file called run.sh in this work area. Copy the code below into it. Make sure you update the domain name in the script. Close it and CHMOD 777 it.

5. Now, run the script. Type ./run.sh. It will run for a while. You'll know it's complete when you are returned to a command prompt.
#!/bin/bash
#

# Maildir to Zimbra import

# Drop in your user root and run as superuser.

#

shopt -s extglob
domain="domain.com" # change to your domain!
for user in `ls -d1 */|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


I hope this helps someone down the road! I know I could have used it. It would have saved a LOT of time.
yogesh2tech
Posts: 45
Joined: Sat Sep 13, 2014 12:42 am

Migration from Maildir File Format to Zimbra SUCCESS!

Post by yogesh2tech »

Hi Dear

This is very important script to migrate mail from old system to newly zimbra server, but u dont think it still need some modification. Because i am using this script to migrate my mail account but after migrating all mails it shows all mail as read while i have lots of unread mails. pls provide the solution to get rid off the problem.

Thanks
Tekify
Posts: 4
Joined: Sat Sep 13, 2014 12:42 am

Migration from Maildir File Format to Zimbra SUCCESS!

Post by Tekify »

Hello,
Yes, it would be nice if it were able to retain the read/unread settings. However, the original script that I found online did not do this either. I didn't add the feature, I just made this script reliable so it works every time.
In my case, having all old mail imported and marked as read was acceptable.
yogesh2tech
Posts: 45
Joined: Sat Sep 13, 2014 12:42 am

Migration from Maildir File Format to Zimbra SUCCESS!

Post by yogesh2tech »

can you please edit this feature to me?

because i am not expert in scripting as you.

pls help me.

Thanks in advance
drwho18
Advanced member
Advanced member
Posts: 60
Joined: Fri Sep 12, 2014 10:33 pm

Migration from Maildir File Format to Zimbra SUCCESS!

Post by drwho18 »

Yep, I wrote/posted the original script, it worked but I quit on it because of the flag issue, and very little interest in the script a the time.
tribear
Advanced member
Advanced member
Posts: 57
Joined: Sat Sep 13, 2014 12:46 am

Migration from Maildir File Format to Zimbra SUCCESS!

Post by tribear »

Can you help me understand that if I use this script I can move my old user mail folders having this layout to Zimbra?
/Home/User/Maildir

>cur

>new

>tmp

.Archives

.Drafts

.Sent

.Trash
So if I try your script I can see where to change the domain. But when I copy mail to a working folder do I need to include the folders /home/user/maildir ...... and subfolder "cur" or just /user/maildir/cur?
Steven
dansomers
Posts: 1
Joined: Sat Sep 13, 2014 2:13 am

Migration from Maildir File Format to Zimbra SUCCESS!

Post by dansomers »

I get a failure on line 7 of the script --> shopt: not found
My default shell is bash, so this should work. shopt -s extglob works fine from the terminal but not in the script. Any ideas?
I am attempting to import maildirs into Zimbra on Ubuntu Lucid.
Tekify
Posts: 4
Joined: Sat Sep 13, 2014 12:42 am

Migration from Maildir File Format to Zimbra SUCCESS!

Post by Tekify »

Try changing line 7 to reflect the full path to shopt. I'm not sure what it is off hand, so you'll need to find it.
hpfs74
Posts: 1
Joined: Sat Sep 13, 2014 2:16 am

Migration from Maildir File Format to Zimbra SUCCESS!

Post by hpfs74 »

I've been in need to import also subfolders so i've changed a little your script.
Maybe it's useful for someone



#!/bin/bash

#

# Maildir to Zimbra import

# Drop in your user root and run as superuser.

#

ZMM="/opt/zimbra/bin/zmmailbox"
domain="domain.com" # change to your domain!

defaultpass="xxxxyyyy"
for user in `ls -d1 */|sed s/\///`

do

echo

echo "User $user"

echo
#

#

find $user -maxdepth 10 -type d -name cur | while read line;

do


folder=$(echo ${line}|cut -f3 -d"/"|sed s/\.//)

line2=$(echo ${line/%cur/new})

#echo "LINE2 $line2"

#echo "FOLDER $folder"

if [ "$folder" = "cur" ]

then

$ZMM -z -m $user@$domain addMessage Inbox $PWD/$user/Maildir/cur

$ZMM -z -m $user@$domain addMessage Inbox $PWD/$user/Maildir/new

else

if [ "$folder" != "Sent" ] && [ "$folder" != "Drafts" ] && [ "$folder" != "Junk" ] && [ "$folder" != "Trash" ]

then
echo $folder | tr '.' '
' > /tmp/qq
folder=""

while read x;

do

folder="$folder/$x"
if [ $($ZMM -z -m $user@$domain gaf | grep "$folder" | wc -l) == 0 ]; then

$ZMM -z -m $user@$domain createFolder "$folder"

fi

done
else

folder="/$folder"

fi
$ZMM -z -m $user@$domain addMessage "$folder" "${PWD}/${line}"

$ZMM -z -m $user@$domain addMessage "$folder" "${PWD}/${line2}"

fi
done
done
unc0nnected
Posts: 34
Joined: Sat Sep 13, 2014 3:52 am

Migration from Maildir File Format to Zimbra SUCCESS!

Post by unc0nnected »

Thanks so much for this, I couldn't believe how easy it was. I added a little extra bit to allow someone to easily specify the user and blogged about it here. This solution was way easier than finding a nice easy way to get my locally stored and organized pop3 accounts synced up, including folder structure to zimbra but after a day of trial and error found the solution to that too!
Any ways here's my modification:



#!/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
Post Reply