How To Migrate Mailboxes Between IMAP Servers With imapsync
This guide explains how you can migrate mailboxes between IMAP servers with imapsync. imapsync is an IMAP synchronisation, sync, copy or migration tool. More than 32 different IMAP server software is supported with success. All flags are preserved, unread will stay unread, read will stay read, deleted will stay deleted.
1 Preliminary Note
I'm using two Debian or Ubuntu servers here (the procedure should be the same for other distributions, only the installation of imapsync will differ):
- server1.example.com (source)
- server2.example.com (target)
I want to migrate the mailbox [email protected] (username: [email protected], password: secret) on server1.example.com to the mailbox [email protected] on server2.example.com (username and password are the same as on server1.example.com).
2 Before We Start
Before we start, let's take a look at both mailboxes to see what's in them (you can do this with an email client such as Outlook, Thunderbird, or a web-based email client such as SquirrelMail - I'm using SquirrelMail here).
server1.example.com:
server2.example.com:
After the migration, the two messages on server1.example.com should be available on server2.example.com.
3 imapsync Installation
It doesn't matter if we install imapsync on server1.example.com or server2.example.com - we can even install it on a third, uninvolved server.
aptitude install imapsync
To learn about its usage, take a look at
man imapsync
and
imapsync --help
4 Doing The Migration
We have to tell imapsync the usernames and passwords of the mail accounts that we want to migrate. Theoretically, we can use the --password1 and --password2 switches for the passwords, but this is dangerous because the passwords would appear in the output of
ps aux
(So any user who is allowed to run that command would know the passwords.)
Therefore it's better to create two files (/etc/secret1 and /etc/secret2 in this example) that contain the passwords (in this example, it's secret for both mail accounts), do a chmod 600 on these files, and use the --passfile1 and --passfile2 switches.
echo "secret" > /etc/secret1
echo "secret" > /etc/secret2
chmod 600 /etc/secret1
chmod 600 /etc/secret2
Now we can do the migration with this simple command:
imapsync --host1 server1.example.com --user1 [email protected] \
--passfile1 /etc/secret1 --host2 server2.example.com --user2 \
[email protected] --passfile2 /etc/secret2
Here's a sample output:
Afterwards we can delete the two password files:
rm -f /etc/secret1
rm -f /etc/secret2
On the target server (server2.example.com), log out of your IMAP account and log back in (a simple refresh might not be enough), and you should see the messages from server1.example.com:
server2.example.com: