Article

How to Migrate Mailboxes Easily With Imapsync on Ubuntu 16.04 LTS

Published on: Fri, Aug 17, 2018 at 11:13 am EST
Email Linux Guides Server Apps Ubuntu

While migrating websites is usually not a problem, sometimes it's more difficult to migrate email boxes. This is especially the case when two servers do not use the same software. Fortunately, to simplify this step, there are many tools, including Imapsync, which is the one we are going to talk about today.

Imapsync tool makes it possible to synchronize mailboxes using the IMAP protocol. This will only work if you also use IMAP to retrieve your emails, or if you do not erase your emails from the server with the POP protocol.

Requirements

  • A sudo user.
  • A Vultr Ubuntu 16.04 LTS server instance.

Before installing any packages on the Ubuntu server instance, it is recommended that you update the system.

sudo apt-get update
sudo apt-get -y upgrade

Installation

First, you need to install the necessary prerequisites to run Imapsync.

sudo apt-get install \
    git                  \
    libauthen-ntlm-perl    \
    libclass-load-perl     \
    libcrypt-ssleay-perl   \
    libdata-uniqid-perl    \
    libdigest-hmac-perl    \
    libdist-checkconflicts-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        \
    libsys-meminfo-perl     \
    libterm-readkey-perl    \
    libtest-fatal-perl      \
    libtest-mock-guard-perl \
    libtest-pod-perl        \
    libtest-requires-perl   \
    libtest-simple-perl     \
    libunicode-string-perl  \
    liburi-perl             \
    make                    \
    cpanminus               \

It may be necessary to update the IMAP and JSON clients with cpanm.

sudo cpanm Mail::IMAPClient
sudo cpanm JSON::WebToken

Download the latest Imapsync code from its official Git repository.

git clone https://github.com/imapsync/imapsync.git

Navigate to the newly cloned repository and install it with the following commands on your system.

cd imapsync
mkdir -p dist
sudo make install

You can check the version of Imapsync using the following command.

imapsync -v

Syncing Emails

The syntax for syncing emails from server1 to server2 using only the required options is shown below.

imapsync --host1 server1.imap.tld --user1 mailbox@email.tld --password1 password1  --host2 server2.imap.tld --user2 mailbox@email.tld --password2 password2

Passwords

To avoid entering plain text mailbox passwords, you can store them in files and use the --passfile1 and --passfile2 arguments to automatically load them from the files containing the passwords.

imapsync --host1 server1.imap.tld --user1 mailbox@email.tld --passfile2 /home/user/pass1 --host2 server2.imap.tld --user2 mailbox@email.tld --passfile2 /home/user/pass2

The connection to the IMAP server

Several options are available to connect Imapsync to your mail server, including the type of connection used. By default, Imapsync will try to use SSL or TLS on port 993 to connect. The following are examples for the connection on the server to migrate (replace 1 with 2 for the second server).

  • --nossl1 for an unencrypted connection via port 143 (not recommended)
  • --ssl1 for an encrypted connection with SSL via port 993
  • --tls1 to connect with TLS via port 993

There are also options to define the type of authentication. This is specified with the --authmech1 and --authmech2 arguments, whose possible values are as follows (in upper case).

  • PLAIN
  • LOGIN
  • CRAM-MD5

Folders

Folder synchronization can cause problems, especially if the default folders, (Inbox, Spam, Archive, Sent), are not named the same on both mail servers. Imapsync offers several options for this:

  • --automap to use automatic default folder recognition.
  • --nomixfolders to avoid merging folders with a similar name according to case sensitivity.
  • --prefix1 to remove a prefix from the folders to migrate.
  • --prefix2 to add a prefix to migrated folders.
  • --skipemptyfolders to not recreate empty folders on the destination server.

Migration options

To speed up email migration, you can use the --usecache argument to temporarily store the folders on the server where you installed Imapsync.

You can also delete messages from the first server with the --delete1 argument.

Finally, before starting your migration, do not hesitate to use the --dry argument to check the result of a command, without executing it.

Want to contribute ?

You could earn up to $300 by adding new articles