APLawrence.com -  Resources for Unix and Linux Systems, Bloggers and the self-employed

Kerio Connect Archive Script

© May 2019 Anthony Lawrence

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:

archive folder

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.

$SAFE="/root/kdir";  # put it wherever you like
@t=localtime(time()-(3600 * 24)); # go back one day

foreach (<*>) {
  next if /#public$/;
  # add anyone else you want to skip
  print "$_\n";
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;
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.

(OLDER)    <- More Stuff -> (NEWER)    (NEWEST)   

Printer Friendly Version

-> Kerio Connect Archive Script

Inexpensive and informative Apple related e-books:

Take Control of High Sierra

Take Control of IOS 11

Take Control of Parallels Desktop 12

Take Control of iCloud

Take Control of Apple Mail, Third Edition

More Articles by © Anthony Lawrence

Printer Friendly Version

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


Linux posts

Troubleshooting posts

This post tagged:

Kerio Connect


Kerio Info

Kerio Pricing

Kerio RSS Feed


Unix/Linux Consultants

Skills Tests

Unix/Linux Book Reviews

My Unix/Linux Troubleshooting Book

This site runs on Linode