Manually convert to Maildir

dannygoh

Verified User
Joined
Feb 9, 2004
Messages
391
Location
Malaysia
Hi,

I have converted to dovecot and one account is deleted all the email while converting.

I have daily backup on my server and it only have mbox format. How do i manually convert the mbox to Maildir for the particular users
 
I dont KNOW for certain, but this line will upgrade all users:

echo "action=convert&value=todovecot" >> /usr/local/directadmin/data/task.queue

it MIGHT be possible to insert a user= to that line - John would have to comment on that however... I'm NOT playing with it at this point - got enough problems with this version of dovecot :(
 
Taken from http://www.qmail.org/mbox2maildir
and modified very lightly:
PHP:
#!/usr/bin/perl
#
# mbox2maildir: coverts mbox file to maildir directory - the reverse of
# maildir2mbox from the qmail distribution.
#
# Usage: mbox2maildir uses the same environment variables as maildir2mbox:
# MAILDIR is the name of your maildir directory; MAIL is the name of your
# mbox file; MAILTMP is ignored.
# WARNING: there is no locking; don't run more than one of these!  you
# have been warned.
#
# based on convert-and-create by Russell Nelson <[email protected]>
# kludged into this by Ivan Kohler <[email protected]> 97-sep-17
 
require 'stat.pl';
 
local $SIG{HUP} = 'IGNORE';
local $SIG{INT} = 'IGNORE';
local $SIG{QUIT} = 'IGNORE'; 
local $SIG{TERM} = 'IGNORE';
local $SIG{TSTP} = 'IGNORE';
 
($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) =
  getpwuid($<);
 
die "fatal: home dir $dir doesn't exist\n" unless -e $dir;
&Stat($dir);
die "fatal: $name is $uid, but $dir is owned by $st_uid\n" if $uid != $st_uid;
 
chdir($dir) or die "fatal: unable to chdir to $dir\n";
$spoolname = "$ENV{MAILDIR}";
-d $spoolname or mkdir $spoolname,0770
  or die("fatal: $spoolname doesn't exist and can't be created.\n");
 
chdir($spoolname) or die("fatal: unable to chdir to $spoolname.\n");
-d "tmp" or mkdir("tmp",0770) or die("fatal: unable to make tmp/ subdir\n");
-d "new" or mkdir("new",0770) or die("fatal: unable to make new/ subdir\n");
-d "cur" or mkdir("cur",0770) or die("fatal: unable to make cur/ subdir\n");
 
open(SPOOL, "<$ENV{MAIL}")
  or die "Unable to open $ENV{$MAIL}\n";
$i = time;
while(<SPOOL>) {
  if (/^From /) {
    $fn = sprintf("new/%d.$$.mbox", $i);
    open(OUT, ">$fn") or die("fatal: unable to create new message");
    $i++;
    next;
  }
  s/^>From /From /;
  print OUT or die("fatal: unable to write to new message");
}
close(SPOOL);
close(OUT);
Usage:
Code:
[server]# MAIL=/var/spool/virtual/[b]domain.com[/b]/[b]user[/b] MAILDIR=/home/[b]username[/b]/imap/[b]domain.com[/b]/[b]user[/b]/Maildir ./mbox2maildir
Just make sure that the directories are chmod 770 and chown username:mail when it's done.

John
 
This is the script that doesn't understand subfolders, changes all the dates and marks all the emails as unread, right?
 
btw if you want to set the dates to "hopefully" the right thing then you can use touch -t YYYYMMDDHHMM.SS filename

you should be able to get the date out of the email from the Delivery-date header

also if you want the files to be marked as unread then change the sprintf command to write to cur/ instead of new/ I think
 
Yes indeed.

I used something like that:
Code:
mb2md -s /home/username/imap/domain/user/mail -R -d /home/username/imap/domain/user/Maildir

There are shorter ways if you manage to run the script as the user (username).

-s is the source
-d is the destination
-R tells it to do it recursively

Dates are kept and emails are correctly put in "cur", so no more unread emails. Also the annoying "DO NOT DELETE" message is automatically removed.
 
I have Redhat 9, Redhat ES3 and Centos 4 and this worked well on all of them.

I created the mb2md.pl file as per the instructions above and copied it to a new folder called mb2mb in my root folder so I could use it as shown below.
remember to chmod it to 777 so you can execute it.

This works for the virtual email accounts.It Works well.
Code:
cd /root/mb2mb
./mb2md.pl -s /var/spool/virtual/yourdomain.com/emailusername -R -d /home/username/imap/yourdomain.com/emailusername/Maildir
cd /home/username/imap/yourdomain.com/emailusername/Maildir/cur
chown username:mail *
chmod 660 *

this is for the site's root email account
Code:
cd /root/mb2md
./mb2md.pl -s /var/spool/mail/username -R -d /home/username/Maildir/
cd /home/username/Maildir/cur
chown username.mail *
chmod 660 *
 
Last edited:
Simple way to convert all IMAP folders (mbox) on server to Maildir

Simple way to convert all at once IMAP folders (mbox) on server to Maildir

Code:
#!/bin/sh

for u in `ls /usr/local/directadmin/data/users`; do
{
    for d in `cat /usr/local/directadmin/data/users/$u/domains.list`; do
    {
        for e in `cat /etc/virtual/$d/passwd`; do
        {
            for r in `echo $e | cut -d: -f1`; do
            {
            mb2md -s /home/$u/imap/$d/$r/mail/mail -R -d /home/$u/imap/$d/$r/Maildir
            }
            done;
        }
        done;
    }
    done;
}
done;

and for all main system accounts

Code:
#!/bin/sh

for u in `ls /usr/local/directadmin/data/users`; do
{
mb2md -s /home/$u/mail/mail -R -d /home/$u/Maildir
}
done;

This works if IMAP folders placed in
/home/username/imap/domain.com/email_user/mail/mail (as for Horde on our
servers). You can change path as you want
 
Last edited:
YOU can't have it both ways - you need to convert all users to maildir at the same time which is what will happen if you use Dovecot.
I did the move to Dovecot last January and never regretted the move. Things are much faster and easier to manage.
 
OK i believe in the conversion, but i put some custom paths in my mailflow, (i had to change var/mail and var/spool/virtual) , that`s actually what is still holding me back.

Can anyone assure that the paths to be used during conversion are paths read from the various directadmin.conf & exim.conf files ?
 
Simple way to convert all at once IMAP folders (mbox) on server to Maildir

Code:
#!/bin/sh
...
mb2md ...
...
Is this script nowadays also used for the 'todovecot'-conversion which is executed by the task.queue? Or is Directadmin still using the old way of converting mbox to maildirs? (which seemed to make some mistakes with time and status)
 
This is what I used for those customers that actually used IMAP on my server. I made a few modifications to the original, so that the time stamps weren't changed and it maintained read/unread status. If it can't figure out the sent date/time it will default to current time. This worked fine for me.

Download the text file rename to perfect_maildir.pl and to use
perfect_maildir.pl /path/to/Maildir < mbox

This would need to be used with a script, to process folders, but it does the mbox conversion better than most other utilities I tried.
 

Attachments

  • perfect_maildir.txt
    5.8 KB · Views: 1,049
Back
Top