Tuesday, June 7, 2016

OS X Calendar Server Maintenance and Cleanup

Having some troubles with my calendar server, I decided, it was time for a cleanup.

We are running the Calendar Server on OS X 10.11.x El Capitan.
Server App version 5.1.5.

First thing to do was to reindex the database.

This is my script that does this. I reads a list of the databases and then issues a reindex command to each table:

#!/bin/sh

# Reindex all calendar server databases
# © 2016 kurt hofmann

# Get stat of the calendar server

calStat=`serveradmin status calendar | grep "calendar:calendarState"`

echo "$calStat" | grep "RUNNING" >/dev/null 2>&1
  if [ "$?" -eq "0" ]; then
      echo "Calendar is running"
      CalRunning=true
  else
      echo "Calendar is stopped"
      CalRunning=false
  fi

if $CalRunning ; then
  echo "Stopping calendar server"
  serveradmin stop calendar
fi


# Do the database stuff

export PGDATABASE=caldav
export PGUSER=caldav

# Get list of tables

dbt=`psql -h /var/run/caldavd/PostgresSocket/ << EOF
\d ;
EOF`


dblist=`echo "$dbt" | cut -d "|" -f 2 | grep "^ " | grep -v "^  "`

# With each database in the list, issue a reindex command

for db in $dblist
do

echo "Now reindexing table $db"

dbt=`psql -h /var/run/caldavd/PostgresSocket/ << EOF
REINDEX table $db ;
EOF`

done

# Restart Calendar server if it was running before we started

if $CalRunning ; then
  echo "Starting Calendar"
  serveradmin start calendar
fi

exit 0


Second thing to do was to purge all old calendar entries.

I already had a script that did this, but this no longer worked because this can no longer be done for all users in one go.
The command:

/Applications/Server.app/Contents/ServerRoot/usr/sbin/calendarserver_purge_events
now requires a userid.

So, I first need to read the contents of the databases to find the uids of all the users that actually use the calendar. Then I delete the entries older than 365 days on the calendar.

Then we do the same thing with the attachments.

The script goes like this:

#!/bin/sh

# Using hints from myself ;-) :
#http://osxadmin.blogspot.ch/2015/06/os-x-yosemite-calendar-server-utilites.html

# These are the commands involved:
#/Applications/Server.app/Contents/ServerRoot/usr/sbin/calendarserver_purge_events -nv

#/Applications/Server.app/Contents/ServerRoot/usr/sbin/calendarserver_purge_attachments -nv

#psql -h /var/run/caldavd/PostgresSocket/ --username=caldav

export PGDATABASE=caldav
export PGUSER=caldav

# Get the UIDs of all users

dbt=`psql -h /var/run/caldavd/PostgresSocket/ << EOF
SELECT * FROM calendar_home;
EOF`

theUsers=`echo "$dbt" | cut -d "|" -f 2 | grep "^ " | grep -v "^  "`

# Purge all old data

for uid in $theUsers
do
  echo $uid
  theResult=`/Applications/Server.app/Contents/ServerRoot/usr/sbin/calendarserver_purge_events -u $uid`
  purge_events=`echo "$theResult" | grep "older"`
  echo "$purge_events"
  /Applications/Server.app/Contents/ServerRoot/usr/sbin/calendarserver_purge_attachments -u $uid
  echo "----"
done


No comments: