dotfiles/overlays/rolling_datasets/bin/roll_dataset
magic_rb 99005cd768
Fully fixup rolling datasets
Signed-off-by: magic_rb <magic_rb@redalder.org>
2023-11-03 20:10:48 +01:00

32 lines
779 B
Bash

# -*- mode: shell-script -*-
source @out@/share/functions/get_old_datasets
export "PATH=$PATH:@runtimePath@"
if [[ "$#" != 2 ]] ; 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")"