When you need to transfer mailboxes from one server to another, for example during a migration, imapsync can be a great tool. It allows for incremental/recursive transfer of emails from one mailbox to another via IMAP. By default, it also transfers all the user’s folders, and you can stop the transfer at any time to resume it later.
However, it’s worth noting that it’s not suitable for keeping two IMAP accounts actively synchronized when the user is independently using both sides. In that case, use dedicated tools for that purpose, such as offlineimap (written by John Goerzen) or mbsync (written by Michael R. Elkins) for two-way synchronizations.
Installing imapsync
First, you’ll need to install some dependencies. To do so, you can run the following command:
# apt update && apt install -y libtest-simple-perl libtest-requires-perl libtest-mock-guard-perl libtest-fatal-perl libpar-packer-perl libnet-ssleay-perl libio-compress-perl libdigest-hmac-perl libcrypt-ssleay-perl libssl-dev libauthen-ntlm-perl libclass-load-perl libcgi-pm-perl libcrypt-openssl-rsa-perl libdata-uniqid-perl libencode-imaputf7-perl libfile-copy-recursive-perl libfile-tail-perl libio-socket-inet6-perl libio-socket-ssl-perl libio-tee-perl libhtml-parser-perl libjson-webtoken-perl libmail-imapclient-perl libparse-recdescent-perl libmodule-scandeps-perl libreadonly-perl libregexp-common-perl libsys-meminfo-perl libterm-readkey-perl libtest-mockobject-perl libtest-pod-perl libunicode-string-perl liburi-perl libwww-perl libtest-nowarnings-perl libtest-deep-perl libtest-warn-perl make cpanminus git rcs gcc apt-file; apt-file update
Then, configure some of the required modules for using imapsync:
# cpanm Crypt::OpenSSL::RSA Proc::ProcessTable Crypt::OpenSSL::Random --force
# cpanm Mail::IMAPClient JSON::WebToken Test::MockObject Dist::CheckConflicts
# cpanm Unicode::String Data::Uniqid --force
# cpanm Mail::IMAPClient Crypt::OpenSSL::PKCS12 IO::Socket::SSL JSON::WebToken JSON Crypt::OpenSSL::RSA LWP HTML::Entities Encode::Byte
Now you can download the application or clone it from the official imapsync repository on GitHub:
# cd /opt && git clone https://github.com/imapsync/imapsync.git
And access the imapsync directory and install it:
# cd imapsync && make install
Then you can copy it to the binaries directory:
# cp imapsync /usr/bin/imapsync
To verify the installation, you can run a functionality check:
# imapsync --testslive
Mailbox Synchronization
To perform mailbox synchronization, you can use the following sequence, modifying the hostname or IP address of the servers, as well as the users and their respective passwords (values in bold and italic).
But first, you can perform a synchronization test with the "--dry" option:
# imapsync --dry --host1 mail.domain.com --user1 user@domain.com --password1 "password" --host2 mail2.domain.com --user2 user2@domain.com --password2 "password"
And to perform the actual synchronization, remove "--dry":
# imapsync --host1 mail.domain.com --user1 user@domain.com --password1 "password" --host2 mail2.domain.com --user2 user2@domain.com --password2 "password"
Additionally, you can use the script provided by the developer to synchronize multiple mailboxes in bulk. Here it is:
#!/bin/sh
#
# $Id: sync_loop_unix.sh,v 1.11 2021/10/20 21:21:47 gilles Exp gilles $
# Example for imapsync massive migration on Unix systems.
# See also http://imapsync.lamiral.info/FAQ.d/FAQ.Massive.txt
#
# Data is supposed to be in file.txt in the following format:
# host001_1;user001_1;password001_1;host001_2;user001_2;password001_2;;
# ...
# The separator is the semicolon character ";" and can be changed to any character
# by modifying IFS=';' in the while loop below.
#
# Each line contains 7 columns, which are the 6 parameter values for
# --host1 --user1 --password1 --host2 --user2 --password2
# plus an extra column for additional parameters and a final extra column
# to prevent the CR LF part from going into the 7th extra parameter.
# Don’t forget the last semicolon.
#
# You can add extra options after the "$@" variable
# Use the backslash character \ at the end of each additional line, except the last one.
# You can also pass extra options via the parameters of this script since
# they will be in "$@". Shell knowledge is your friend.
# The credentials filename "file.txt" used for the loop can be renamed
# by changing "file.txt" below.
# The file file_failures.txt will contain the lines from file.txt that ended
# in error, for any reason. It’s there to easily notice and replay
# the failed imapsync runs. I’ll let you figure out how to handle it.
echo Looping through account credentials found in file.txt
echo
line_counter=0
# Clear the error list
file_failures.txt
{ while IFS=';' read h1 u1 p1 h2 u2 p2 extra fake
do
line_counter=`expr 1 + $line_counter`
{ echo "$h1" | tr -d '\r' | egrep '^#|^ *$' ; } /dev/null && continue # this skips commented lines in file.txt
echo "==== Starting imapsync with host1 $h1 user1 $u1 host2 $h2 user2 $u2 $extra $@ ===="
if imapsync --host1 "$h1" --user1 "$u1" --password1 "$p1" \
--host2 "$h2" --user2 "$u2" --password2 "$p2" $extra "$@"
then
echo "Successful sync for line $line_counter "
else
echo "$h1;$u1;$p1;$h2;$u2;$p2;$extra;" | tee -a file_failures.txt
fi
echo "==== Finished imapsync with host1 $h1 user1 $u1 host2 $h2 user2 $u2 $extra $@ ===="
echo
done
}
We hope this article has been helpful, and remember, if you have questions about this or any other issue related to your servers on Clouding, don’t hesitate to contact soporte@clouding.io. We’re here to support you with whatever you need!