Mass convert and migrate mails from mbox to maildir destination

Written by - 0 comments

Published on - Listed in Mail Linux

A couple of days ago I have had an issue on a mail server. Incoming mails have the following flow on this server:

Postfix -> Procmail -> Spamassassin -> Dovecot LDA -> Maildir of user

After a system reboot the logfile of Dovecot's LDA (Local Delivery Agent) didn't have correct permissions and dovecot couldn't write into it anymore.
Luckily I have the procmail log enabled, so I saw the following error message appearing since the reboot:

From  Sat Feb  1 17:09:13 2014
 Subject: *****SPAM***** =?UTF-8?B?RWlud2FuZGVydW5nOiBFcyBnZWh0IHVtIDIgVmVyZmFz
  Folder: /var/mail/web90p7                                               64318
Can't open log file /var/log/dovecot/dovecot-deliver.log: Permission denied
procmail: Program failure (75) of "/usr/lib/dovecot/deliver"

This caused that Dovecot LDA refused to deliver the mail. Instead the mail was saved into /var/mail/web90p7 in mbox format.

To fix the issue, I temporarily chmodded the dovecot logfile (/var/log/dovecot/dovecot-deliver.log) to 777. Incoming mails then were correctly delivered into the maildir destination again.

But I needed to find a solution for the mails stored in mbox format: I needed to place them into the correct maildir destination.
In the past I already had to do mailserver migrations from mbox to maildir, so I knew the script mb2md (mailbox to maildir) would probably help me.

To automate the task, I copied all mbox files to a save place (into /tmp/mailrestore) and then launched this one-liner:

for mbox in $(ls | grep web); do /tmp/mailrestore/ -s /tmp/mailrestore/$mbox -d /tmp/mailrestore/mailrestore-$mbox-maildir/; chown -R $mbox:poponly /tmp/mailrestore/mailrestore-$mbox-maildir; find /tmp/mailrestore/mailrestore-$mbox-maildir/ -type f -mtime 0 -exec mv {} /home/mail/$mbox/Maildir/new/ \; ; done

1. Go through all files found containing "web" -> all the mbox files
2. For each found mbox file, launch mb2md with the source (-s) path of the mbox file and the destination (-d) path where to store the found mails. I chose to stay in /tmp/mailrestore (I could've used the real destination path, too).
3. Set correct ownership of the created maildir
4. Search for files (mails) created within the last 24 hours in the created maildir and move them into the user's real maildir path. I placed them directly in the "new" subfolder so the mails would appear as unread for the user.

Done :-)

Add a comment

Show form to leave a comment

Comments (newest first)

No comments yet.

RSS feed

Blog Tags:

  AWS   Android   Ansible   Apache   Apple   Atlassian   BSD   Backup   Bash   Bluecoat   CMS   Chef   Cloud   Coding   Consul   Containers   CouchDB   DB   DNS   Database   Databases   Docker   ELK   Elasticsearch   Filebeat   FreeBSD   Galera   Git   GlusterFS   Grafana   Graphics   HAProxy   HTML   Hacks   Hardware   Icinga   Influx   Internet   Java   KVM   Kibana   Kodi   Kubernetes   LVM   LXC   Linux   Logstash   Mac   Macintosh   Mail   MariaDB   Minio   MongoDB   Monitoring   Multimedia   MySQL   NFS   Nagios   Network   Nginx   OSSEC   OTRS   Office   PGSQL   PHP   Perl   Personal   PostgreSQL   Postgres   PowerDNS   Proxmox   Proxy   Python   Rancher   Rant   Redis   Roundcube   SSL   Samba   Seafile   Security   Shell   SmartOS   Solaris   Surveillance   Systemd   TLS   Tomcat   Ubuntu   Unix   VMWare   VMware   Varnish   Virtualization   Windows   Wireless   Wordpress   Wyse   ZFS   Zoneminder