#!/bin/sh
#
# Copyright (C) Matthieu Patou <mat@matws.net> 2010-2011
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# Revised 2013-09-25, Brian Martin, as follows:
#    - Allow retention period ("DAYS") to be specified as a parameter.
#    - Allow individual positional parameters to be left at the default
#      by specifying "-"
#    - Use IS0 8601 standard dates (yyyy-mm-dd instead of mmddyyyy).
#    - Display tar exit codes when reporting errors.
#    - Don't send error messages to /dev/null, so we know what failed.
#    - Suppress useless tar "socket ignored" message.
#    - Fix retention period bug when deleting old backups ($DAYS variable
#      could be set, but was ignored).



FROMWHERE=/usr/local/samba
WHERE=/usr/local/backups
DAYS=90				# Set default retention period.
if [ -n "$1" ] && [ "$1" = "-h" -o "$1" = "--usage" ]; then
	echo "samba_backup [provisiondir] [destinationdir] [retpd]"
	echo "Will backup your provision located in provisiondir to archive stored"
	echo "in destinationdir for retpd days. Use - to leave an option unchanged."
	echo "Default provisiondir: $FROMWHERE"
	echo "Default destinationdir: $WHERE"
	echo "Default destinationdir: $DAYS"
	exit 0
fi

[ -n "$1" -a "$1" != "-" ]&&FROMWHERE=$1	# Use parm or default if "-".  Validate later.
[ -n "$2" -a "$2" != "-" ]&&WHERE=$2		# Use parm or default if "-".  Validate later.
[ -n "$3" -a "$3" -eq "$3" 2> /dev/null ]&&DAYS=$3	# Use parm or default if non-numeric (incl "-").

DIRS="private etc sysvol"
#Number of days to keep the backup
WHEN=`date +%Y-%m-%d`	# ISO 8601 standard date.

if [ ! -d $WHERE ]; then
	echo "Missing backup directory $WHERE"
	exit 1
fi

if [ ! -d $FROMWHERE ]; then
	echo "Missing or wrong provision directory $FROMWHERE"
	exit 1
fi

cd $FROMWHERE
for d in $DIRS;do
	relativedirname=`find . -type d -name "$d" -prune`
	n=`echo $d | sed 's/\//_/g'`
	if [ "$d" = "private" ]; then
		find $relativedirname -name "*.ldb.bak" -exec rm {} \;
		for ldb in `find $relativedirname -name "*.ldb"`; do
			tdbbackup $ldb
			Status=$?	# Preserve $? for message, since [ alters it.
			if [ $Status -ne 0 ]; then
				echo "Error while backing up $ldb - status $Status"
				exit 1
			fi
		done
		# Run the backup.
		#    --warning=no-file-ignored set to suppress "socket ignored" messages.
		tar cjf ${WHERE}/samba4_${n}.${WHEN}.tar.bz2  $relativedirname --exclude=\*.ldb --warning=no-file-ignored --transform 's/.ldb.bak$/.ldb/'
		Status=$?	# Preserve $? for message, since [ alters it.
		if [ $Status -ne 0 -a $Status -ne 1 ]; then	# Ignore 1 - private dir is always changing.
			echo "Error while archiving ${WHERE}/samba4_${n}.${WHEN}.tar.bz2 - status = $Status"
			exit 1
		fi
		find $relativedirname -name "*.ldb.bak" -exec rm {} \;
	else
		# Run the backup.
		#    --warning=no-file-ignored set to suppress "socket ignored" messages.
		tar cjf ${WHERE}/${n}.${WHEN}.tar.bz2  $relativedirname --warning=no-file-ignored
		Status=$?	# Preserve $? for message, since [ alters it.
		if [ $Status -ne 0 ]; then
			echo "Error while archiving ${WHERE}/${n}.${WHEN}.tar.bz2 - status = $Status"
			exit 1
		fi
	fi
done

find $WHERE -name "samba4_*bz2" -mtime +$DAYS -exec rm  {} \;
