a glob of nerdishness

November 14, 2007

Restore IMAP data from Mail’s offline imapmbox backup

written by natevw @ 9:48 am

Let’s say you come into work and find a note on your desk from your boss: “The mail server went belly up.” All the messages on the IMAP server are gone. What to do?

The first step, and this is very important: Do not open Mail.app until you’ve made a copy of the offline mailbox cache. (You can hold Shift while logging in to keep it from automatically opening.) If you let Mail sync to your now-empty IMAP account, it will erase your offline copies lickety-split. As long as this doesn’t happen, it’s pretty easy to restore the server from your local backup.

  1. Find the corresponding IMAP-user@host folder inside of ~/Library/Mail/. Make a copy somewhere safe, like your Desktop.
  2. Rename all the .imapmbox folders inside of your new copy to have the .mbox extension instead.
  3. Now you can open Mail, and import the main backup folder. Select “File > Import Mailboxes…”, choose the “Mail for OS X” option and then select the modified IMAP-user@host folder.
  4. Move folders back onto the IMAP server. You might need to make one new folder (”Add Mailbox”) on the IMAP server so that it shows in the sidebar, and then you can drag the rest from the Import folder. Any sent messages or todos can be moved to those special mailboxes as well.
  5. If Mail complains that the folders you are trying to drag in already exist, one workaround I found is to delete the IMAP account and set it up again. A simple “synchronize” might have also done the trick.

Once Mail is done uploading the messages, you can delete your “Import” copies of each Mailbox. Then you can get back to seizing the day, whilst hoping you don’t have to do any of this again.


  1. In this, it is important that you give the name of a
    folder where the .mboxes are, and not the .mbox files

    Comment by Peter — November 15, 2007 @ 5:56 am

  2. Unfortunately, when doing the dragging to the server, much
    of the content of the folders does not go over.
    It becomes necessary to drag the messages also.

    Comment by Peter — November 15, 2007 @ 6:03 am

  3. And even then, when dragging them onto the server
    (gmail) many of them get lost on the way.

    Comment by Peter — November 15, 2007 @ 6:44 am

  4. Thank you for your clarification in your first comment. I’ve modified the text above slightly (”the backup folder” -> “the main backup folder”) so hopefully between that and your description it will be clear.

    I did not have any problems with folder contents not uploading completely. At least I hope not! (And I did compare counts on a few.) I wonder, did you wait for all “Mail Activity” to finish? Sometimes it takes a little while to finish uploading, but in the meantime some messages do show up. Re-moving individual messages should not hurt.

    While I don’t think they would cause what you describe, Gmail does have some key differences from a normal IMAP server. Stay tuned for tips and tricks on that!

    Comment by natevw — November 15, 2007 @ 7:07 am

  5. I had to do a recontruct on the mailserver last night – I have a copy of imapmboxes locally & I’ve followed the above, and it has imported the mailboxes, but not their contents, should the contents return once I return the mailboxes to the server

    Comment by James Tipping — November 29, 2007 @ 2:16 am

  6. Wow! I can’t believe it was as simple as changing the extension to .mbox. I spent hours trying all sorts of things before I stumbled upon your site via Google. I had opened Mail.app, but had a complete backup of my HD which I made just before upgrading to Leopard.

    It worked perfectly.

    Comment by James Brown — November 29, 2007 @ 4:46 am

  7. @James Tipping: did you have Mail.app set to “Keep copies of messages for offline viewing” in the Advanced tab, and do you see a bunch of .emlx files in the mailboxes when you look at the folders in the Finder?

    Comment by natevw — November 29, 2007 @ 8:27 am

  8. If you’re using Time Machine (or some other good backup system) you’re OK even if you open Mail.app and let it sync away your lost-from-the-server messages. Just go to your backup and restore that IMAP-user@host folder inside of ~/Library/Mail/.

    Comment by Damian — March 16, 2008 @ 5:21 pm


    Comment by Peter — May 30, 2008 @ 1:06 pm

  10. Wow thanks man! You saved my ass… somehow a folder on my MobileMe account inside Mail got deleted and luckily i have installed “Email Backup” which is an old app that has now changed name to “Email Backup Pro” and costs money.

    My Email Backup has backed up my imap folders and with your guide i retrieved them… :-) thanks!

    Comment by Mark Barner — August 6, 2008 @ 2:30 am

  11. Glad this could be of help to a few others too!

    Comment by natevw — August 7, 2008 @ 8:49 am

  12. Natevw: You’re my hero. I was conversing idly while using Pine and hitting “Y” instead of “N” (multitaskers beware!) and deleted a bunch of sent-mail. This solution works brilliantly for recovering IMAP from the local cache (that I happened to have on a backup drive!). W00t! Thanks for publishing this solution. For those interested, the Pine option in .pinerc to disable the “do you want to delete to save disk space is pruning-rule=ask-no instead of pruning-rule=ask-yes

    Comment by Nick — January 10, 2009 @ 5:52 am

  13. and, for if one has many folders to rename, this will help for i in * ; do mv “$i” `basename “$i” .imapmbox`.mbox ; done

    Comment by Nick — January 10, 2009 @ 8:46 am

  14. Handy shell script and tips, thanks Nick!

    Comment by natevw — January 12, 2009 @ 3:04 pm

  15. Awesome! Thank you for this tip. It saved me.

    Comment by paul — August 15, 2009 @ 7:37 pm

  16. I tried this for my mobileme account but it didn’t import anything. It creates an Import folder with my INBOX and Sent Messages, but I look inside and it is empty. But when I go to the folder I see a folder in each of the .mbox (previously .imapbox) and see my messages in the folder CachedMessages titled 1, 2, 3.1, 3.2, 3.3, 3.partial, 5, 6, 7, etc. to 24. there is also a info.plist, mbox.SKindex and mbox.SKindex.isValid. That is all, there are no Messages folder with .elmx files in them, so I have a feeling that this was made with an earlier version of Mail.app or something. Any way to get these back into mail?

    Cheers, aj

    Comment by Andrew Jung — November 16, 2009 @ 3:19 pm

  17. Thank you so much! – I accidentally deletet an folder on the imap-Server. I’ve tried hours, but with this, I could restore it within Minutes… – Thank you, thank you, thank you!!!

    Comment by Bjoern — January 27, 2010 @ 6:58 am

  18. Many many thanks. Worked a treat. Had thought to rename to mbox but got nervous incase really messed up things. Much appreciated.

    Comment by Bronwyn — August 29, 2010 @ 9:09 pm

RSS feed for comments on this post.

Sorry, the comment form is closed at this time.