Koozali.org: home of the SME Server

Archive old email - solution

Offline Mace

  • **
  • 65
  • +0/-0
Archive old email - solution
« on: October 10, 2006, 08:49:54 AM »
I searched for a solution for archiving old email but didn't find any that worked the way that I wanted. I am an EX-outlook user and one feature I did like was the archiving. It would set up a browsable archive in a different file so you could search or browse and find what you needed while keeping your active mailbox nice and lean.

I'm not very experienced but since I finally came up with something that might be worth sharing I've posted the solution I came up with.

I wrote this for IMAP but I suppose this script could be easily modified to support POP3 by putting all of the archived email in /home/e-smith/files/users/<archive-user>/Maildir/cur instead of creating and moving the mail into IMAP folders.

I created a separate user account to hold my archived emails then wrote the following script called archive-mail that creates all of the IMAP folders you have set up and moves the old email (it doesn't move any unread messages) to the proper folders in the archive account. I run it via a weekly cron job. It should work for any user and can be ran manually as well.
Code: [Select]
if [ -z $3 ]
then
   echo "-------------------------------------------------------"
   echo "Usage:"
   echo "archive-mail <active-user> <archive-to-user> <days>"
   echo
   echo "<days> represents the number of days worth"
   echo "of mail to keep in your active mail folders"
   echo "i.e. 60 will archive all read mail older than 60 days)."
   echo "-------------------------------------------------------"
else
   cd /home/e-smith/files/users/$1/Maildir/
   find . -type d  | xargs -i mkdir -pv /home/e-smith/files/users/$2/Maildir/{}
   find . -type f -mtime +$3 -name \*\:\*S\* -print > "archive-list.tmp"
   cat archive-list.tmp | xargs --max-lines=50 -i mv -fv {} /home/e-smith/files/users/$2/Maildir/{}
   cat archive-list.tmp > "/home/e-smith/files/users/$2/Maildir/mail-archive_`date +'%m-%d-%Y_%H%M%S'`.log"
   rm -f archive-list.tmp
   chown -R $2:$2 /home/e-smith/files/users/$2/Maildir
   echo
   echo "All unread mail older than $3 days successfully archived"
   echo
fi

This script takes (and requires) three parameters. archive-mail <active-user> <archive-to-user> <days>. The first parameter <active-user> is the user account that you're archiving FROM. The second <archive-to-user> is the user account you're archiving TO. The third <days> specifies how much email not to archive in days (i.e. 45 would keep 45 days worth of email, plus any unread messages, in your active account and archive the rest to the archive user account).

Now you can login to the archive user account via IMAP and browse/search all of your archived email as if you were looking at your main account.

I use Thunderbird and have set up both my main and archive accounts in it using IMAP and it has worked flawlessly so far. I set the server properties of the archive account to NOT check mail at startup or at any timed interval.

Like I said, I'm not very experienced so please offer any suggestions or improvements that you may have.

Regards,
Sterling