Migrate Emails using ImapSync

ImapSync is one of those great tools that comes really handy when you have to migrate emails from a non-compatible server to a cPanel based server. On this post we will show you how to migrate emails using ImapSync.

ImapSync Requirements

ImapSync requires you to have access to both mailboxes, on the destiny and on the origin server. That means you will have to:

Get password for the current email boxes on the server.
Create the exact same mailboxes with and assign same passwords on the new server.

Installing ImapSync

CentOS 7 Installation

First install access to the Epel repository using yum package manager:

yum install epel-release

Then install imapsync and all its dependencies:

yum install imapsync

Centos 6 Installation

Install the CentOS 6 Epel repository

wget http://dl.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm
rpm -Uvh epel-release-6-8.noarch.rpm

Then install imapsync and its dependencies:

yum install imapsync

The installation should look like this:

Migrate Emails using ImapSync
Eg. 01. Installing ImapSync on CentOS 6.x

Verify ImapSync works ok by running this command:

./imapsync

Now, let’s run an additional sync test between two real imap test maiboxes located at test.lamiral.info.

./imapsync --testslive

If everything is fine, then you are ready to test imapsync live on your server.

Migrate Emails using ImapSync Step by Step

Once you have the same mailboxes and access created on the new server, you must execute this command:

imapsync --host1 "hostname.oldserver.com" --user1 "oldemail" --password1 "originpassword" --host2 "hostname.nixcp.com" --user2 "newemail" --password2 "destinypassword"

On this example, you must replace this variables:

hostname.oldsever.com: with your origin imap host name.
oldemail: your email box to migrate.
originpassword: your original mailbox password on the old server.
hostname.nixcp.com: with your new imap hostname on the new server.
newemail: your destiny box to receive the emails, its the same as "oldemail".

Replace those variables with your real ones and you’ll be able to migrate emails using ImapSync without any problems.

ImapSync Logs

All imapsync logs are stored at /root/LOG_imapsync/ and you can get lot of information about when the email migration process started, when finished, how much time it took, the size of the email box, and total number of emails migrated.

Using more you can explore the imapsync logs to find out how your migration is going:

[[email protected]:~]more [email protected]
Transfer started at Thu Feb  2 12:41:08 2017
PID is 540349
Log file is LOG_imapsync/[email protected] ( to change it, use --logfile path ; or use --nolog to turn off logging )
$RCSfile: imapsync,v $ $Revision: 1.684 $ $Date: 2016/03/17 08:35:03 $ 
Here is a [linux] system (Linux server.nixcp.com Tue Dec 27 17:42:53 EST 2016 x86_64)
Command line used:
/usr/bin/imapsync --host1 oldserver.nixcp.com --user1 [email protected] --password1 MASKED --host2 server.nixcp.com --user2 [email protected] --password2 MASKED
Temp directory is /tmp  ( to change it use --tmpdir dirpath )
PID file is /tmp/imapsync.pid ( to change it use --pidfile filepath ; to avoid it use --pidfile "" )
Modules version list:
Mail::IMAPClient     3.37
IO::Socket           1.31
IO::Socket::IP       ?
IO::Socket::INET     1.31
IO::Socket::SSL      1.31
Net::SSLeay          1.35
Compress::Zlib       2.021
Digest::MD5          2.55
Digest::HMAC_MD5     1.01
Digest::HMAC_SHA1    1.01
Term::ReadKey        2.30
File::Spec           3.3
Time::HiRes          1.9721
Unicode::String      2.09
IO::Tee              0.64
File::Copy::Recursive 0.38
Authen::NTLM         1.09
URI::Escape          3.31
Data::Uniqid         0.12
JSON                 ?
JSON::WebToken       ?
Crypt::OpenSSL::RSA  ?
LWP                  6.15
HTML::Entities       3.64
Filesys::DfPortable  ?
( use --no-modules_version to turn off printing this Perl modules list )
Info: turned ON syncinternaldates, will set the internal dates (arrival dates) on host2 same as host1.
Host1: will try to use LOGIN authentication on host1
Host2: will try to use LOGIN authentication on host2
Host1: imap connexion timeout is 120 seconds
Host2: imap connexion timeout is 120 seconds
Host1: IMAP server [oldserver.nixcp.com] port [143] user [[email protected]]
Host2: IMAP server [server.nixcp.com] port [143] user [[email protected]]
Host1: connecting and login on host1 [oldserver.nixcp.com] port [143] with user [[email protected]]
Host1 failure: can not open imap connection on host1 [oldserver.nixcp.com] with user [[email protected]]: Unable to connect to oldserver.nixcp.com: 
[[email protected]:~]

On big email boxes when you run the command it may seem it is frozen or stuck, but it’s just calculating the files and directories to migrate, after a few minutes it should show you the information on the screen.

If it takes too long, you can check out what is imapsync doing by running:

ps aux | grep imapsync

ImapSync Usage

The command offers lot of options to explore alternative ways to migrate emails using ImapSync, you can fetch all the available options by running

imapsync

Expected output:

[[email protected]:~]imapsync

usage: /usr/bin/imapsync [options]

Several options are mandatory.
str means string
int means integer
reg means regular expression
cmd means command

--dry : Makes imapsync doing nothing, just print what would
be done without --dry.

--host1 str : Source or "from" imap server. Mandatory.
--port1 int : Port to connect on host1. Default is 143, 993 if --ssl1
--user1 str : User to login on host1. Mandatory.
--showpasswords : Shows passwords on output instead of "MASKED".
Useful to restart a complete run by just reading the log.
--password1 str : Password for the user1.
--host2 str : "destination" imap server. Mandatory.
--port2 int : Port to connect on host2. Default is 143, 993 if --ssl2
--user2 str : User to login on host2. Mandatory.
--password2 str : Password for the user2.

--passfile1 str : Password file for the user1. It must contain the
password on the first line. This option avoids to show
the password on the command line like --password1 does.
--passfile2 str : Password file for the user2. Contains the password.

--ssl1 : Use a SSL connection on host1.
--ssl2 : Use a SSL connection on host2.
--tls1 : Use a TLS connection on host1.
--tls2 : Use a TLS connection on host2.
--debugssl int : SSL debug mode from 0 to 4.
...
...
...

Conclusion

Now you know how to migrate emails using ImapSync, it is easy if you do it carefully and you have all the email passwords from each mail box. But it takes more time than generating and restoring a classic full backup from cPanel. However it is the safest way to migrate emails from other control panel servers that are not 100% compatible with cPanel.

About the Author: Santiago Borges

Experienced Sr. Linux SysAdmin and Web Technologist, passionate about building tools, automating processes, fixing server issues, troubleshooting, securing and optimizing high traffic websites.

Leave a Reply

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