Moving emails from old server to new server using bash, python and offlineimap

I had a task of moving emails from an old server to a new server. Considering that stream of emails would be coming in on the live server, rsync didn’t look like a good option to sync the Maildirs.

So, I searched around the Internet for better solutions. The logically better solutions contained using the imap protocol to fetch the emails.

The first tool I came across was imapsync which uses imap to imap sync. But, I had to discard that option as it would involve me knowing passwords for all the users at both ends or resetting passwords on both the servers. There are other tools that would do two way sync. But that was again an overkill for my purpose.

Then I stumbled across offlineimap which is itself written in python. It fetches email from remote imap server and stores it in a local Maildir

First step was to reset the passwords for all the users on the remote server, this is a safe operation as the remote server is not in use anymore and will terminated soon. So, I reset the password of all the users

[gist id=2565856]

Be careful to use the above script as it is. It will reset password for all the users. Modify it to filter as per your requirements

So, now we are all set to setup sync on the local server. First of all, download offlineimap from your distro’s repository. Now we need to set it up to sync the emails. of all the users that exist locally. To do that, we need to add all the users information to the ~/.offlineimaprc file.

Here’s a simple python script to populate the .offlinelimaprc file.

[gist id=2575049]

That’s it. Simple enough, we are done. Now run the offlineimap command to get syncing.

 

Leave a Reply

Your email address will not be published. Required fields are marked *