Backing up the Apple Mail Server (AMS)
5 May 2003
While the mail server that Apple includes in OS X Server will never be seen running large enterprise mail systems it does suit the needs of a lot of smaller organizations.
AMS can be mostly managed from the GUI—with the exception of backing up the mail database. If you are actually in front of the machine, this isn’t that big of a deal. Just stop the mail server and copy /Library/AppleMailServer somewhere else. Then start the server back up.
However, if you are trying to script this things get harder. First off you need to stop the mail server or else you risk corrupting your backed up database. To do this you can script a “kill” command. However the mail server process is being watched by the watchdog process which will restart the mail server if it stops. This becomes a problem. To get around this you need to edit /etc/watchdog.conf so that watchdog doesn’t know to look after the mail server.
To do all of this for you we’ve hacked together a little script. The big parts were written by the good folks at Feratech.
Here’s how you use it:
mailbak -b /Volumes/XRAID/MailBackUP
will stop AMS, being careful to take care of watchdog, copy the mail database to /Volumes/XRAID/MailBackUP, start AMS back up, then finish off by reseting the watchdog config file.
You can also do the reverse:
mailbak -b /Volumes/XRAID/MailBackUp
Now AMS is stopped, the contents of /Volumes/XRAID/MailBackUP is copied into /Library/AppleMailServer.
Copy the script below. Paste it into a file on your machine and make it executable. Now you can call the script from cron to run when you need it to.
#!/bin/sh
# mailback
#
# Script to shut down AMS and then backup or restore a mail db and then
# finish off by restarting AMS
#
# most of the work was done by the good people at Feratech
# www.feratech.com
#
# transformation from starting and stopping mail to doing a backup
# Joel Rennich - 4/23/03
# Configuration
service=mailservice
pidfile=/private/var/run/watchdog.pid
tmpfile=/tmp/mailctl.tmp
watchdog=/etc/watchdog.conf
# Define functions
start() {
echo Starting $service
cat $watchdog | sed "s/${service}:off:/${service}:respawn:/" > ${tmpfile}
cat ${tmpfile} > $watchdog
kill -HUP $pid
echo done
}
stop() {
echo Stopping $service
cat $watchdog | sed "s/${service}:respawn:/${service}:off:/" > ${tmpfile}
cat ${tmpfile} > $watchdog
kill -HUP $pid
echo done
}
usage() {
echo "usage: mailback [-b] location to save the backed up database"
echo " [-r] location of backed up database to restore"
}
# Read watchdog process id
pid=`cat $pidfile`
# Grab the options from the command line
while getopts b:r: SWITCH
do
case $SWITCH in
b) BACKUP_DIR=$OPTARG;;
r) RESTORE_DIR=$OPTARG;;
*) USAGE=YES;;
esac
done
if [ ! -z "$BACKUP_DIR" ]
then
if [ ! -d $BACKUP_DIR ]
then
mkdir -p "$BACKUP_DIR"
fi
stop
kill -HUP $pid
rsync -a /Library/AppleMailServer $BACKUP_DIR
start
fi
if [ ! -z "$RESTORE_DIR" ]
then
stop
kill -HUP $pid
rsync -a ${RESTORE_DIR}/AppleMailServer /Library
start
fi
if [ ! -z "$USAGE" ]
then
usage
fi