How to Search Through Kerio Connect Backup Archives

Kerio Connect is a commercial email/groupware server and MS Exchange alternative that runs on Linux, Mac OSX and Windows. It is a great solution for organizations that require full featured email hosted in house without the licensing costs of MS Exchange.

The built in email backup schedule in Kerio Connect can do full and incremental backups of the server’s entire mail store on a custom schedule. By default, the mailstore backups are stored in a series of 2GB dated zip files. On servers that have large mailstores, this can potentially result hundreds of zips generated by a full backup.

Kerio Connect has a built in kmsrecover utility for restoring an entire mailstore, mailbox, or subfolder from either A: the most recent backup or B: a specific zip file. How does one know which specific backup file though? What if a user’s inbox or subfolder spans multiple zip files? And what if we just want an overview of which of the many archives contains mail for the user we are looking for, without restoring all of it?

I wrote an interactive bash script that does just this, and serves as a companion to kmsrecover by identifying the zips that contain mail from a specific Kerio Connect email address. Once identified, the sysadmin can use kmsrecover, or standard Linux commands (unzip, grep, etc) to explore and extract mail from the zip archive.

This script is tested on Ubuntu Server 14 with Kerio Connect 8.5.2, and can be adapted for Mac OSX or other Linux distributions. The script does require the unzip package, and will prompt you for installation if it is not found.

If you are looking for technical support, maintenance, or licensing for your Kerio Connect mail server, please contact us to discuss your needs.

 

#!/bin/bash

####### GLOBAL VARIABLES ######
BACKUPDIR=$(cat /opt/kerio/mailserver/mailserver.cfg | grep \”BackupDir | awk -F ‘>’ ‘{print $2}’ | awk -F ‘<‘ ‘{print $1}’)

###### FUNCTIONS #######

intro () {

############ INTRO AND CHECKING FOR UNZIP PACKAGE######
echo
echo Welcome to the Kerio Backup Search Tool!
echo
echo This script requires the Unzip package! I am checking if it is installed already.
sleep 1
if ! which unzip > /dev/null; then
echo -e “Unzip is not installed. Would you like to install it? (y/n) \c”
read -e REPLY

if [ “$REPLY” = “y” ] ; then
apt-get -y install unzip
echo unzip has been installed
else
echo OK then, best of luck you you!
exit
fi
else
echo Great, I see that unzip is already installed. Lets move on.
sleep 1
fi
}

get_input () {
############ GET EMAIL ADDRESS FROM USER INPUT #########
echo
echo Enter the full email address that we are looking for today followed by [ENTER]
echo
read -e EMAILADDRESS

if [[ $EMAILADDRESS != *?”@”?*”.”?* ]] ; then
echo You have entered an invalid e-mail address!
get_input
else echo “Thanks! I’m searching Kerio backup zip files for “$EMAILADDRESS”. This could take some time! Maybe it is time for a snack?”
echo
fi

EMAILDOMAIN=$(echo $EMAILADDRESS | awk -F ‘@’ ‘{print $2}’)
EMAILUSER=$(echo $EMAILADDRESS | awk -F ‘@’ ‘{print $1}’)

}

preflight () {

########### CLEANUP PREVIOUS TEMP FILE IF IT EXISTS########
if [ -f /tmp/keriobackupsearchresults.txt ] ; then
rm /tmp/keriobackupsearchresults.txt
fi
echo “Thanks! I’m searching Kerio backup zip files for “$EMAILADDRESS”. This could take some time! Maybe it is time for a snack?”
echo
}

search () {
########### PROGRESS BAR ##########
while true; do echo -n .; sleep 1; done &
trap ‘kill $!; rm /tmp/keriobackupsearchresults.txt’ SIGTERM SIGKILL

########## SEARCHING FOR MATCHES ########

for file in “$BACKUPDIR”/*.zip ; do
if ( unzip -l “$file” 2>/dev/null | grep -q “$EMAILDOMAIN/$EMAILUSER” ); then
echo “$file” >> /tmp/keriobackupsearchresults.txt
fi
done;
echo done
kill $!
}

list_output () {
########## LIST OUTPUT ##########
if ! [ -f /tmp/keriobackupsearchresults.txt ] ; then

echo “Sorry, this email account does not exist in your backups. Someone is going to be very angry :(”
exit
else
echo “Congratulations! The following kerio backup zip files contain email for “$EMAILADDRESS”. Check the man pages for unzip to find out how to search and extract from these archives.”
echo
sleep 1
cat /tmp/keriobackupsearchresults.txt
rm /tmp/keriobackupsearchresults.txt
fi
echo

echo That is all, Thanks for playing.
}
########

preflight
intro
get_input
search
list_output
exit

mac-tech anonymous profile

“The built in email backup schedule in Kerio Connect can do full and incremental backups of the server’s entire mail store on a custom schedule. By default, the mailstore backups are stored in a series of 2GB dated zip files. On servers that have large mailstores, this can potentially result hundreds of zips generated by a full backup.”