Using Imapsync to import email accounts from one server to another: I provide Web hosting services using Ispconfig. For quite sometime, it was a real pain to migrate existing Cpanel customers to Ispconfig, especially when you’re required to import all users’ Mailboxes and data. I had to manually import Dovecot Mailbox folders between the two servers.

This was boring and tedious manual process. Fortunately, I did discover a nice tool called imapsync which automates this process of mail syncing between servers. This can work on any setup, It doesn’t have to be Cpanel-Ispconfig relationship. All you need is two Mail servers with IMAP. Let’s dive deep to using Imapsync.

using Imapsync: Prerequisites

  • Have a FQDN – Both has to be same on server A and B. In my case, on Ispconfig and Cpanel
  • Working IMAP service on both servers
  • If on Shared Hosting, make sure you know the IP address of the server hosting your web content
  • You should know usernames and passwords for the source email accounts and destination

Installing imapsync

Before using Imapsync , you’ll definitely need to install first.

Install imapsync on Debian and Ubuntu

1. First install required dependency packages:

sudo apt-get install   \
liburi-perl             \
make                    \
libauthen-ntlm-perl \
libcrypt-ssleay-perl \
libdigest-hmac-perl  \
libfile-copy-recursive-perl \
libio-compress-perl \
libio-socket-inet6-perl \
libio-socket-ssl-perl   \
libio-tee-perl          \
libmail-imapclient-perl \
libmodule-scandeps-perl \
libnet-ssleay-perl      \
libpar-packer-perl      \
libreadonly-perl        \
libterm-readkey-perl    \
libtest-pod-perl        \
libtest-simple-perl     \
libunicode-string-perl  \
cpanminus

2. Then manually install perl module Data::Uniqid and Mail::IMAPClient

# cpanm Data::Uniqid Mail::IMAPClient

3. Now download imapsync tarball and install it.

Check for the latest here: https://github.com/imapsync/imapsync/releases

cd /usr/local/src
wget https://github.com/imapsync/imapsync/archive/imapsync-1.727.tar.gz
tar zxvf tar zxvf imapsync*
cd imapsync-imapsync*

Do a dependencies test that shows also the basic example:

 ./imapsync

A live test showing imapsync job:

 ./imapsync --testslive

Now the install command (need root priviledges again):

# cp imapsync /usr/bin/

Now go to read http://imapsync.lamiral.info/#doc start with the tutorial.

Install imapsync on CentOS/RHEL

# yum -y install epel-release
# yum -y install imapsync

Installation reference:

Imapsync usage syntax:
imapsync --host1 server1.example.com \
 --user1 [email protected] --password1 secret1 \
 --host2 server2.example.com \
 --user2 [email protected] --password2 secret2

server1.example.com and server2.example.com can be replaced with appropriate IP addresses.

using Imapsync: Migrating Email accounts

using Imapsync needs creation of a file with usernames and passwords like below:

# vim $HOME/accounts.txt

Add Mailbox accounts with passwords.

host1_1;user1_1;password1_1;host1_2;user1_2;password1_2;
host2_;user2_1;password2_1;host2_2;user2_2;password2_2;
host3_1;user3_1;password3_1;host3_2;user3_2;password3_2;
e.t.c
  • Each line contains 6 columns, columns are parameter values for –host1 –user1 –password1 –host2 –user2 –password2
  • Separator is character semi-colon “;” it can be changed by any character changing IFS=‘;’ in the while loop below.

The add this script:

cat > $HOME/imapsync-batch.sh<<EOF
#!/bin/bash
echo "Looping on account credentials found in accounts.txt"
echo ""
{ while IFS=';' read  h1 u1 p1 h2 u2 p2 fake
    do 
        { echo "$h1" | egrep "^#" ; } > /dev/null && continue # this skip commented lines in accounts.txt
        echo "==== Starting imapsync from host1 $h1 user1 $u1 to host2 $h2 user2 $u2 ===="
        imapsync --host1 "$h1" --user1 "$u1" --password1 "$p1" \
                 --host2 "$h2" --user2 "$u2" --password2 "$p2" \
                 "[email protected]"  
        echo "==== Ended imapsync from host1 $h1 user1 $u1 to host2 $h2 user2 $u2 ===="
        echo
    done 
} < $HOME/accounts.txt
EOF

You can proceed to using Imapsync script and wait for IMAP Mail sync to finish.

# bash $HOME/imapsync-batch.sh

This will sync emails from host1 to host2 and will give you real time progress about the transfer.

If you encounter network connection problems or timeout, just run the script again and it will pick up where it left.

To this point, we’ve covered important essentials on using Imapsync to sync mail accounts between two servers. This can be applied to any Mail server- Mostly Open source mail solutions.