zfs_snapshot_cron.sh: add custom prefix support

This commit is contained in:
Alice Gaudon 2021-01-08 14:57:35 +01:00
parent bc90f87636
commit 8b4ffe3349

View File

@ -1,6 +1,5 @@
#!/bin/bash #!/bin/bash
PREFIX="zsc-"
CONFIG_FILE="$HOME/.zfs_snapshot_cron" CONFIG_FILE="$HOME/.zfs_snapshot_cron"
FORMAT='+%Y-%m-%d' FORMAT='+%Y-%m-%d'
@ -16,7 +15,17 @@ handle_pool() {
W=$3 W=$3
M=$4 M=$4
Y=$5 Y=$5
echo "--- $POOL --- $D $W $M $Y"
PREFIX=$6
if test -z "$PREFIX"; then
PREFIX="zsc-"
fi
if test "!no_prefix!" = "$PREFIX"; then
PREFIX=""
fi
echo "--- $POOL --- $D $W $M $Y $PREFIX"
SNAPSHOTS=($(zfs list -t snapshot -o name | grep "$POOL@$PREFIX" | sort -r)) SNAPSHOTS=($(zfs list -t snapshot -o name | grep "$POOL@$PREFIX" | sort -r))
echo "Snapshots: ${#SNAPSHOTS[@]}" echo "Snapshots: ${#SNAPSHOTS[@]}"
@ -30,7 +39,7 @@ handle_pool() {
EXISTS=1 EXISTS=1
fi fi
scomp=($(split_date "$sdate") $(date -d "$sdate" "+%u %W")) scomp=($(split_date "$sdate") $(date -d "$stripped_date" "+%u %W"))
echo -n "${scomp[3]} ${scomp[4]} | " echo -n "${scomp[3]} ${scomp[4]} | "
@ -60,20 +69,20 @@ handle_pool() {
fi fi
} }
cat "$CONFIG_FILE" | while IFS=: read pool recursive d w m y; do cat "$CONFIG_FILE" | while IFS=: read pool recursive d w m y prefix; do
echo "| ##### Pool: $pool Recursive: $recursive d: $d w: $w m: $m y: $y #####" echo "| ##### Pool: $pool Recursive: $recursive Prefix: $prefix d: $d w: $w m: $m y: $y #####"
if [ $recursive = "true" ]; then if [ $recursive = "true" ]; then
SUBPOOLS=($(zfs list -o name | grep "$pool/")) SUBPOOLS=($(zfs list -o name | grep "$pool/"))
echo -e "Subpools: ${SUBPOOLS[@]}" echo -e "Subpools: ${SUBPOOLS[@]}"
echo -e '|' echo -e '|'
for p in "${SUBPOOLS[@]}"; do for p in "${SUBPOOLS[@]}"; do
handle_pool $p $d $w $m $y | awk '{print "| " $0}' handle_pool $p $d $w $m $y $prefix | awk '{print "| " $0}'
echo -e '|' echo -e '|'
done done
fi fi
handle_pool $pool $d $w $m $y | awk '{print "| " $0}' handle_pool $pool $d $w $m $y $prefix | awk '{print "| " $0}'
echo -e "----------------------------------------------------------------------" echo -e "----------------------------------------------------------------------"
done done