How to restore a corrupt mailboxes.db file for Cyrus IMAP

Introduction

Every once in a while, Cyrus IMAP will die due to a failure in the mailboxes.db file database data. This problem is typically cause by a read/write failure to the mailboxes.db file, which can occur from situations like a power outage during a write to the file. This can lead to corrupt database indexing data in the file. The solution is to process the file as a flat file and extract the valuable data. Since we are aware of the cyrus IMAP mailboxes.db database schema, we can achieve this goal through a script. The following lists the necessary steps to take to extract and rebuild your mailboxes.db file.

The Recovery Process

Export mailboxes.db data to flat file

Download this script. skiplist.py

To execute this script, you must have python installed. Execute the following script to parse the mailboxes.db file and export the data to a flat file.

python ./skiplist.py mailboxes.db > mailboxes.txt

Backup current mailboxes.db

Execute the following command to backup the current mailboxes.db file. This is important to backup the current mailboxes.db file in case this process does not work. If this is not done, you WILL NOT be able to repeat this process or retrieve data from the old mailboxes.db file. The destination file name can be what you like. I have selected mailboxes.backup since it seems to be a clear. A date in the filename may also be useful if you wish to perform regular backups, or backups upon change of the mailboxes.db file.

mv mailboxes.db mailboxes.backup

Import mailboxes from flat file

cvt_cyrusdb /var/lib/cyrus/mailboxes.txt flat /var/lib/cyrus/mailboxes.db skiplist

Ensure cyrus ownership

The following command is executed to ensure cyrus is the owner of the file. This allows for the cyrus user, and therefore cyrus IMAP, to be able to read the mailboxes.db file.

chown cyrus mailboxes.db

Final Touches

Finally, restart the server or cyrus IMAP to allow for the changes to take effect.

Leave a Reply