#!/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).

. /etc/samba/samba_backup.conf
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 destinationdir: $WHERE"
	echo "Default destinationdir: $DAYS"
	exit 0
fi

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

WHEN=`date ${WHEN_FORMAT}`

for d in $FROMWHERE;do
	n=`echo $d | sed 's/\//_/g'`
	if [ "`basename $d`" = "private" ]; then
		find $d -name "*.ldb.bak" -exec rm {} \;
		for ldb in `find $d -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  $d --xattrs --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 $d -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  $d --xattrs --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  {} \;
