add old snapshot deletion

This commit is contained in:
Jochen Welzel 2021-01-07 09:54:55 +01:00
parent 7fb8538a44
commit 44dc136c8a

View File

@ -35,6 +35,13 @@ DATE=$(date "+%Y%m%d%H%M")
DATE30=$(date -d "-30day" "+%Y%m%d")
SNAPNAME="backup_$DATE"
abortFunction()
{
zfs set backup:success="false" $SRCZFS
zfs set backup:failed="$DATE" $SRCZFS
exit 1
}
VMDISKS=$(virsh domblklist $VM | egrep -o vd.)
DISKSPEC=""
for disk in $VMDISKS
@ -55,7 +62,7 @@ then
if [ $? -ne 0 ]
then
echo "VM snapshot creation failed" >&2
exit 1
abortFunction
else
echo "Snapshot created without --quiesce"
fi
@ -65,7 +72,7 @@ zfs snapshot "$SRCZFS"@"$SNAPNAME"
if [ $? -ne 0 ]
then
echo "ZFS snapshot creation failed" >&2
exit 1
abortFunction
fi
for disk in $VMDISKS
@ -79,7 +86,7 @@ do
if [[ $disk == *"snapshot"* ]]
then
echo "Snapshot deletion failed for $disk" >&2
exit 1
abortFunction
fi
done
@ -102,9 +109,20 @@ then
zfs set backup:date="$DATE" $SRCZFS
zfs destroy "$SRCZFS"@"$LASTSNAP"
else
zfs set backup:success="false" $SRCZFS
zfs set backup:failed="$DATE" $SRCZFS
abortFunction
fi
zfs destroy tank/snapshots/$VM
# cleanup old backups
OLDSNAPS=$(ssh $DESTHOST zfs list -r -t snapshot -o name $DESTZFS 2>/dev/null | grep $DATE30)
if [ ${#OLDSNAPS} -gt 0 ]
then
for oldsnap in $OLDSNAPS
do
ssh $DESTHOST sudo zfs destroy $oldsnap
done
else
echo "No old snapshots to cleanup"
fi