Credit goes directly to cenolan.com’s blog post – http://www.cenolan.com/2012/02/mass-email-migration-from-imap-to-google-apps-using-imapsync/

This method includes a text file containing the users source email address and password as well as the destination (Google) email address and password. Imapsync is the engine to transfer the email and there is a bash script to drive it.

*Allow each user to update their password once email is directed to Google
**Allow less secure apps to allow the transfer to work – http://support.google.com/a/answer/6260879?hl=en – turn off again once complete

Insert the following into a text file (update with live details):

user1@source.tld;user1sourcepassword;user1@destination.tld;user1destinationpassword
user2@source.tld;user2sourcepassword;user2@destination.tld;user2destinationpassword
user3@source.tld;user3sourcepassword;user3@destination.tld;user3destinationpassword

Add as many users as necessary. I found it helpful to add 10 users into a text file at a time to keep an eye on transfers.

Create the script:

vim syncscript.sh

Insert the following:

#!/bin/bash

#Configure servers
SERVER1=source.mailserver.com
SERVER2=imap.googlemail.com

#Uncomment to hide folder sizes
#FAST="--nofoldersizes"

#Uncomment to do a dry run (no actual changes)
#DRY="--dry"

#Uncomment to just sync folders (no messages)
#JUSTFOLDERS="--justfolders"

#Uncomment to just connect (no syncs at all)
#JUSTCONNECT="--justconnect"

#Set the path to your imapsync binary
imapsync=imapsync

#Users file
if [ -z "$1" ]
then
echo "No users text file given."
exit
fi

if [ ! -f "$1" ]
then
echo "Given users text file \"$1\" does not exist"
exit
fi

{ while IFS=';' read u1 p1 u2 p2; do

$imapsync --usecache --tmpdir /var/tmp \
--host1 ${SERVER1} --user1 "$u1" \
--password1 "$p1" --ssl1 \
--host2 ${SERVER2} \
--port2 993 --user2 "$u2" \
--password2 "$p2" --ssl2 \
--addheader
${FAST} ${DRY} ${JUSTFOLDERS} ${JUSTCONNECT} \
--regextrans2 's{Sent$}{[Gmail]/Sent Mail}' \
--regextrans2 's{Sent Items$}{[Gmail]/Sent Mail}' \
--regextrans2 's{Sent Messages$}{[Gmail]/Sent Mail}' \
--regextrans2 's{Drafts$}{[Gmail]/Drafts}' \
--exclude 'INBOX.Trash|INBOX.spam|INBOX.Apple Mail To Do'

done ; } < $1 [html] Make the script executable: [html] chmod +x syncscript.sh 
Running the script:
 ./syncscript.sh users.txt > migrationlog.txt 2>&1 &

A log will be kept of the email transfer.

The script will more than likely take a long time to run so run it in screen.

Written by Matt Cooper
Hi, I'm Matt Cooper. I started this blog to pretty much act as a brain dump area for things I learn from day to day. You can contact me at: matt@linuxtutorial.co.uk.