Articles August 24, 2004 at 6:37 am

Cyrus IMAP Mailbox Recovery

I’ve occasionally encountered IMAP mailboxes that were unreadable and undeletable
using any mail client. In Mac OS X Mail, this usually produces an “Unable to Select” error.

Here’s how to fix the server-side mailbox. At the bottom is a bonus: how to recover the locally cached .imapmbox file from Mail.

I learned how to do these steps from two web pages: Bynari Support and Code Comments.

Step 1: Backup the locally-cached mail

The file will be inside ~/Library/Mail/IMAP-email-address, with an extension “.imapmbox”. Copy this file to your Desktop.

Step 2: Repairing the Cyrus folder

  1. Use Server Admin to stop the mail server
  2. Change to root, and enter the directory /var/imap:
    # sudo -s
    # cd /var/imap
  3. backup mailboxes.db:
    # cp mailboxes.db mailboxes.db-backup
  4. Using Workgroup Manager, set user cyrus’s shell to bash
  5. (Ed. note: Rather than give cyrus a shell you could just use sudo -u cyrus to do the following steps.)

  6. su to cyrus, and dump the database to a text file:
    # su cyrus
    # /usr/bin/cyrus/bin/ctl_mboxlist -d > /tmp/mboxlist.txt
  7. Edit the text file and remove the lines that contain the problem folders:
    # pico /tmp/mboxlist.txt
    
  8. remove mailboxes.db:
    # rm mailboxes.db
  9. rebuild mailboxes.db from the new text file:
    # cat /tmp/mboxlist.txt | /usr/bin/cyrus/bin/ctl_mboxlist -u
  10. Reconstruct all mailboxes
    # /usr/bin/cyrus/bin/reconstruct -r -f user

    If there are any remaining folders that print an error, they’ll be listed when performing cyrus reconstruct: (ie.. user.someuser: Mailbox does not exist).

  11. Exit back from cyrus and root
    # exit
    # exit
  12. Using Workgroup Manager, set user cyrus’s shell back to /usr/bin/false.
  13. Restart Mail using Server Admin

Step Three: Recovering a locally-cached imapmbox

  1. Create a fake imap email account. Don’t use a real name or password. We want the account to remain offline.

  2. Quit Mail

  3. In the Finder, open the directory holding the newly-created INBOX.imapmbox file (It’s in ~/Library/Mail/IMAP-something/”).

  4. Move this (empty) INBOX.imapmbox to the trash.

  5. Move the cached mail file you had copied to the Desktop to this folder. Rename the file to “INBOX.imapmbox”.

  6. Start Mail again, and click on the new account name. Your messages should reappear.

  7. Copy the messages from this folder to another one, either IMAP or locally.

  8. In Mail’s preferences, delete the account you had created.

Make sure you keep this fake account offline, or else Mail will overwrite your cached mailbox with an empty one!

No Comments

  • Take a closer look at step 4.

    You need to either give cyrus a shell or use
    sudo -u cyrus.


    Breaking my server to save yours.

    Josh Wisenbaker
    http://www.afp548.com

  • you know I had a problem mailbox on my mailserver too. I followed the
    step and got the following output if I used “sudo -u cyrus” on it’s own.
    I’m guessing it would stem back to the cyrus user not having a shell specified
    in OD. Similar problems can surface if running postgres without a valid shell.

    gonzo:/var/imap root# sudo -u cyrus
    usage: sudo -V | -h | -L | -l | -v | -k | -K | [-H] [-P] [-S] [-b] [-p prompt]
                [-u username/#uid] -s | <command>

    what I did was use “sudo -u cyrus /bin/bash” to get the cyrus user a shell. I
    was then able to run all the rest of the commands. a quick execution of the id
    command verified that were indeed running as the cyrus user:

    
    gonzo:/var/imap cyrus$ id
    uid=77(cyrus) gid=6(mail) groups=6(mail)
    


    What is pirate software? Software for inventory of stolen
    treasure?

  • To reconstruct one mailbox just use this command
    sudo -u cyrus /usr/bin/cyrus/bin/reconstruct -r -f user/xxx

    I found this at:
    http://www.macosxhints.com/article.php?story=20040914201805804

    and remember to stop the server before doing this

  • Step Three doesn’t work under 10.5

Leave a reply

You must be logged in to post a comment.