zsc.sh: fix snapshot creation and deletion order
fix script lock use faster zfs snapshot list commands
This commit is contained in:
parent
06a1b7aec7
commit
9e16259e52
41
zsc.sh
41
zsc.sh
@ -9,8 +9,8 @@ DATE=$(date $FORMAT)
|
|||||||
|
|
||||||
|
|
||||||
### LOCK ###
|
### LOCK ###
|
||||||
echo "Locking ZSC"
|
echo "Locking $0"
|
||||||
if ! lockfile-create -p -r 2 "zsc"; then
|
if ! lockfile-create -p -r 2 "$0"; then
|
||||||
echo "Another backup is running, aborting"
|
echo "Another backup is running, aborting"
|
||||||
exit 49
|
exit 49
|
||||||
fi
|
fi
|
||||||
@ -37,20 +37,28 @@ handle_dataset() {
|
|||||||
PREFIX=""
|
PREFIX=""
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
|
||||||
echo "--- $DATASET --- $D $W $M $Y $PREFIX"
|
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
|
for s in "${SNAPSHOTS[@]}"; do
|
||||||
sdate=$(echo "$s" | awk 'match($0, /.+@'"$PREFIX"'(.+)/, a) {print a[1]}')
|
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"))
|
scomp=($(split_date "$sdate") $(date -d "$stripped_date" "+%u %W"))
|
||||||
|
|
||||||
echo -n "${scomp[3]} ${scomp[4]} | "
|
echo -n "${scomp[3]} ${scomp[4]} | "
|
||||||
@ -74,15 +82,6 @@ handle_dataset() {
|
|||||||
echo "DISCARD"
|
echo "DISCARD"
|
||||||
fi
|
fi
|
||||||
done
|
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
|
cat "$CONFIG_FILE" | while IFS=: read dataset recursive auto_create d w m y prefix; do
|
||||||
@ -105,5 +104,5 @@ done
|
|||||||
|
|
||||||
|
|
||||||
### UNLOCK ###
|
### UNLOCK ###
|
||||||
echo "Unlocking ZSC"
|
echo "Unlocking $0"
|
||||||
lockfile-remove "zsc"
|
lockfile-remove "$0"
|
||||||
|
Loading…
Reference in New Issue
Block a user