Thursday, 12 December 2013

Convert Mbox mail system to Mdir system

Mbox system uses a single file to store the emails while Mdir system uses multiple files to store each incoming email.

1.Go to the Maildir directory for the user.
2.Tar the Maildir file.
3.tar -zcvf Maildir.tar.gz Maildir
4.Copy the files to the mbox mail directory of the user.
5.domain:/home/kk1/mail # scp root@domain.com:/hsphere/local/var/vpopmail/domains/domain.com/k/Maildir.tar.gz .
6.untar the Maildir
7.domain:/home/kk1/mail # tar -zxvf Maildir.tar.gz
8.For the particular directory we are trying to convert, select that. In the case below, we are trying to convert the mails in the sent-mail folder.
9.Create a file /root/convertm with the following contents :
#!/usr/bin/python
# -*- coding: utf-8 -*-

import mailbox
import sys
import email

mdir = mailbox.Maildir(sys.argv [-2], email.message_from_file)
outfile = file(sys.argv[-1], ‘w’)

for mdir_msg in mdir:
# parse the message:
msg = email.message_from_string(str(mdir_msg))
outfile.write(str(msg))
outfile.write(‘\n’)

outfile.close()
10.domain:/home/kk1/mail/Maildir # python /root/convertm .sent-mail ../output.mbox2
11.The file output.mbox2 will contain all the required emails.


I will use an example to explain this. Here, I am going to convert all the mbox style directories which were placed under the old squirrel mail configuration, from the following directory in the old server. This directory belongs to the old email account workreports@domain.com
[root@11-22-33-44 mail]# pwd
/oldserver/home/imap/domain.com/workreports/mail

Step 1:
Transfer the files to the new server.
If ‘dom’ is the user account for the domain.com in the new server,
~# cd ~dom
scp -r root@11.22.33.44:/oldserver/home//imap/domain.com/workreports/mail .
Change the permission of this newly formed mail directory at the new server to 755 ( Another user will operate on this directory which will be explained below. Hence you need to allow read permission )
~#chmod -R 755 mail/
Step 2:
Create a user named convertm ( You can choose any name ) using a tight password and shell access.
Step 3:
Upload the file below into its convertm’s home directory,
mb2mdir
Change directory to the mail directory mentioned in step 1 where you downloaded the emails as user ‘dom’. Now you must have guessed why I allowed read permission on that directory,
~# cd /hsphere/local/home/mail
Step 5:
Issue the following command,
for i in *;do cat $i > /var/spool/mail/convertm && /home/convertm/mb2md-3.20.pl -m /var/spool/mail/convertm && mkdir /home/convertm/.${i} && cp -rf /home/convertm/Maildir/* /home/convertm/.${i}/;done
This command will convert each mailbox file under the mail directory into its mbox format and place it under the directory /home/convertm/
Step 6:
Once it is done, please switch user to ‘root’ and change directory to /home/convertm/
~#cd /home/convertm/
Your challenge is to copy all the .* directories on to /hsphere/local/var/vpopmail/domains/domain.com/workreports/Maildir which is where hsphere horde normally stores its imaped files. Horde by default stores the directories beginning with a ‘.’ character.
Hence, issue the following command which will filter out the bash files and unneeded ‘.’s
for i in `ls -d .* | grep -v ‘^[.]*$’ | grep -v bash`;do cp -rf $i /hsphere/local/var/vpopmail/domains/domain.com/workreports/Maildir/;done
Step 7:
You are done!
Now don’t forget to delete the files after work,
# rm -rf /hsphere/local/home/mail

No comments:

Post a Comment