2020-02-05 05:48:37 +01:00
|
|
|
#!/bin/bash
|
|
|
|
|
2021-03-04 13:15:17 +01:00
|
|
|
# ZSC - zfs snapshot cron
|
|
|
|
|
|
|
|
CONFIG_FILE="$HOME/.zsc"
|
2020-02-05 05:48:37 +01:00
|
|
|
|
|
|
|
FORMAT='+%Y-%m-%d'
|
|
|
|
DATE=$(date $FORMAT)
|
|
|
|
|
2021-03-04 14:21:35 +01:00
|
|
|
|
|
|
|
### LOCK ###
|
|
|
|
echo "Locking ZSC"
|
|
|
|
if ! lockfile-create -p -r 2 "zsc"; then
|
|
|
|
echo "Another backup is running, aborting"
|
|
|
|
exit 49
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
2020-02-05 05:48:37 +01:00
|
|
|
split_date() {
|
|
|
|
echo "$1" | sed -E 's/([0-9]{4})-([0-9]{2})-([0-9]{2})/\1\n\2\n\3/'
|
|
|
|
}
|
|
|
|
|
2021-03-04 14:21:35 +01:00
|
|
|
handle_dataset() {
|
|
|
|
DATASET=$1
|
2020-02-05 05:48:37 +01:00
|
|
|
D=$2
|
|
|
|
W=$3
|
|
|
|
M=$4
|
|
|
|
Y=$5
|
2021-01-08 14:57:35 +01:00
|
|
|
|
|
|
|
PREFIX=$6
|
|
|
|
if test -z "$PREFIX"; then
|
|
|
|
PREFIX="zsc-"
|
|
|
|
fi
|
|
|
|
|
|
|
|
if test "!no_prefix!" = "$PREFIX"; then
|
|
|
|
PREFIX=""
|
|
|
|
fi
|
|
|
|
|
2021-03-04 14:21:35 +01:00
|
|
|
echo "--- $DATASET --- $D $W $M $Y $PREFIX"
|
2020-02-05 05:48:37 +01:00
|
|
|
|
2021-03-04 14:21:35 +01:00
|
|
|
SNAPSHOTS=($(zfs list -t snapshot -o name | grep "$DATASET@$PREFIX" | sort -r))
|
2020-02-05 05:48:37 +01:00
|
|
|
echo "Snapshots: ${#SNAPSHOTS[@]}"
|
|
|
|
|
|
|
|
EXISTS=0
|
|
|
|
|
|
|
|
for s in "${SNAPSHOTS[@]}"; do
|
|
|
|
sdate=$(echo "$s" | awk 'match($0, /.+@'"$PREFIX"'(.+)/, a) {print a[1]}')
|
|
|
|
|
|
|
|
if [ "$sdate" = $DATE ]; then
|
|
|
|
EXISTS=1
|
|
|
|
fi
|
|
|
|
|
2021-01-08 14:57:35 +01:00
|
|
|
scomp=($(split_date "$sdate") $(date -d "$stripped_date" "+%u %W"))
|
2020-02-05 05:48:37 +01:00
|
|
|
|
|
|
|
echo -n "${scomp[3]} ${scomp[4]} | "
|
|
|
|
|
|
|
|
echo -n "${sdate}: "
|
|
|
|
|
|
|
|
if [ $D -gt 0 ]; then
|
|
|
|
D=$(( D-1 ))
|
|
|
|
echo "KEEP; D: $D"
|
2020-04-07 00:11:38 +02:00
|
|
|
elif [ $W -gt 0 ] && [ ${scomp[3]#0} -eq 1 ]; then
|
2020-02-05 05:48:37 +01:00
|
|
|
W=$(( W-1 ))
|
|
|
|
echo "KEEP; W: $W"
|
2020-04-07 00:11:38 +02:00
|
|
|
elif [ $M -gt 0 ] && [ ${scomp[3]#0} -eq 1 ] && [ $(( ${scomp[4]#0} % 4 )) -eq 0 ]; then
|
2020-02-05 05:48:37 +01:00
|
|
|
M=$(( M-1 ))
|
|
|
|
echo "KEEP; M: $M"
|
2020-04-07 00:11:38 +02:00
|
|
|
elif [ $Y -gt 0 ] && [ ${scomp[3]#0} -eq 1 ] && [ ${scomp[4]#0} -eq 4 ]; then
|
2020-02-05 05:48:37 +01:00
|
|
|
Y=$(( Y-1 ))
|
|
|
|
echo "KEEP; Y: $Y"
|
|
|
|
else
|
|
|
|
zfs destroy "$s"
|
|
|
|
echo "DISCARD"
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
|
|
|
if [ $EXISTS -eq 0 ]; then
|
2021-03-04 14:21:35 +01:00
|
|
|
echo -e "Creating snapshot $DATASET@$PREFIX$DATE"
|
|
|
|
zfs snapshot $DATASET@$PREFIX$DATE
|
2020-02-05 05:48:37 +01:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2021-03-04 14:21:35 +01:00
|
|
|
cat "$CONFIG_FILE" | while IFS=: read dataset recursive d w m y prefix; do
|
|
|
|
echo "| ##### Dataset: $dataset Recursive: $recursive Prefix: $prefix d: $d w: $w m: $m y: $y #####"
|
2020-02-05 05:48:37 +01:00
|
|
|
|
|
|
|
if [ $recursive = "true" ]; then
|
2021-03-04 14:21:35 +01:00
|
|
|
SUBDATASETS=($(zfs list -o name | grep "$dataset/"))
|
|
|
|
echo -e "| Subdatasets: ${SUBDATASETS[@]}"
|
2020-02-05 05:48:37 +01:00
|
|
|
echo -e '|'
|
2021-03-04 14:21:35 +01:00
|
|
|
for p in "${SUBDATASETS[@]}"; do
|
|
|
|
handle_dataset $p $d $w $m $y $prefix | awk '{print "| " $0}'
|
2020-02-05 05:48:37 +01:00
|
|
|
echo -e '|'
|
|
|
|
done
|
|
|
|
fi
|
|
|
|
|
2021-03-04 14:21:35 +01:00
|
|
|
handle_dataset $dataset $d $w $m $y $prefix | awk '{print "| " $0}'
|
2020-02-05 05:48:37 +01:00
|
|
|
|
|
|
|
echo -e "----------------------------------------------------------------------"
|
|
|
|
done
|
2021-03-04 14:21:35 +01:00
|
|
|
|
|
|
|
|
|
|
|
### UNLOCK ###
|
|
|
|
echo "Unlocking ZSC"
|
|
|
|
lockfile-remove "zsc"
|