copying email across hosts

Often times, during server migrations across control panels (or in the absence of control panels), one needs to copy email which the client wants to preserve. IMAP allows us to be lazy and leave all our years of communication online on our servers. So when a client insists that they need their email moved you’re left with the supremely boring task of setting up the two imap accounts in an imap client (thunderbird or outlook), one from old server the other from the new one, downloading all mail from old server then dragging across all the folders to the new server. Extremely slow and frankly frustrating…..

Enter imapsync (well it’s been around for 3 years at least maybe longer).

It doesn’t matter where you install imapsync as you’re going to be passing the hostnames of source and destination to the tool. If you’re on CentOS, get the EPEL repository by running

yum install epel-release

Next setup imapsync by running

yum -y install imapsync

This will setup the dependencies etc. and you should now proceed to the actual mail copy.

For the purpose of clarity we are going to be copying the mails from server1 to server2. Just for the sake of structure and discipline make a directory for each account you are going to synchronise.  In this folder make two files, pass1 and pass2

echo “passwordold” > pass1

echo “passwordnew” > pass2

now run the command that does it all:

imapsync –host1 server1 –user1 <username_old> –passfile1 ./pass1 –host2 server2 –user2 <username_new> –passfile2 ./pass2


Now just sit back and relax as the script logs into both servers, figures out what all folders to subscribe to on the source server, displays the counts and then starts the copy process.

imapsync at work

For a decenly well maintained mailbox it should be done in under 30 minutes. The messages can be suppressed or leave them on they tell you where it is in the copy process. Finally you get a nice summary of what went down:

Start difference host2 – host1 : -650 messages, -205293825 bytes (-195.783 MiB)
Final difference host2 – host1 : -12 messages, -1403319 bytes (-1.338 MiB)
Detected 0 errors


If you login to your mail client on the new server you should see all the mail from your old server.

Leave a Reply

Your email address will not be published. Required fields are marked *