#!/bin/sh

#
# A script to back up the 3 major components of
# Open Directory on an OS X Server 10.3 machine
#

#
# Original script - Joel Rennich 4/4/04
#

#
# Some variables
#

LDAP_BACK=NO
KRB_BACK=NO
LDAP_BACK=NO
ALL_BACK=NO
PWS_BACK=NO

#
# Version info
#

VERSION_NUM=".9"
VERSION=NO

#
# check for root privs
#

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

backup_ldap() {

# This shuts down your LDAP server
/System/Library/StartupItems/LDAP/LDAP stop

# This dumps the entire contents of your LDAP server to an LDIF file 
# that can be easily backed up

/usr/sbin/slapcat > $BACKUP_DIR/ldap.dump

# This starts the LDAP server back up again
/System/Library/StartupItems/LDAP/LDAP start

}

backup_pws() {

# This tars up the relevant files
/usr/bin/gnutar -czv /var/db/authserver > $BACKUP_DIR/authserver.tgz

}

backup_krb() {

# And now for the Kerberos KDC
/usr/sbin/kdb5_util dump $BACKUP_DIR/kdb.dump

}

while getopts b:l:k:p:av SWITCH
do
        case $SWITCH in
            b) BACKUP_DIR=$OPTARG;;
            l) LDAP_BACK=YES;;
            k) KRB_BACK=YES;;
            p) PWS_BACK=YES;;
            a) ALL_BACK=YES;;
            v) VERSION=YES;;
            *)  echo "Open Directory backup for 10.3 server"
                echo useage: $PROGRAM [ -b backup_directory ] [ -r ] [-alkp]
                
            exit 1;;
        esac
done

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

if [ $ALL_BACK = "YES" ]
then
    check_root
    backup_ldap
    backup_pws
    backup_krb
fi

if [ $LDAP_BACK = "YES" ]
then
    check_root
    backup_ldap
fi

if [ $PWS_BACK = "YES" ]
then
    check_root
    backup_pws
fi

if [ $KRB_BACK = "YES" ]
then
    check_root
    backup_krb
fi
