#!/bin/bash
#                       /usr/local/bin/rsnapshot-postexec
# https://crystalfaeries.net/posix/bin/rsnapshot-postexec
# celeste:crystalfaery RSNAPSHOT-POSTEXEC 2019-08-13 13:50:19+00:00
# invoked by                      /etc/rsnapshot.conf

# The only optional argument is "-du", for a Disk Useage analysis

# we handle either hourly or daily as maximum frequency of backups
myhost="$(echo `hostname` | sed 's/\..*//')"	# extract hostname
	 file_system="/home/rsnapshot"	# default
cd	"${file_system}"	# default
	 file_system="$( grep snapshot_root /etc/rsnapshot.conf | head -n 1 | cut -f 2 | sed 's/\/$//' )"
cd	"${file_system}" || ls -Flad "${file_system}"	 1>&2
cd	"${file_system}"/daily.1/${myhost}  > /dev/null 2>&1
cd	"${file_system}"/daily.0/${myhost}  > /dev/null 2>&1
cd	"${file_system}"/hourly.1/${myhost} > /dev/null 2>&1 
cd	"${file_system}"/hourly.0/${myhost} > /dev/null 2>&1 
if	[ "${file_system}" = "`pwd`" ]
	then
		echo "${0} found no backups in ${file_system}"
		exit 0 # we could not find any backups inside root file system?
	fi

#	CALCULATE DISK SPACE USED	#
while	[[ "$#" -gt 0 ]]
do
    if	[ "${1}" == "-du" ]
    then
	rsnapshot du  2> /dev/null > /var/log/${myhost}.rsnapshot.du.txt
	du -a    -x 2> /dev/null | sed 's/	\./	/' | sort -k2 >> /var/log/${myhost}.rsnapshot.du.txt
	du -s    -x $( grep '^exclude' /etc/rsnapshot.conf | cut -f 2 ) 2> /dev/null | sed 's/	\./	/' | sort -k2 > /var/log/${myhost}.rsnapshot.excluded.txt
    else
	echo "${0} argument ${1} unacceptable." 1>&2
	exit 1
    fi
    shift	# consume the argument
done

exit ${?}
