Migrating email accounts from one hosting server to another is often a nightmare for IT administrators and hosting resellers. The traditional method involving Outlook (downloading emails to a local PST file and re-uploading them) is slow, prone to errors, and consumes massive amounts of local disk space.
In this tutorial, I will show you how to migrate emails efficiently using Imapsync. This tool transfers emails directly between servers using the IMAP protocol, preserving your folder structure and saving your local storage.
What is Imapsync?
Imapsync is a command-line tool for Linux that synchronizes mailboxes between two IMAP servers. It is "idempotent," meaning you can run it multiple times without creating duplicate emails—it simply resumes where it left off.
Prerequisites
- A computer with a Linux terminal (If you are on Windows, use WSL - Windows Subsystem for Linux).
- The hostname of the old server (e.g.,
old.server.com). - The hostname of the new server (e.g.,
new.server.com). - Usernames and passwords for the email accounts on both servers.
Step 1: The Password File Trick (Crucial!)
Many email passwords contain special characters like @, $, or !. If you type these directly into the command line, Linux might misinterpret them, causing errors. The safest way is to save the password in a text file.
Open your terminal and create a password file (assuming all accounts use the same migration password):
echo 'YourSecurePassword123' > pass.txt
This creates a file named pass.txt containing your password.
Step 2: Migrating a Single Account
To migrate a single email account, use the following command structure. Note that we are using --passfile instead of typing the password directly.
./imapsync --host1 old.server.com --user1 info@old-domain.com --passfile1 pass.txt --ssl1 \
--host2 new.server.com --user2 info@new-domain.com --passfile2 pass.txt --ssl2--dry flag at the end of the command first. This performs a "simulation" to check if the credentials and connection are correct without actually moving any data.Step 3: Batch Migration (Automating Multiple Accounts)
If you have 10, 15, or 50 email accounts, typing the command one by one is tedious. Here is a Bash script I used to migrate 15 accounts automatically.
Create a file named migrate_all.sh using the command nano migrate_all.sh and paste the following code:
#!/bin/bash
# --- Configuration ---
HOST1="premium264.web-hosting.com" # Old Server
HOST2="premium909.web-hosting.com" # New Server
PASSFILE="pass.txt"
DOMAIN1="old-domain.com"
DOMAIN2="new-domain.com"
# --- List of Usernames ---
# Add your email usernames here (the part before the @)
declare -a users=(
"admin"
"info"
"sales"
"hr"
"support"
"ceo"
# Add as many users as you need
)
# --- The Loop ---
for user in "${users[@]}"; do
USER1="${user}@${DOMAIN1}"
USER2="${user}@${DOMAIN2}"
echo "========================================="
echo "Syncing: $USER1 ---> $USER2"
echo "========================================="
./imapsync --host1 $HOST1 --user1 $USER1 --passfile1 $PASSFILE --ssl1 \
--host2 $HOST2 --user2 $USER2 --passfile2 $PASSFILE --ssl2 --noauthmd5
echo "Completed: $USER1"
echo "Waiting 5 seconds..."
sleep 5
done
echo "Batch Migration Completed!"Save the file and run it using: bash migrate_all.sh
Pro Tip: Migrating "Recent" Emails First
If you have huge mailboxes (e.g., 10 years of data), migrating everything at once might take days. To let your clients start working immediately, you can migrate only the last 3 months (90 days) of emails first.
Simply add the --maxage 90 flag to your command:
./imapsync ... [other options] ... --maxage 90
Once the recent emails are transferred, simply remove the --maxage 90 flag and run the script again. Imapsync will skip the emails that are already there and download the older ones in the background.
Important Considerations
- Disable Sleep Mode: Since the data passes through your computer (Server A > Your PC > Server B), ensure your computer does not go to sleep during the process.
- Bandwidth: This process uses your internet bandwidth. If you migrate 30GB of data, you will download 30GB and upload 30GB.
- Resuming: If your internet connection drops, don't worry. Just run the script again. Imapsync will pick up exactly where it left off.
Using imapsync has saved me hours of work and prevented local storage issues. I hope this guide helps you with your next server migration!
The Ultimate Guide to Server-to-Server Email Migration Using Imapsync
Migrating email accounts from one hosting server to another is often a nightmare for IT administrators and hosting resellers. The traditional method involving Outlook (downloading emails to a local PST file and re-uploading them) is slow, prone to errors, and consumes massive amounts of local disk space.
In this tutorial, I will show you how to migrate emails efficiently using Imapsync. This tool transfers emails directly between servers using the IMAP protocol, preserving your folder structure and saving your local storage.
What is Imapsync?
Imapsync is a command-line tool for Linux that synchronizes mailboxes between two IMAP servers. It is "idempotent," meaning you can run it multiple times without creating duplicate emails—it simply resumes where it left off.
Prerequisites
- A computer with a Linux terminal (If you are on Windows, use WSL - Windows Subsystem for Linux).
- The hostname of the old server (e.g.,
old.server.com). - The hostname of the new server (e.g.,
new.server.com). - Usernames and passwords for the email accounts on both servers.
Step 1: The Password File Trick (Crucial!)
Many email passwords contain special characters like @, $, or !. If you type these directly into the command line, Linux might misinterpret them, causing errors. The safest way is to save the password in a text file.
Open your terminal and create a password file (assuming all accounts use the same migration password):
echo 'YourSecurePassword123' > pass.txt
This creates a file named pass.txt containing your password.
Step 2: Migrating a Single Account
To migrate a single email account, use the following command structure. Note that we are using --passfile instead of typing the password directly.
./imapsync --host1 old.server.com --user1 info@old-domain.com --passfile1 pass.txt --ssl1 \
--host2 new.server.com --user2 info@new-domain.com --passfile2 pass.txt --ssl2
--dry flag at the end of the command first. This performs a "simulation" to check if the credentials and connection are correct without actually moving any data.
Step 3: Batch Migration (Automating Multiple Accounts)
If you have 10, 15, or 50 email accounts, typing the command one by one is tedious. Here is a Bash script I used to migrate 15 accounts automatically.
Create a file named migrate_all.sh using the command nano migrate_all.sh and paste the following code:
#!/bin/bash
# --- Configuration ---
HOST1="premium264.web-hosting.com" # Old Server
HOST2="premium909.web-hosting.com" # New Server
PASSFILE="pass.txt"
DOMAIN1="old-domain.com"
DOMAIN2="new-domain.com"
# --- List of Usernames ---
# Add your email usernames here (the part before the @)
declare -a users=(
"admin"
"info"
"sales"
"hr"
"support"
"ceo"
# Add as many users as you need
)
# --- The Loop ---
for user in "${users[@]}"; do
USER1="${user}@${DOMAIN1}"
USER2="${user}@${DOMAIN2}"
echo "========================================="
echo "Syncing: $USER1 ---> $USER2"
echo "========================================="
./imapsync --host1 $HOST1 --user1 $USER1 --passfile1 $PASSFILE --ssl1 \
--host2 $HOST2 --user2 $USER2 --passfile2 $PASSFILE --ssl2 --noauthmd5
echo "Completed: $USER1"
echo "Waiting 5 seconds..."
sleep 5
done
echo "Batch Migration Completed!"
Save the file and run it using: bash migrate_all.sh
Pro Tip: Migrating "Recent" Emails First
If you have huge mailboxes (e.g., 10 years of data), migrating everything at once might take days. To let your clients start working immediately, you can migrate only the last 3 months (90 days) of emails first.
Simply add the --maxage 90 flag to your command:
./imapsync ... [other options] ... --maxage 90
Once the recent emails are transferred, simply remove the --maxage 90 flag and run the script again. Imapsync will skip the emails that are already there and download the older ones in the background.
Important Considerations
- Disable Sleep Mode: Since the data passes through your computer (Server A > Your PC > Server B), ensure your computer does not go to sleep during the process.
- Bandwidth: This process uses your internet bandwidth. If you migrate 30GB of data, you will download 30GB and upload 30GB.
- Resuming: If your internet connection drops, don't worry. Just run the script again. Imapsync will pick up exactly where it left off.
Using imapsync has saved me hours of work and prevented local storage issues. I hope this guide helps you with your next server migration!

Post a Comment