Yesterday I mentioned testing how well Kerio Connect's new automatic reindexing responds to deliberate abuse. It turns out that it responds well, so I took advantage of that to create an automatic archiving script.
This is a simple need that I am sure Kerio will add someday. The problem is that some users simply will not ever clean up their INBOX. As it grows larger, the system slows down because it has to search the #msgs directory for free slots. Large directories just bog things down; archiving prevents that.
As written here, this should run once a month. It creates dated subfolders of INBOX and moves older email into these. Here i used the "ctime" to decide age, but you could also open each message and use internal dates.
You can see the result here:
The 2011_11_16 folder was by this script created from older messages in INBOX. When it runs next month, it will create another subfolder of INBOX.
As it can take time for the automatic index checking to take place, I have this set to run at 1 minute past midnight on the 1st of the month. By the time I get up in the morning, the reindexing should be long done.
This script is provided without guarantee, warranty or anything else. Use at your own risk and check results carefully. IF YOU DON'T UNDERSTAND THE SCRIPT, YOU PROBABLY SHOULDN'T USE IT!
This script depends apon the background reindexing features introduced in Kerio Connect 7.3 and will not work on earlier versions. It may not work on later versions because of structural changes to Kerio..
Finally, this was tested on a Linux system. Perl can run on Windows but you would need to change the script.
I use the temporary directory with the rename to avoid the reindexer finding messages before everything is copied into place.. this way, it all appears at once.
#!/usr/bin/perl $KERIO="/opt/kerio/mailserver/store/mail/aplawrence.com/"; $SAFE="/root/kdir"; # put it wherever you like @t=localtime(time()-(3600 * 24)); # go back one day chdir($KERIO); foreach (<*>) { next if /#public$/; # add anyone else you want to skip $who=$_; print "$_\n"; $dir=sprintf("%4d_%0.2d_%0.2d",$t[5]+1900,$t[4]+1,$t[3]); exit 1 if (-e "$KERIO/$who/INBOX/$dir"); # already exists if (not -e "$SAFE/$who") { mkdir("$SAFE/$who") or exit 1; } if (not -e "$SAFE/$who/$dir") { mkdir("$SAFE/$who/$dir") or exit 1; mkdir("$SAFE/$who/$dir/#msgs") or exit 1; mkdir("$SAFE/$who/$dir/#assoc") or exit 1; } chdir ("$KERIO/$who/INBOX/"); foreach (<*.fld>) { print "$who $_\n"; system("/bin/cp $_ $SAFE/$who/$dir"); # for windows system("copy $_ $SAFE/$who/$dir"); # I'm not sure this is needed - I think you just need an empty status.fld # doing it this way causes errors about # "two different folders with the same GUID" # Folders work, but those errors are in Error log. # # So, this section can probably just be: # open(STATUS,">$SAFE/$who/$dir/status.fld"); close STATUS; # } chdir("#msgs"); $rightnow=time(); foreach(<*.eml>) { @stats=stat $_; $howold=($rightnow - $stats[10]) / (3600 * 24); # days old next if $howold < 7; print "$who $_ $howold\n"; rename $_,"$SAFE/$who/$dir/#msgs/$_"; } rename "$SAFE/$who/$dir","$KERIO/$who/INBOX/$dir"; }
A more complex script could archive by the correspondent or anything else you might glean from the message content. Your imagination is the only limit.
Got something to add? Send me email.
More Articles by Anthony Lawrence © 2011-11-17 Anthony Lawrence
Printer Friendly Version
Kerio Connect Archive Script Copyright © November 2011 Tony Lawrence
Have you tried Searching this site?
This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more.
Contact us
Printer Friendly Version