#!/bin/bash
#
# sys-snapshots-clean
#
# Clean old snapshosts
#
# (c)2018 Marek Uher <marek@uher.info>
#

SNAPPER_KEEP_SNAPSHOTS="2"
SNAPPER_CONFIG=""

for SNAPPER_CONFIG in `snapper list-configs | egrep -v "(^Config|^-)" | awk '{ print $1 }'`; do
	for SNAPPER_SNAPSHOT in `snapper -c ${SNAPPER_CONFIG} list | grep timeline | awk '{ print $1 }'`; do
		echo -en "snapper -c ${SNAPPER_CONFIG} delete ${SNAPPER_SNAPSHOT}: [error code = "
		snapper -c ${SNAPPER_CONFIG} delete ${SNAPPER_SNAPSHOT}
		echo -en $?
		echo "]"
	done
done
for SNAPPER_CONFIG in `snapper list-configs | egrep -v "(^Config|^-)" | awk '{ print $1 }'`; do
	SNAPSHOTS_TO_DELETE=`snapper -c ${SNAPPER_CONFIG} list | grep "Regular system backup" | wc -l`
	SNAPSHOTS_TO_DELETE=`expr ${SNAPSHOTS_TO_DELETE} - 2`
	if [ "${SNAPSHOTS_TO_DELETE}" -lt "${SNAPPER_KEEP_SNAPSHOTS}" ]; then SNAPSHOTS_TO_DELETE=0 ; fi
	for SNAPSHOT in `snapper -c ${SNAPPER_CONFIG} list | grep "Regular system backup" | head -n "${SNAPSHOTS_TO_DELETE}" | awk '{ print $1 }'`; do
		echo -en "snapper -c ${SNAPPER_CONFIG} delete ${SNAPSHOT}: [error code = "
		snapper -c ${SNAPPER_CONFIG} delete ${SNAPSHOT}
	echo -en $?
	echo "]"
	done
done

exit 0

# /* End of file: sys-snapshots-clean */
