dotfiles/overlays/rolling_datasets/bin/roll_dataset
2023-10-07 22:42:14 +02:00

30 lines
741 B
Bash

# -*- mode: shell-script -*-
source @out@/share/functions/get_old_datasets
if [[ "$#" != 3 ]] ; then
echo "This script requires precisely 2 arguments!"
exit 1
fi
dataset="$1"
to_keep="$2"
generation="$(zfs get :generation "$dataset" -H -o value)"
generation="$(("$generation" + 1))"
zfs set ":generation=$generation" "$dataset"
zfs send "$dataset" | zfs recv "$dataset/$generation"
zfs rollback "$dataset@blank" && echo "rollback complete"
while IFS="\n" read gen
do
printf "$gen < $(("$generation" - "$to_keep")) => "
if [ "$gen" -lt "$(("$generation" - "$to_keep"))" ]; then
zfs destroy -r "$dataset/$gen"
echo "destroyed"
else
echo "kept"
fi
done <<< "$(get_old_datasets "$dataset")"