Sunday, September 25, 2005

Repair Courier IMAP maildir

A handy little script to repair maildirs.

I stupidly deleted all empty folders in users' homes, realizing too late that it would delete some of the essential new, cur and tmp directories in the maildirs. After that, folders could not be opened anymore. Outlook would say "STATUS failed", and Thunderbird reported "Unable to open this mailbox".

This little script recreated the missing directories:

#!/bin/sh

for M in /home/*/Maildir ; do
echo checking $M
for d in cur new tmp ; do
[ -d "$M/$d" ] || { echo "* Creating $M/$d"; mkdir "$M/$d"; }
chown -c --reference "$M" "$M/$d"
chmod -c 700 "$M/$d"
done
find $M -type f -name maildirfolder | while read ; do
f=`dirname "$REPLY"`
echo " checking $f"
for d in cur new tmp ; do
[ -d "$f/$d" ] || { echo " * Creating $f/$d"; mkdir "$f/$d"; }
chown -c --reference "$f" "$f/$d"
chmod -c 700 "$f/$d"
done
done
done

9 Comments:

Anonymous Anonymous said...

thanks for your little script :o).
i had same problems after running "cleanlinks".
everything is fine again now.

23 January, 2006 22:23  
Anonymous Niki said...

thank you too for this script! helped a lot!

07 February, 2007 03:24  
Anonymous Tilder said...

OMG. I have been looking for a fix to the "STATUS failed" thing ever since I put in courier-imap.

Thank you so very much.

16 June, 2007 04:00  
Anonymous Dairenn Lombard said...

Hello,

I upgraded your script for users of vpopmail with virtual domains, and, it also accounts for missing new/cur/tmp directories in the .Draft/, .Sent/ and .Trash/ directories:


#!/bin/sh

domain=$1

if [ -z "$domain" ] ; then
echo -n "Enter broken domain name: "
read domain
fi

if [ -d "/home/vpopmail/domains/$domain/*/Maildir" ] ; then
echo "I couldn't find /home/vpopmail/domains/$domain/*/Maildir"
echo "Exiting."
exit 1
fi

for M in /home/vpopmail/domains/$domain/*/Maildir ; do
echo checking $M
for d in cur new tmp ; do
[ -d "$M/$d" ] || { echo "* Creating $M/$d"; mkdir "$M/$d"; }
chown -c --reference "$M" "$M/$d"
chmod -c 700 "$M/$d"
done
find $M -type f -name maildirfolder | while read ; do
f=`dirname "$REPLY"`
echo " checking $f"
for d in cur new tmp ; do
[ -d "$f/$d" ] || { echo " * Creating $f/$d"; mkdir "$f/$d"; }
chown -c --reference "$f" "$f/$d"
chmod -c 700 "$f/$d"
done
done
done

for subdir in .Sent .Drafts .Trash ; do
for M in /home/vpopmail/domains/$domain/*/Maildir/$subdir ; do
echo checking $M
for d in cur new tmp ; do
[ -d "$M/$d" ] || { echo "* Creating $M/$d"; mkdir "$M/$d"; }
chown -c --reference "$M" "$M/$d"
chmod -c 700 "$M/$d"
done
find $M -type f -name maildirfolder | while read ; do
f=`dirname "$REPLY"`
echo " checking $f"
for d in cur new tmp ; do
[ -d "$f/$d" ] || { echo " * Creating $f/$d"; mkdir "$f/$d"; }
chown -c --reference "$f" "$f/$d"
chmod -c 700 "$f/$d"
done
done
done
done

Hope someone gets some use out of this.

26 November, 2007 19:27  
Anonymous Anonymous said...

You have saved my arse.....

Your script fix my email beautifully, Thanks so much:)

Randall

12 January, 2008 10:19  
Anonymous Anonymous said...

Yes, i agree all postings here. Very clean and easy. I just surrounded this script with running through all my virtual domains on a plesk-system.

So i added/changed the lines

for DOM in /var/qmail/mailnames/*; do
for M in $DOM/*/Maildir; do

in the beginning and the closing

done

at the end.

21 January, 2008 21:44  
Anonymous Anonymous said...

That vpopmail modified script saved me! THANKS FOR THAT!!!

31 July, 2008 03:23  
Anonymous Anonymous said...

Thank you very much! helped me to repair our companys mail server :)

28 February, 2010 20:33  
Anonymous James Chappell said...

Thanks for this script. Worked first time, saved me googling for yet more hours!

:)

30 May, 2013 00:33  

Post a Comment

Links to this post:

Create a Link

<< Home