#!/bin/bash

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

#
# 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
#
# Added paths to Junk and AV config files along with /var/amavis and /var/clamav - Ian Kelly 8/05
#
# 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/
# AMaViS/ClamAV/SpamAssassin additions below:
CLAM_CONF=/etc/clamav.conf
FRESHCLAM_CONF=/etc/freshclam.conf
AMAVISD_CONF=/etc/amavisd.conf
SPAMASSASSIN_CONF=/etc/mail/spamassassin/
VAR_AMAVIS=/var/amavis/
VAR_CLAMAV=/var/clamav/
CLAM_RESTORE=/etc
FRESHCLAM_RESTORE=/etc
AMAVISD_RESTORE=/etc
SPAMASSASSIN_RESTORE=/etc/mail/spamassassin



POSTFIX_CONFIG=NO

#
# Version Info
#

VERSION_NUM="1.2"
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.4 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
 
# AMaViS/ClamAV/SpamAssassin additions below:        
        echo "backing up SpamAssassin and ClamAV config files"
        ditto -rsrc  $CLAM_CONF "$BACKUP_DIR""/CLAM/"        
        ditto -rsrc  $FRESHCLAM_CONF "$BACKUP_DIR""/FRESHCLAM/"        
        ditto -rsrc  $AMAVISD_CONF "$BACKUP_DIR""/AMAVISD/"        
        ditto -rsrc  $SPAMASSASSIN_CONF "$BACKUP_DIR""/SPAMASSASSIN/"
    	ditto -rsrc  $VAR_AMAVIS "$BACKUP_DIR""/VAR_AMAVIS"   
        ditto -rsrc  $VAR_CLAMAV "$BACKUP_DIR""/VAR_CLAMAV"
        
        start_server
        	
        	echo \\"mailbox Sizes:\\"
			du -h -d 2 /var/spool/imap/ 
    
    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

#
#	this will restore the junk and clamav configuration files back to their default locations
#
        echo "restoring SpamAssassin and ClamAV config files"
        ditto -rsrc   "$RESTORE_DIR""/CLAM/clamav.conf" $CLAM_RESTORE
        ditto -rsrc   "$RESTORE_DIR""/FRESHCLAM/freshclam.conf" $FRESHCLAM_RESTORE
        ditto -rsrc   "$RESTORE_DIR""/AMAVISD/amavisd.conf" $AMAVISD_RESTORE
        ditto -rsrc   "$RESTORE_DIR""/SPAMASSASSIN/" $SPAMASSASSIN_RESTORE
    	ditto -rsrc   "$RESTORE_DIR""/VAR_AMAVIS/" $VAR_AMAVIS
        ditto -rsrc   "$RESTORE_DIR""/VAR_CLAMAV/" $VAR_CLAMAV
        
        start_server
        logger "mail system restored"
fi