So I was migrating a site of mine to a new host, but I had to move a hell of a lot of emails across. The big problem is that I was moving from one setup (a custom one) to a very different one (cPanel’s). To save me from headaches, I needed something that was as agnostic as possible regarding server configuration. Well, thankfully there’s a wonderful Linux tool called imapsync which uses the IMAP protocol to do it all.
The basic syntax is very simple:
imapsync --host1 the.first.host --user1 "first-username" \ --passfile1 first-password-file --host2 the.second.host \ --user2 "second-username" --passfile2 second-password-file
It looks complex but it’s quite simple. As you are taking emails from one server and pushing them to another, the options are rather logical:
- --host1 - the hostname of the current email server
- --user1 - the username to log in as
- --passfile1 - the plaintext file that contains the password. There is an option to supply the password via commandline argument but this is a security risk (anyone else running the ‘ps’ command would be able to see it)
As you can imagine, the –host2, –user2, etc options are for the server being migrated to.
This is a very flexible solution as you can run is on a completely different host from the originating and destination servers. The script will also go and create the directories during the migration process, which is very handy. The only real downside is that as it has to connect to the servers via IMAP, it can take a long time if you have lots of folders and emails to move.