Using imapsync

I’ve recently needed to do a big migration of some webshops, including a lot of email accounts from a non-cPanel server to a cPanel server, this means you’re quite limited with the migration, and in this case very limited. There was no possible way to do a backup, so everything needed to be migrated using FTP, so in the end, the process was long and boring. Another problem I had, was emails, these was hosted on an external mail server, I couldn’t get backup either and import it to cPanel, and Fetch Mail in Horde web-client couldn’t import the emails either, so I was looking for a solution, and found imapsync, and I want to show how you can be using imapsync to transfer multiple email accounts from one server to another, quite simple.

The imapsync tool is a CLI tool, you can install on linux, simply by running:

yum install imapsync
#or
apt-get install imapsync

When you have installed the tool, you need to make sure you have 2 email accounts on each server, then you can run a command like following:

imapsync --host1 source.server.com \
 --user1 [email protected] --password1 mypassword \
 --host2 dest.server.com \
 --user2 [email protected] --password2 mypassword

This will sync emails from host1 to host2, and will give you realtime progress about the transfer, something like this:

imapsync-progress

What is really amazing about imapsync is that if the connection suddenly drops, you can run the code again, and it will pick up where it left, this is really amazing if you want to make sure everything is transferred without any problems.

But let’s say we want to transfer 10 accounts, it sucks to run the command 10 times, so we can automate that, but we need some scripting, first you create a file called imapsync-accounts.txt here we’ll put the users and passwords, as following:

user1;password1
user2;password2
#...
user9;password9
user10;password10

Keep in mind, username and password need to be the same on source and destination server for this to work.

Then put the following code into a file in same path as the imapsync-accounts.txt and call it imapsync-batch.sh:

You can find the code here.

Then you can simply run the script by running:

bash imapsync-batch.sh

And it should start syncing all email accounts to the new server.

Please keep in mind, that it needs to list all emails and calculate size of those emails before proceeding, so sometimes it might seem that the script is stuck, but it’s just calculating sizes in the background.

Another cool thing is, some imap servers use a ‘Send mail’ folder called ‘Sent Messages‘ – where others might use ‘.Sent‘. imapsync will know about this and map the Sent Messages or .Sent folder correctly on the destination server.