#!/usr/bin/sudo /bin/bash
# (Debian: Bitte ersetzen Sie die Zeile 1 (die "shebang") mit "#!/bin/bash" und starten Sie das Skript als root)
# (Debian: Please replace the line 1 (the "shebang") with "#!/bin/bash" and start the script as root)
###
# LastUpdate: 2018-03-31
# Version: 2.0
# Author: Jörg Reinholz, fastix WebDesign & Consult Kassel, http://www.fastix.org
# Requirements: Ubuntu 16.04 (Debian, Mint e.t.c.) with btrfs on root-filesstem
# Licence: https://code.fastix.org/lizenz.php
# Non-Warranty: https://code.fastix.org/haftung.php
###
### Config: ###
# Anzahl der Snapshots welche erhalten bleiben:
# Number of snapshots which are preserved:
noDel=5;
### Hinweis: Starten Sie das Skript mit der Option "-a" um alle Snapshots zu löschen.
### Hint: Start the script with the option "-a" to delete all snapshots
### Program. Do not edit until you know EXACT what you do! ###
if [ "$1" = "-a" ]; then
noDel=0;
fi
rootdevice=$(/bin/df / | /usr/bin/tail -n1 | /usr/bin/cut -d " " -f1);
workdir=$(/bin/mktemp -d -t "delete-apt-snapshots-XXXXX");
olddir=$(/bin/pwd);
/bin/mount -t btrfs "${rootdevice}" "${workdir}";
cd "${workdir}";
echo;
lSnapshots=$(/bin/ls -d @apt-snapshot* 2> /dev/null);
count=0;
for snapshot in ${lSnapshots}; do
count=$((${count}+1));
done
cDelete=$((${count}-${noDel}));
if [ ${cDelete} -lt 0 ]; then
cDelete=0;
fi
echo "Snapshots: ${count}";
echo "To Delete: ${cDelete}";
for snapshot in ${lSnapshots}; do
if [ ${cDelete} -gt 0 ]; then
/bin/echo -n "delete ${snapshot} .. ";
/sbin/btrfs subvolume delete "${snapshot}/@" 1> /dev/null 2> /dev/null;
/sbin/btrfs subvolume delete "${snapshot}" 1> /dev/null 2> /dev/null;
/bin/echo " passed";
else
/bin/echo "skipping ${snapshot}";
fi
cDelete=$((${cDelete}-1));
done
/bin/echo;
cd "${olddir}";
/bin/umount "${workdir}";
/bin/rmdir "${workdir}";