Friday 2 September 2011

Importing a whole bunch of MH mail files into dovecot

I had some old MH mail that I recovered from backup that I wanted to import into my dovecot server. This is personal mail so I will only describe how to do it for my account. The MH file paths were of the form:


from/alice/1
from/alice/2
from/bob/1
from/carol/1


I wanted to import the alice mail into the logical dovecot mail folder Archives/from/alice and similarly for the others. (Archives is a Thunderbird standard.) In my dovecot setup however, the corresponding maildir directory is ~/Mail/.Archives.from.alice


First I shut down Thunderbird so that it wouldn't interfere during the second step later. This script does the job (backslash continuation inserted for clarity):


for d in from/*
do
  for f in $d/[0-9]*
  do
    /usr/lib/dovecot/deliver -s -m \
      Archives.${d/\//.} -p $f
  done
done


See the documentation of deliver for the meanings of the -s -m and -p options.


You can track the progress of the import by following the syslog file for mail. Any failed imports go into INBOX.


This second step is not required, but I am obsessive. I wanted each imported file to have the same timestamp as the old one. This script uses findup from the fslint toolkit to collect the duplicates (backslash continuation inserted for clarity):


/usr/local/src/fslint-2.42/fslint/findup \
  from ~/Mail/.Archives.*.* > /tmp/dups


This generates pairs of duplicates in /tmp/dups separated by a blank line. This script then applies the old timestamp to each new file.


while read a
do
  read b
  touch -r $a $b
  read c
done < /tmp/dups


I still have to figure out how to mark all the old mail read from the CLI. I just went into Thunderbird and marked each folder read. If you have a good solution, please do comment.