Zimbra 8.7 Compact indexes

Zimbra 8.7 Compact indexes

I forget where we picked up this script, but it comes in handy to reduce the usage of the index volumes.  Worked great up to Zimbra 8.6.

With Zimbra 8.7 some adjustments needed to be made sine the directory structure was moved around a bit as reflected in lines 7 & 8 of the code.

Here is the code adjusted for Zimbra 8.7

#!/bin/bash
# When the num of deleted docs are > 50,000, index compaction is recommended
# https://bugzilla.zimbra.com/show_bug.cgi?id=76414
# stsimb Sep 2015
#
export PATH="/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/sbin:/usr/sbin"
export PERL5LIB=/opt/zimbra/common/lib/perl5/x86_64-linux-gnu-thread-multi:/opt/zimbra/common/lib/perl5
export PERLLIB=/opt/zimbra/common/lib/perl5/x86_64-linux-gnu-thread-multi:/opt/zimbra/common/lib/perl5
SCRIPT_NAME=`basename $0`
LOCKFILE="/tmp/${SCRIPT_NAME}.lock"
[ -f ${LOCKFILE} ] && logger "$0 already running......" && echo "Already running..." && exit 1
date > "${LOCKFILE}"

### REAL START SCRIPT #########################################################

THRESHOLD=1000

input=$(mktemp)
zmprov="/opt/zimbra/bin/zmprov"
zmaccts="/opt/zimbra/bin/zmaccts"

# get all active accounts
$zmaccts | awk '/@.*active/ {print $1}' | sort -u > ${input}

# process all accounts
for acct in $(cat ${input}); do
echo -n "$(date) ${acct}"

# getIndexStats
stats="`$zmprov getIndexStats $acct`"
echo -n " ${stats//:/ }"

# compare with threshold
numDeletedDocs=${stats##*:}
if [ ${numDeletedDocs} -gt ${THRESHOLD} ]; then
# start compact job
echo -n " compact index "
$zmprov compactIndexMailbox $acct start
else
# skip this account
echo " skip index compaction."
fi
done

rm -f "${input}"
### REAL END SCRIPT ###########################################################
### Please do not write below this line.

/bin/rm -f "${LOCKFILE}"
exit $?