zsc.sh: fix snapshot creation and deletion order

fix script lock
use faster zfs snapshot list commands
This commit is contained in:
Alice Gaudon 2021-03-08 12:13:44 +01:00
parent 06a1b7aec7
commit 9e16259e52

41
zsc.sh
View File

@ -9,8 +9,8 @@ DATE=$(date $FORMAT)
### LOCK ###
echo "Locking ZSC"
if ! lockfile-create -p -r 2 "zsc"; then
echo "Locking $0"
if ! lockfile-create -p -r 2 "$0"; then
echo "Another backup is running, aborting"
exit 49
fi
@ -37,20 +37,28 @@ handle_dataset() {
PREFIX=""
fi
echo "--- $DATASET --- $D $W $M $Y $PREFIX"
SNAPSHOTS=($(zfs list -t snapshot -o name | grep "$DATASET@$PREFIX" | sort -r))
echo "Snapshots: ${#SNAPSHOTS[@]}"
EXISTS=0
# Create new snapshot
if [ $(zfs list -t snapshot -o name "$DATASET" | grep "$DATE" | head -n1 | wc -l) -eq 0 ]; then
echo -e
if [ $AUTO_CREATE == "true" ]; then
echo -e "Creating snapshot $DATASET@$PREFIX$DATE"
zfs snapshot "$DATASET@$PREFIX$DATE"
else
echo -e "Skip snapshot creation"
fi
echo -e
fi
# Delete old snapshots
SNAPSHOTS=($(zfs list -t snapshot -o name "$DATASET" | grep "@$PREFIX" | sort -r))
for s in "${SNAPSHOTS[@]}"; do
sdate=$(echo "$s" | awk 'match($0, /.+@'"$PREFIX"'(.+)/, a) {print a[1]}')
if [ "$sdate" = $DATE ]; then
EXISTS=1
fi
scomp=($(split_date "$sdate") $(date -d "$stripped_date" "+%u %W"))
echo -n "${scomp[3]} ${scomp[4]} | "
@ -74,15 +82,6 @@ handle_dataset() {
echo "DISCARD"
fi
done
if [ $EXISTS -eq 0 ]; then
if [ $AUTO_CREATE == "true" ]; then
echo -e "Creating snapshot $DATASET@$PREFIX$DATE"
zfs snapshot $DATASET@$PREFIX$DATE
else
echo -e "Skip snapshot creation"
fi
fi
}
cat "$CONFIG_FILE" | while IFS=: read dataset recursive auto_create d w m y prefix; do
@ -105,5 +104,5 @@ done
### UNLOCK ###
echo "Unlocking ZSC"
lockfile-remove "zsc"
echo "Unlocking $0"
lockfile-remove "$0"