Migrating Email Between IMAP Servers with Imapsync

Ever needed to move a email account from one server to another and all you have is the login name and password for the mail account. Whether it be cPanel to Zimbra, cPanel to cPanel, Plesk to cPanel or just to backup your Gmail account this great program has saved me a few times now.

imapsync created by Gilles LAMIRAL which allows syncing from IMAP to IMAP accounts. It will run any where a Perl interpreter can run. I have noticed that it is good idea to run on a server that is nether source nor destination although this is just for speed.

Install

This was pretty straight forward for me.
Centos
sudo yum install imapsync.noarch
Ubuntu
sudo apt-get install imapsync

or check out Gilles page for manual instructions and a paid support which he offers.

Howto Use for a Single Account

Source
– mail.domain.com
– username1
– password1

Destination
– mail2.domain.com (can be an Ip if no DNS is set or using as a backup method)
– username2 (can be the same as username1)
– password2 (can be the same as password1)

imapsync --nosyncacls --syncinternaldates --host1 mail.domain.com --user1 username1 --password1 password1 --host2 mail2.domain.com --user2 username2 --password2 password2 --noauthmd5

You can run this multiple times to make sure the email is in sync right up to when the DNS is changed.

Running a Batch for Multiple Accounts

Now doing a batch of account at with a single script (this is for linux based systems only).
This is the script I use, it is a mix of scripts and from different sources including my own way of doing things likelockfiles. Use at own risk.

This is made up of 2 files;

  • imapsync-accounts.txt is in the format of user;pass each on a new line.
  • imapsync-batch.sh is the script below.

#!/bin/bash

####################################################
# File name - imapsync-batch.sh
# Description - Batch IMAP to IMAP sync
# Creation - DEC 06 2010 - BN
# Modified - DEC 13 2010 - BN
# Requires - imapsync-accounts.txt
# Requires - Username/password to be the same on both sides
#          - may change this if I ever need it to backup to different account.
####################################################

SERVERNAME=$HOSTNAME
SCRIPT_NAME="$SERVERNAME - Batch IMAP TO IMAP"
MAIL=/bin/mail;
MAIL_RECIPIENT="deadmail AT mrbuckykat.com"
LOCK_FILE="/tmp/$SERVERNAME.imapsync.lockfile"
LOGFILE="imapsync_log.txt"

#host1 is Source
HOST1=mail.somedomain.com

#host2 is Dest
HOST2=mail2.somedomain.com

#domain is where email account is "every thing after the @"
DOMAIN=somedomain.com

####################################################
###### Do not modify past here
####################################################

if [ ! -e $LOCK_FILE ]; then
touch $LOCK_FILE
#Run core script

TIME_NOW=$(date +"%Y-%m-%d %T")
echo "" >> $LOGFILE
echo "————————————" >> $LOGFILE
echo "IMAPSync started – $TIME_NOW" >> $LOGFILE
echo "" >> $logfile

{ while IFS=’;’ read u1 p1; do
USER_NAME=$u1"@"$DOMAIN
echo "Syncing User $USER_NAME"
TIME_NOW=$(date +"%Y-%m-%d %T")
echo "Start Syncing User $u1"
echo "Starting $u1 $TIME_NOW" >> $LOGFILE
imapsync –nosyncacls –syncinternaldates –host1 $HOST1 –user1 "$USER_NAME" –password1 "$p1" –host2 $HOST2 –user2 "$USER_NAME" –password2 "$p1" –noauthmd5
TIME_NOW=$(date +"%Y-%m-%d %T")
echo "User $USER_NAME done"
echo "Finished $USER_NAME $TIME_NOW" >> $LOGFILE
echo "" >> $LOGFILE
done ; } < imapsync-accounts.txt TIME_NOW=$(date +"%Y-%m-%d %T") echo "" >> $LOGFILE
echo "IMAPSync Finished – $TIME_NOW" >> $LOGFILE
echo "————————————" >> $LOGFILE

#End core script
#uncomment if you want a email once script is finished – useful for big syncs
#echo " IMAPSync Finished" | $MAIL -s "[$SCRIPT_NAME] Finshed" $MAIL_RECIPIENT
rm -f $LOCK_FILE

else
TIME_NOW=$(date +"%Y-%m-%d %T")
echo "$SCRIPT_NAME at $TIME_NOW is still running" | $MAIL -s "[$SCRIPT_NAME] !!WARNING!! still running" $MAIL_RECIPIENT
echo "$SCRIPT_NAME at $TIME_NOW is still running"
fi

And all that is left is to run it.

Once again if you need to re sync, all you need to do is re run the script and it will sync it again.

Notes / Reference:

It is a one way sync, to go back you will need to flip the servers.

http://freshmeat.net/projects/imapsync/

http://wiki.zimbra.com/wiki/User_Migration/

http://www.mrbuckykat.com/migrating-email-between-imap-servers-with-imapsync/

Leave a comment