Import Mail accounts from one server to another using Imapsync

Posted on 25 views

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

How To Install imapsync on Linux

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

Install imapsync on CentOS 8 / Rocky Linux 8 / AlmaLinux 8

Install required tools:

sudo yum -y install wget perl-App-cpanminus gcc

Install the following Perl module:

sudo cpanm lazy Test::MockObject

Download and installimapsync:

perl -Mlazy imapsync
chmod +x imapsync

Test it’s working:

./imapsync --testslive
./imapsync --tests

Install imapsync on CentOS 7

Enable EPEL repository on CentOS 7 system:

sudo yum install epel-release

Install imapsync on CentOS 7 from EPEL repo

sudo yum install imapsync

Confirm successful installation:

# Test
$ imapsync

# A live test:
$ imapsync --testslive --tls1 --ssl2

# Unit tests
$ imapsync --tests

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  \

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

sudo cpanm Data::Uniqid Mail::IMAPClient JSON::WebToken

3. Now download imapsync tarball and install it.

Check for the latest here:

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):

sudo cp imapsync /usr/bin/

Now go to read start with the tutorial.

Installation reference:

Import Mail accounts from one server to another using Imapsync

Below is a basic usage example of Imapsync:

imapsync --host1 \
 --user1 [email protected] --password1 secret1 \
 --host2 \
 --user2 [email protected] --password2 secret2

Note that and must be replaced with appropriate IP addresses or hostnames of the mail servers.

Using Imapsync: Migrating Email accounts

To use imapsync with multiple accounts, we’ll need to create a file with usernames and passwords like below:

$ vim $HOME/accounts.txt

Add Mailbox accounts with passwords.

#next line
  • 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/< /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 ===="
 < $HOME/accounts.txt

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

$ bash $HOME/

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.


Gravatar Image
A systems engineer with excellent skills in systems administration, cloud computing, systems deployment, virtualization, containers, and a certified ethical hacker.