#!/bin/bash

#
# Script to back up the postfix and cyrus
# spool files on OSXS 10.3
#

#
# Original script - William Bowden 1/04
# Minor updates - Joel Rennich 3/04
# Fixed version number and postfix yelling when it was already started - Joel Rennich 3/04
#

#
# These are the default storage areas for Postfix and Cyrus
# If you have changed this from the stock installation
# please edit this accordingly
#

POST_SPOOL=/var/spool/postfix
CYRUS_COMMAND=/var/imap
CYRUS_STORE=/var/spool/imap
CYRUS_STORE_RESTORE=/var/spool/

POSTFIX_CONFIG=NO

#
# Version Info
#

VERSION_NUM="1.1"
VERSION=NO

#
# check for root privs
#

check_root() {
    if [ `whoami` != "root" ]
    then
        echo "you need to be root to do this"
        exit 0;
    fi
}

#
# this starts the mail system
#

start_server() {
    echo "Starting the mail system"
    serveradmin start mail
    
    # sometimes serveradmin is dumb and doesn't start postfix
    # if it's already running this should just error nicely
    # although we send it to /dev/null anyway
    postfix start 2> /dev/null
    echo done
}

#
# this stops the mail system
#

stop_server() {
    echo "Stopping the mail system"
    serveradmin stop mail
    echo done
 }
  
#
# this checks for options when the script is called
#

while getopts b:r:cpv SWITCH
do
        case $SWITCH in
                b) BACKUP_DIR=$OPTARG;;
                r) RESTORE_DIR=$OPTARG;;
                c) CYRUS_STORE=$OPTARG;;
                p) POSTFIX_CONFIG=YES;;
                v) VERSION=YES;;
                *)  echo "mailback script for 10.3 server"
                    echo useage: $PROGRAM [ -b backup_directory] [ -r restore directory] [-c cyrus mail spool] [-p]
                    echo "-b specifies the backup directory"
                    echo "-r will restore the files from that directory"
                    echo "use -c to specify a non-standard cyrus mail store"
                    echo "use -p to back up /etc/postfix also"
                    echo "use -v to just check the version of the script"
                    echo ""
                    echo examples:
                    echo "Backup up to a directory: mailback -b /Volumes/Xraid/mailbackup"
                    echo "Restore from a directory: mailback -r /Volumes/Xraid/mailbackup"
                    exit 1;;
        esac
done

#
# This prints the version and then bails
#

if [ $VERSION = "YES" ]
then
    echo "mailback version $VERSION_NUM"
    exit 1
fi

#
# this backs up the spools
#

if [ ! -z "$BACKUP_DIR" ]
then
        check_root
        if [ ! -d $BACKUP_DIR ]
        logger "backing up the mail system to $BACKUP_DIR"
        then
                mkdir -p "$BACKUP_DIR"
        fi
        
        stop_server
        
        echo "backing up the Postfix spool"
        ditto -rsrc  $POST_SPOOL  $BACKUP_DIR"/POSTFIX_SPOOL"
        
        echo "backing up the Cyrus delivery database"
        ditto -rsrc  $CYRUS_COMMAND $BACKUP_DIR"/CYRUS_COMMAND"
        
        echo "backing up the Cyrus mail store"
        #ditto -rsrc  $CYRUS_STORE  $BACKUP_DIR"/CYRUS_SPOOL"
        rsync -a $CYRUS_STORE $BACKUP_DIR
        
        if [ $POSTFIX_CONFIG = "YES" ]
        then
            echo "backing up the Postifx config files"
            rsync -a /etc/postfix $BACKUP_DIR
        fi
        
        start_server
        
        logger "mail system backed up"
fi

#
# this restores the spools
#

if [ ! -z "$RESTORE_DIR" ]
then
        check_root
        logger "restoring the mail system from $RESTORE_DIR"
        stop_server
        
        echo "restoring the Postfix spool"
        ditto -rsrc  $RESTORE_DIR"/POSTFIX_SPOOL"  $POST_SPOOL
        
        echo "restoring the Cyrus delivery database"
        ditto -rsrc  $RESTORE_DIR"/CYRUS_COMMAND" $CYRUS_COMMAND
        
        echo "restoring the Cyrus mail store"
        #ditto -rsrc  $RESTORE_DIR"/CYRUS_SPOOL"  $CYRUS_STORE
        rsync -a  $RESTORE_DIR"/imap"  $CYRUS_STORE_RESTORE

        if [ $POSTFIX_CONFIG = "YES" ]
        then
            echo "restoring the Postifx config files"
            rsync -a $RESTORE_DIR"/postfix" /etc
        fi
        
        start_server
        logger "mail system restored"
fi