How to Migrate Emails with imapsync

Need to migrate your emails from one provider to another? Want to move your Gmail to an alternative provider? Then it's time to learn about imapsync

If you need to move your emails from one provider to another or if you just need to backup your IMAP account, imapsync is a great tool for that.

If you have a G Suite no-fee legacy edition account and do not need / cannot afford Google Workspace and found an alternative email provider where to migrate your emails, now would be a good time to learn how to move emails using IMAP protocol.

What is imapsync?

imapsync is an IMAP transfer tool. The purpose of imapsync is to migrate IMAP accounts or to backup IMAP accounts. 

Fore more information, check the official imapsync website:
https://imapsync.lamiral.info

How to install imapsync on Ubuntu?

The lines below will install imapsync in the current directory (and before that they will install lots of prerequisites). The final line will test if everything is OK.

sudo apt-get install git rcs make makepasswd libtest-warn-perl time  \
libauthen-ntlm-perl libclass-load-perl libfile-copy-recursive-perl   \
libcrypt-ssleay-perl libdata-uniqid-perl libdigest-hmac-perl         \
libdist-checkconflicts-perl libencode-imaputf7-perl                  \
libfile-tail-perl libio-compress-perl libio-socket-inet6-perl        \
libio-socket-ssl-perl libio-tee-perl libmail-imapclient-perl         \
libmodule-scandeps-perl libnet-dbus-perl libnet-ssleay-perl          \
libpar-packer-perl libreadonly-perl libregexp-common-perl            \
libsys-meminfo-perl libterm-readkey-perl libtest-fatal-perl          \
libtest-mock-guard-perl libtest-mockobject-perl libtest-pod-perl     \
libtest-requires-perl libtest-simple-perl libunicode-string-perl     \
liburi-perl libtest-nowarnings-perl libtest-deep-perl

sudo cpanm Crypt::OpenSSL::RSA Crypt::OpenSSL::Random --force
sudo cpanm Mail::IMAPClient JSON::WebToken Test::MockObject 
sudo cpanm Unicode::String Data::Uniqid

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

cd imapsync
mkdir -p dist
sudo make install

./imapsync --testslive

How to install imapsync on Rocky Linux / CentOS?

The lines below will install imapsync in the current directory (as you can see from the first command, before actually installing imapsync, other prerequisites will be installed). The final line will test if everything is OK.

dnf install wget perl-App-cpanminus gcc
wget https://imapsync.lamiral.info/imapsync
perl -Mlazy imapsync
chmod +x imapsync

./imapsync --testslive

How to use imapsync to migrate emails?

When running imapsync, the software connects to the source and target servers and moves the emails. You need to provide the connection information from both servers and the parameters are:

–host1 (old-server.example.com)
–user1 (olduser@example.com)
–password1 (somepassword)
–ssl1 (use if SSL is required)
–host2 (new-server.example.com)
–user2 (newuser@example.com)
–password2 (somepassword)
–ssl2  (use if SSL is required)

Here is an example:

imapsync --host1 source-server.example123.com --user1 some.user@example123.com --password1 1234 --ssl1 --host2 target-server.example789 --user2 any.user@example789.com --password2 abcd --ssl2

You can re-run the script several times and only the new (not yet synced) emails will be copied. There are several other parameters that you can use. You can restrict the date range for which you are doing the migration. Here is an example:

imapsync ... --search "SENTSINCE 1-Jan-2020 SENTBEFORE 31-Dec-2021"

When migrating from Gmail / G Suite you should skip the All Mail folder. You can add other folders to the skip list (e.g. Important), too. If you don’t do this you will get duplicate email since those emails will also be in Inbox, etc.

imapsync ... --exclude 'All Mail|Important'

How to use imapsync to backup emails?

imapsync cannot backup to a local file/repository. The workaround to use this tool for backup is to copy the emails into a subfolder of another IMAP account.

For more details on how to use the tool, you can review the official documentation:
https://imapsync.lamiral.info/#doc

Leave a Reply