#!/bin/sh
#
# Copyright 2003-2024 Luke Mewburn <luke@mewburn.net>
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
# 3. The name of the author may not be used to endorse or promote products
#    derived from this software without specific prior written permission.
# 
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
# TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
# USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#
# do-rdiff-backup --
#	Backup various file systems with rdiff-backup.
#	supports removing old increments, and "nodump" lists.
#
#	http://www.mewburn.net/luke/src/do-rdiff-backup
#

# TODO:
#   *	automagically build nodump ?
#   *	support options like "test" and "list incrementals"
#   *	support cmd-line overrides of env vars?
#   *	add per-fs extra-args ?
#

export PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/pkg/bin:/usr/pkg/sbin

# read values of:
#	RD_BACKUPDIR
#	RD_FILESYSTEMS
#	RD_KEEPINCREMENTS
#

. /etc/do-rdiff-backup.conf
if [ -n "$*" ]; then
	RD_FILESYSTEMS=$*
fi

validate_params()
{
	if [ -z ${RD_BACKUPDIR} ]; then
		echo 1>&2 "$0: RD_BACKUPDIR is not set"
		exit 1
	fi
	if ! cd ${RD_BACKUPDIR}; then
		echo 1>&2 "$0: can't cd to ${RD_BACKUPDIR}"
		exit 1
	fi

	if [ -z "${RD_FILESYSTEMS}" ]; then
		echo 1>&2 "$0: RD_FILESYSTEMS is not set"
		exit 1
	fi

	set -- ${RD_FILESYSTEMS}
	while [ $# -gt 1 ]; do
		src=$1
		dest=$2
		shift 2
		if [ \! -d ${dest} ]; then
			echo 1>&2 "$0: ${dest} is not a directory"
			exit 1
		fi
	done
}

backup_filesystems()
{
	set -- ${RD_FILESYSTEMS}
	while [ $# -gt 1 ]; do
		src=$1
		dest=$2
		shift 2
		excl=${dest}.nodump
		if [ \! -f ${excl} ]; then
			excl=
		fi

		echo ""
		echo "==> Source: ${src}  Destination: ${dest}${excl:+	Exclude list: }${excl}"

		# clean old increments
		#
		if [ -n "${RD_KEEPINCREMENTS}" ]; then
			echo "==> Cleaning old increments from ${dest}"
			rdiff-backup --remove-older-than ${RD_KEEPINCREMENTS} ${dest}
		fi

		# back up file system
		#
		echo "==> Backing up ${src} to ${dest}"
		rdiff-backup \
			--exclude-device-files \
			--exclude-other-filesystems \
			--print-statistics \
			${excl:+--exclude-filelist} ${excl} \
			${src} ${dest}

#XXX		# statistics
#XXX		#
#XXX		echo "==> Average statistics for ${dest}"
#XXX		rdiff-backup --calculate-average ${dest}/rdiff-backup-data/session_statistics.*

	done
}

#
#	main program
#

echo -n "==> Started at: "; date

ulimit -S -d 524288
ulimit -S -n 512

validate_params

echo "==> Disk usage before:"
df ${RD_BACKUPDIR} | tail +2
backup_filesystems

echo ""
echo "==> Disk usage after:"
df ${RD_BACKUPDIR} | tail +2

echo -n "==> Finished at: "; date
