The Wild

IMAPSync Mailbox Migration on CentOS

· 5 min read · Mammals

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

Migrating mailboxes between servers is a common task when moving hosting providers, upgrading infrastructure, or consolidating accounts. Using imapsync on CentOS is one of the most reliable ways to copy mailboxes while preserving folders, flags, and dates. This guide walks you through a safe, step-by-step migration using imapsync, from installing the tool to verifying the final results.

What You'll Need

  • A CentOS server (7, 8, or 9) with root or sudo access.
  • Access to both the source and destination IMAP servers (hostname, port, username, password).
  • IMAP must be enabled on both servers (usually port 143 for plain, 993 for SSL/TLS).
  • Enough disk space on the CentOS machine to store temporary files if you use the --tmpdir option (optional).
  • An internet connection to download imapsync and its dependencies.

Step-by-Step

  1. Install required packages. Open a terminal and run:
    sudo yum install epel-release -y
    sudo yum install git cpanminus -y
    On CentOS 8/9, use dnf instead of yum. EPEL provides many Perl modules needed.
  2. Install imapsync from source. The official repository is on GitHub. Clone it and install dependencies:
    cd /opt
    git clone https://github.com/imapsync/imapsync.git
    cd imapsync
    sudo cpanm --installdeps .
    This will install all Perl modules required. If any fail, check the Troubleshooting section.
  3. Make imapsync executable. The script is already executable, but verify:
    chmod +x imapsync
    ./imapsync --version
    You should see version information. If not, ensure Perl is installed (sudo yum install perl).
  4. Test connectivity to both servers. Use a simple IMAP client or telnet to ensure you can reach them:
    telnet source.example.com 993
    If telnet is not installed, use nc -vz source.example.com 993. This step helps avoid timeouts later.
  5. Run a dry-run migration. Before the real migration, test with a single mailbox and the --dry flag:
    ./imapsync --host1 source.example.com --user1 olduser --password1 'pass1' --host2 dest.example.com --user2 newuser --password2 'pass2' --dry
    This will show what would be done without making changes. Check the output for errors.
  6. Perform the actual migration. Remove the --dry flag. For a typical migration, add useful flags:
    ./imapsync --host1 source.example.com --user1 olduser --password1 'pass1' --host2 dest.example.com --user2 newuser --password2 'pass2' --syncinternaldates --useheader 'Message-ID' --addheader --regextrans2 's/\/INBOX/INBOX/'
    • --syncinternaldates preserves the original receive dates.
    • --useheader 'Message-ID' helps avoid duplicates.
    • --addheader adds a header to identify migrated messages (optional).
    • --regextrans2 fixes folder name issues if needed.
    Run this for each mailbox. You can automate with a loop if you have many.
  7. Monitor the progress. imapsync prints a lot of output. Watch for lines like Messages transferred: X and Errors: 0. If errors appear, note them for troubleshooting.
  8. Verify the migration. Log into the destination mailbox using a webmail client or IMAP client. Check that folders, subfolders, and message counts match the source. You can also run imapsync again with the same command; it should find no new messages to copy (or only a few if new mail arrived).
  9. Update DNS and client settings. Once verified, change your email client (Outlook, Thunderbird) to point to the new server. Update MX records if the mail server hostname changed. Keep the old server running for a few days to catch any missing messages.

Common Mistakes

  • Running without testing: Skipping the dry-run can lead to unexpected folder name changes or authentication failures.
  • Incorrect password quoting: Passwords with special characters (like $, !, &) must be quoted in single quotes to prevent shell expansion.
  • Using wrong port: If you use SSL (993) but don't specify --ssl1 or --ssl2, imapsync will try plain IMAP and fail. Always check the server's supported method.
  • Ignoring folder separators: Some servers use '.' as separator, others use '/'. Use --sep1 and --sep2 if needed.
  • Forgetting to enable IMAP on destination: Many hosting control panels have IMAP disabled by default. Ensure it's active.

Tips

  • Run imapsync as a non-root user (e.g., sudo -u imapsync) to avoid potential permission issues with temporary files.
  • Use --tmpdir /var/tmp to avoid filling up /tmp on small systems.
  • For large mailboxes, run the migration during off-peak hours to reduce load on the source server.
  • Consider using --maxbytespersecond 1000000 to throttle bandwidth if the source server is sensitive.
  • Keep a log file: ./imapsync ... > migration.log 2>&1 so you can review later.
  • If you have hundreds of mailboxes, write a shell script that reads a CSV file with usernames and passwords.

Troubleshooting

  • Authentication failed: Double-check the username and password. For some servers, the username must include the domain (e.g., user@example.com). Also, ensure IMAP is enabled for that account.
  • SSL certificate errors: If you get “certificate verify failed”, add --ssl1 --ssl2 and optionally --sslargs1 'SSL_verify_mode => 0' for testing (not recommended for production). Better to fix the certificate.
  • Connection timeouts: Add --timeout1 120 --timeout2 120 to increase the time imapsync waits for a response. Also check firewall rules on both servers.
  • Folder name issues: If folders appear with double slashes (e.g., INBOX//Sent), use --regextrans2 's/\/\//\//g' to clean them.
  • Perl module installation fails: If cpanm --installdeps fails, you may need to install missing system libraries. For example, sudo yum install openssl-devel for SSL modules. Check the error message and install the corresponding -devel package.
  • Duplicate messages: If imapsync copies messages that already exist, add --useheader 'Message-ID' and --skipcrossduplicates to avoid duplicates.

Frequently Asked Questions

Does imapsync work with any IMAP server?

Yes, imapsync works with any IMAP server that follows standard protocols, including Dovecot, Courier, Exchange, Zimbra, and Gmail (with app passwords).

Can I migrate just a few folders instead of the whole mailbox?

Yes, use the --folder option to specify a folder, or --include with a regex to select multiple folders. For example: --include '^INBOX/Sent'.

Will imapsync preserve read/unread status?

Yes, by default imapsync copies the lag flag (read/unread) and other flags. It also preserves the internal date when you use --syncinternaldates.

How long does a migration take?

It depends on the number of messages and their size. A typical mailbox with a few thousand emails might take minutes, while a 10GB mailbox could take hours. You can estimate by running a test with a small folder.

Can I run imapsync on Windows?

Yes, but this guide is for CentOS. On Windows, you can use Strawberry Perl and follow similar steps, or use the pre-built Windows executable from the imapsync website.

What if I need to migrate from an old server that is going offline soon?

Run the migration as soon as possible, then run a final sync just before decommissioning. imapsync will only copy new messages since the last run, so it's quick.