#!/bin/bash --
######################################################################
# drfullback (mondoclean) Ver. 0.9.4
# - Take a Disaster Recovery Full Backup using Mondo Rescue.
#
# * If you call me "mondoclean" through symbolic link at run time,
# It just clean up mondo and mindi's working directories and exit soon
# - necessary directories will be created if they don't exist (except
# $MONDO_OUT on NFS).
#
# 0.9.4<-0.9.3a:
# * Came to use temporary command file to fix `-E' argument building
#   problem.
# 0.9.3a<-0.9.2:
# * Get rid of backing-up mounted NFS share itself when image
#   destination is NFS even if `-N' option was not specified.
# 0.9.2<-0.9.1:
# * Support saving of backup images to NFS share.
# 0.9.1<-0.9.0:
# * Added variables to define exclude path and backup image size.
######################################################################

ME=$(basename $0)

##############################
### Setting variables

# Kernel which you want to use for Disaster Recovery Boot image.
# Leave it blank if you want mindi to pick up currently running kernel.
#BOOTKERNEL=/boot/vmlinuz-initrd-2.6.18-194.el5

# Directory or device into which mondoarchive backup images go.
# [Default: /var/cache/mondo]
MONDO_OUT=/home/mondo/iso
#
# If you want to backup out to NFS share, specify a relative path
# from where NFS share is mounted.
#MONDO_OUT=/

# NFS URI. If this is defined, the script executes mondoarchive with
# `-n $NFS_URI' option. So, comment this out if it is not what you want.
# [Default: Not defined]
#NFS_URI=your_nfs_server:/path/to/nfs_share

# Directory into which mondorescue.iso goes.
# [Default: /var/cache/mindi]
#MINDI_IMGDIR=/var/cache/mindi

# Directory where ISO images are built by mondoarchive before archiving.
# [Default: /var/mondo/scratch]
#MONDO_SCRATCHDIR=/var/mondo.scratch

# Directory which mondoarchive uses for temporary files other than ISOs.
# [Default: /var/mondo/tmp]
#MONDO_TMPDIR=/home/mondo/mondo.tmp

# Temporary script file to create and run.
# [Default: /var/tmp/${ME}-temp.sh]
#TEMPSCRIPT=/tmp/${ME}-temp.sh

##############################
## Optional definitions

# Exclude paths. If multiple, separate with space and enclose with `"'.
# NB: Mondo automatically excludes mindi/mondo working directories and
# local $MONDO_OUT. Plus, this script tries to exclude NFS mountpoint
# in which $MONDO_OUT resides.
#EXCLUDE_FROM_BACKUP="/home/mondo /var/cache/mindi"

# Maximum size of backup image/media.
IMGSIZE_MAX=4480m

##############################
### Functions

add_nfs_exc () {
    local mpoint

    if [ -n "${NFS_URI}" ]; then
	mpoint=$(grep -w ${NFS_URI} /etc/mtab |cut -d" " -f 2)

	# Failsafe in case NFS is not mounted.
	[ -z "$mpoint" ] && return

	if [ -n "$EXCLUDE_FROM_BACKUP" ]; then
	    if echo ${EXCLUDE_FROM_BACKUP[@]} |grep -qwv $mpoint; then
		EXCLUDE_FROM_BACKUP="${EXCLUDE_FROM_BACKUP} ${mpoint}"
	    fi
	else
	    EXCLUDE_FROM_BACKUP=${mpoint}
	fi
    fi
}

mkexcludedef () {
    local cnt

    add_nfs_exc

    # Build -E option string
    [ -z "$EXCLUDE_FROM_BACKUP" ] && return

    cnt=$(echo ${EXCLUDE_FROM_BACKUP[@]} |/usr/bin/wc -w)

    if [ $cnt -gt 1 ]; then
	EXCLUDEDEF="-E \"${EXCLUDE_FROM_BACKUP}\""
    else
	EXCLUDEDEF="-E ${EXCLUDE_FROM_BACKUP}"
    fi
}

##############################
### Main

## Executor check.
if [ $(id -u) -ne 0 ]; then
    echo "error: only root can run $ME"
    exit 1
fi

## Check variables and set defaults.
MONDO_OUT=${MONDO_OUT:-/var/cache/mondo}
MINDI_IMGDIR=${MINDI_IMGDIR:-/var/cache/mindi}
MONDO_SCRATCHDIR=${MONDO_SCRATCHDIR:-/var/mondo/scratch}
MONDO_TMPDIR=${MONDO_TMPDIR:-/var/mondo/tmp}
TEMPSCRIPT=${TEMPSCRIPT:-/var/tmp/${ME}-temp.sh}

if [ -z "${MONDO_OUT%/}" -a -z "${NFS_URI}" ]; then
    echo "variable MONDO_OUT can not be null nor /. Aborting.."
    exit 1
fi

if [ -z "${MONDO_SCRATCHDIR%/}" ]; then
    echo "variable MONDO_SCRATCHDIR can not be null nor /. Aborting.."
    exit 1
fi

if [ -z "${MONDO_TMPDIR%/}" ]; then
    echo "variable MONDO_TMPDIR can not be null nor /. Aborting.."
    exit 1
fi

if [ -z "${MINDI_IMGDIR%/}" ]; then
    echo "variable MINDI_IMGDIR can not be null nor /. Aborting.."
    exit 1
fi

## Clean everything up before we start.

# We won't clean up existent ISOs if $MONDO_OUT is on NFS.
if [ -z "${NFS_URI}" ]; then
    if [ -d $MONDO_OUT ]; then
	rm -f ${MONDO_OUT}/*.iso
    else
	mkdir -p $MONDO_OUT
    fi
fi

if [ -d $MONDO_SCRATCHDIR ]; then
    if [ -n "${MONDO_SCRATCHDIR%/}" ]; then
	rm -rf ${MONDO_SCRATCHDIR}/*
    fi
else
    mkdir -p ${MONDO_SCRATCHDIR}
fi

if [ -d $MONDO_TMPDIR ]; then
    if [ -n "${MONDO_TMPDIR%/}" ]; then
	rm -rf ${MONDO_TMPDIR}/*
    fi
else
    mkdir -p $MONDO_TMPDIR
fi

if [ -d $MINDI_IMGDIR ]; then
    if [ -n "${MINDI_IMGDIR%/}" ]; then
	rm -rf ${MINDI_IMGDIR}/*
    fi
fi

## Quit here if executor called me "mondoclean".
[ $ME = mondoclean ] && exit 0

## Heart of the script from here.
[ -n "$BOOTKERNEL" ] && KOPT="-k $BOOTKERNEL"
[ -n "$IMGSIZE_MAX" ] && IMGSIZEDEF="-s $IMGSIZE_MAX"
mkexcludedef

if [ -n "${NFS_URI}" ]; then
    echo exec mondoarchive -O -n $NFS_URI $IMGSIZEDEF -g -d $MONDO_OUT -9 $KOPT -S $MONDO_SCRATCHDIR -T $MONDO_TMPDIR $EXCLUDEDEF >$TEMPSCRIPT
else
    echo exec mondoarchive -O -i $IMGSIZEDEF -g -d $MONDO_OUT -9 $KOPT -S $MONDO_SCRATCHDIR -T $MONDO_TMPDIR $EXCLUDEDEF >$TEMPSCRIPT
fi

sh $TEMPSCRIPT
rm -f $TEMPSCRIPT
