How to Migrate Emails Between Servers (IMAP) Without Losing Messages or Folders Using imapsync

How to Migrate Emails Between Servers (IMAP) Without Losing Messages or Folders Using imapsync

The Complete Guide: Migrating Emails with imapsync Without Headaches

When a company changes its email provider (for example, moving from classic hosting to Microsoft 365 or a server managed by ForgeNEX), the biggest fear is always the same: losing important emails, folders, or attachments.

There are many tools that promise to make it easy, but few are as solid as imapsync, a free utility that copies messages from one IMAP account to another without touching the originals and maintaining the entire structure.

Here is a practical guide to using it safely.


1. What is imapsync and why use it?

imapsync is a command-line tool written in Perl that allows you to synchronize two IMAP mailboxes. It is ideal when:

  • You are migrating from an old server to a new one.

  • You are switching providers (e.g., from Gmail or Roundcube to Microsoft 365, Zoho, CyberPanel, etc.).

  • You want to maintain a mirror copy of a mailbox (incremental backup).

Its great advantage: it doesn't delete anything from the source, and you can repeat the operation as many times as you want; it only transfers new or modified messages.


2. Quick Installation

On most Linux systems, you can install it like this:

 
sudo apt update sudo apt install imapsync -y

If it's not in your repository (some older Debian or minimalist systems), you can install it from the source:

 
sudo apt install git make perl git clone https://github.com/imapsync/imapsync.gitcd imapsync sudo make install

To check that it's operational:

 
imapsync --version

3. Basic Migration Example

Let's say you are migrating from an old server (mail.oldserver.com) to a new one (mail.newserver.com).

 
imapsync \ --host1 mail.oldserver.com --user1 [email protected] --password1 'SOURCE_PASSWORD' \ --host2 mail.newserver.com --user2 [email protected] --password2 'DESTINATION_PASSWORD' \ --ssl1 --ssl2 --automap

Key Parameters:

  • --ssl1 --ssl2: forces a secure connection.

  • --automap: detects differences in folder names (e.g., “Sent” vs “Enviados”).

  • --syncinternaldates: preserves the original dates of the emails.

  • --delete2duplicates: avoids duplicate messages if you sync multiple times.


4. Avoiding Blocks from Service Limits

Some services (like Gmail or Office 365) limit the number of IMAP connections or traffic per hour. If you see errors like rate limit or Too many connections, add pauses:

 
--maxbytespersecond 20000 --nofoldersizes --timeout 120

You can also split the migration by critical folders first (Inbox, Sent) and then the rest. Example:

 
--folder INBOX --folder "Sent" 

5. Repeating an Incremental Sync

Once the initial bulk copy is done, you can run the same command again every night during the transition. It will only copy new or modified messages. This way, when you change the DNS or MX records, the mailboxes will already be up to date without any interruptions.


6. Migrating Multiple Accounts Automatically

If you manage many accounts (typical in companies with multiple users), create a file named users.txt with this structure:

 
user1@domain.com;pass1;user1@domain.com;newpass1 user2@domain.com;pass2;user2@domain.com;newpass2

And then:

 
while IFS=';' read u1 p1 u2 p2; doimapsync --host1 mail.oldserver.com --user1 "$u1" --password1 "$p1" \         --host2 mail.newserver.com --user2 "$u2" --password2 "$p2" \         --ssl1 --ssl2 --automap --syncinternaldatesdone < users.txt

7. Validating the Migration

After the copy is complete:

  • Verify the total number of folders and messages on both sides.

  • Open a test account and check old email headers.

  • Use the --dry parameter to simulate the process before a real run.


8. Final Tips

  • Never run bulk migrations without making previous backups.

  • Use an intermediary machine (a VPS or local server) to avoid overloading the source and destination servers.

  • If a server blocks your IP due to excessive attempts, adjust the --maxbytespersecond or the interval.

  • Save logs: --logfile "user.log" helps with debugging and documentation.


Conclusion

imapsync remains, in 2025, the most stable tool for migrating IMAP emails in a controlled and professional manner, without depending on third-party services or limited web interfaces.
It's perfect for SMEs that want to maintain control of their data and for administrators looking for secure and repeatable solutions.

And if you work with panels like CyberPanel, HestiaCP, or Microsoft 365, you can easily integrate it into automation scripts or cron jobs to keep backups synchronized.

Share: