so you have a user who by accident deleted their emails and you have no backups. oh dear you say? well not if you are the mail administrator.

i’m running postfix with mailscanner. every mail that passes through gets archived, except high scoring spam ofcourse. the problem that i had was that i set the archiving to save it as a raw queue file so i could not just copy it into the mail queue and sends it off to the relavent people who it was intended for.

in anycase, i had to somehow search through the archive mail for the user’s email address, convert it into a format that i can just copy into the user’s mailbox without clogging the mail queue. yes, this will still duplicate those emails that were not lost, but that’s the user’s issue, not mine.

this is how find, postcat, sed and grep saved my life.

find and copy the user’s email and then convert it. (note – some of the directories you will have to create)

first we change directory to the archives

pop:~# cd /var/spool/MailScanner/archive/

then we use find and grep to search for the user’s email address and pipe it to a file

pop:~# find ./ -name “*” -exec grep -H user@domain.com {} \; > /root/extract/users/user/find.txt

then we cat the file and make it a little bit more neater to use

pop:~# cat /root/extract/users/user/find.txt | cut -c15-40 > /root/extract/users/user/user.txt

then we write a while command to actually move the emails to another directory

pop:~# while read i; do cp $i /root/extract/users/user/sort/ ; done < /root/extract/users/user/user.txt

then we change to that directory

pop:~# cd /root/extract/users/user/sort/

then we pipe the files in the directory to a file

pop:~# ls > ../list.txt

now we convert the emails to a format that you can just copy into the user’s mailbox on the mailserver

pop:~# while read i; do `postcat -v $i | grep “regular_text:” | sed “s/regular_text: //g” > ../extracted/$i.mbox` ; done < ../list.txt

now we move the emails to the user’s mailbox

pop:~# mv ../extracted/ /home/vpopmail/domain/user/Maildir/new/

change the permission on the folder

pop:~# chown -R postfix:postfix /home/vpopmail/domain/user/Maildir/new/

Share and Enjoy:
  • Print
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Add to favorites
  • FriendFeed
  • LinkedIn
  • Posterous
  • Reddit
  • RSS
  • Slashdot
  • StumbleUpon
  • Tumblr
  • Twitter
  • Blogplay