Backing up an IMAP Mailbox using imapsync

A client of mine uses GoDaddy’s unlimited IMAP service to host their E-mail. This has been in place since before Gmail was the obvious solution so don’t give me crap about that. The client in question has a pretty high turnover rate and some of the turnovered are higher profile individuals for whom we need to keep their E-mail in case it needs to be later referenced.

For a long time the solution was to just not delete their accounts. Eventually I felt we had too many dangling unreferenced accounts though and wanted to find a way to clean things up. I picked an employee that was unlikely to turnover and explained to her that some new folders were going to show up in her top-level mailbox on her account. No need for alarm, it’s just the archives of the old employees. Enter the 1-liner:

imapsync --host1 imap.secureserver.net --user1 oldemployee@mail.com --password1 supersecretpass --user2 remainingemployee@mail.com --password2 supersecretpass2 --host2 imap.secureserver.net --regextrans2 's#(.*)#user_ark/$1#' 2>&1

This uses a tool called imapsync which is a yum installable on RedHat like systems using EPEL. Obviously some values need to be substituted. The interesting thing that took some figuring out was the regular expression to put in so that the user’s E-mail all ended up in a sub-folder of the remaining user’s account. I discovered I couldn’t put a period within the “user_ark” part which disappointed me. The imapsync program doesn’t seem to behave very well with regards to RegExs, I just took it in stride and achieved what I was able to. Before actually running the command it should be tested with a “–dry” flag so that no changes are made, the output is useful to find out if anything bad or harmful will come of the operation, or if it will even be successful.

On your production system you may find that the server uses a different seperator and/or prefix. This will cause your RegEx to change somewhat. Imapsync is self-documented well when ran without arguments, it just doesn’t behave very well, so experiment with that “–dry” flag before mangling your users’ Inboxes.

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>