#!/bin/bash
# Author: Pablo Yanez Trujillo <yanezp@informatik.uni-freiburg.de>
# creates a tar.gz/tar.bz2 backup and split into files with an equal size

# to backup
# backup-restore -b -s size [-t gz] -p prefix path1 path2 [path3 path4 ...]

# to restore
# backup-restore -r -p prefix path1 path2 

# for gentoo users

function ebegin()
{
        echo -e "$@" ...
}
        
function einfo()
{       
        echo -e "$@"
}
function ewarn()
{
        echo -e "$@"
}       
function eerror()
{       
        echo -e "$@"
}       
function eend()
{       
        local retval=${1:-0}
        shift
        return ${retval}
}

# inclduing Gentoo Layout functions
[[ -f /sbin/functions.sh ]] && . /sbin/functions.sh

function usage()
{
	[[ -n "$1" ]] && echo "${BAD}!!!${NORMAL} $1"
	echo "Usage: ${HILITE}`basename $0`${NORMAL} ${GOOD}type ${WARN}<options>${NORMAL} path1 path2"
	echo "where ${GOOD}type${NORMAL} is one of"
	echo "${GOOD}-b${NORMAL}		backup"
	echo "${GOOD}-r${NORMAL}		restore"
	echo
	echo "where ${WARN}<options>${NORMAL} is one of"
	echo "${WARN}-s size${NORMAL}		size in bytes of the splitted files to be saved as backup"
	echo "		Use the format of split(1)"
	echo "${WARN}[-t]${NORMAL}		type of backup. Use ${WARN}gz${NORMAL} or ${WARN}bz2${NORMAL}. Default ${WARN}gz${NORMAL}"
	echo "${WARN}-p prefix${NORMAL}	prefix of splitted file names"
	echo
	echo "path1: using ${GOOD}-b${NORMAL} is the absolute path of directory where the splitted files have to be saved"
	echo "path2: using ${GOOD}-b${NORMAL} absolute path of file/dir. to be backuped"
	echo
	echo "path1: using ${GOOD}-r${NORMAL} is the absolute path where the splitted file are saved"
	echo "path2: using ${GOOD}-r${NORMAL} is the absolute path where the backup has to be restored"
	echo
	echo "Examples:"
	echo "To backup the /home directory in /export/backup/home with max. file size 1GB"
	echo "${HILITE}`basename $0`${NORMAL} ${GOOD}-b ${WARN}-s 1000m -t bz2 -p home-\`date +%d.%m.%y\`${NORMAL} /export/backup/home /home"
	echo
	echo "To restore the backup of 03.06.05 of /export/backup/home/home-03.06.05* in /home"
	echo "${HILITE}`basename $0`${NORMAL} ${GOOD}-r ${WARN}-p home-03.06.05 -t bz2 ${NORMAL} /export/backup/home /home"
	echo
	echo "Comments and bugs to Pablo Yanez Trujillo <yanezp@informatik.uni-freiburg.de>"
}

TAR_TYPE="gz"

while getopts s:t:p:rb c; do
	case ${c} in
		b)
			TYPE="backup"
			;;
		r)
			TYPE="restore"
			;;
		s)
			SIZE="${OPTARG}"
			;;
		t)
			if [ "${OPTARG}" != "gz" ] && [ "${OPTARG}" != "bz2" ] ; then
				usage "Unkown compress type ${OPTARG}"
				exit 1
			fi

			TAR_TYPE="${OPTARG}"
			;;
		p)
			PREFIX="${OPTARG}"
			;;
		?)
			usage "option -${c} ${OPTARG} unknown"
			exit 1
			;;
		*)
			usage "unknown error"
			exit 1
			;;
	esac
done

if [ -z "${TYPE}" ] ; then
	usage "Use -b/-r options"
	exit 1
fi

if [ -z "${PREFIX}" ] ; then
	usage "Use -p option to set the prefix"
	exit 1
fi

if [ -z "${SIZE}" ] && [ "${TYPE}" == "backup" ] ; then
        usage "Use -s option to set the size"
        exit 1
fi

shift `expr ${OPTIND} - 1`

if [ -z "$1" ] || [ -z "$2" ] ; then
	usage "No paths given"
	exit 1
fi


if [ "${TYPE}" == "backup" ] ; then
	if [ "${TAR_TYPE}" == "gz" ] ; then
		TAR_COMMAND="tar cpzO"
		SPLIT_END=".tar.gz."
	else
		TAR_COMMAND="tar cpjO"
		SPLIT_END=".tar.bz2."
	fi

	SPLIT_PATH="$1/${PREFIX}${SPLIT_END}"

	cd $2

	${TAR_COMMAND} . | split -d -b ${SIZE} - ${SPLIT_PATH}
else
	if [ "${TAR_TYPE}" == "gz" ] ; then
                TAR_COMMAND="tar xpz"
                SPLIT_END=".tar.gz."
        else
                TAR_COMMAND="tar xpj"
                SPLIT_END=".tar.bz2."
        fi

	SPLIT_PATH="$1/${PREFIX}${SPLIT_END}"

	COUNT=`ls ${SPLIT_PATH}* 2>/dev/null | wc -w`

	if [ "$COUNT" == "0" ] ; then
		eerror "There are not backup files in $1"
		exit 1
	fi

	cd $2 || exit 1

	COUNT=`expr ${COUNT} - 1`

	(for i in `seq 0 ${COUNT}` ; do
		cat $SPLIT_PATH`printf "%0.2d\n" ${i}`
	done) | ${TAR_COMMAND}
fi
