add logging

This commit is contained in:
Jochen Welzel 2021-01-07 12:58:28 +01:00
parent 7ac1988d75
commit b4b0b16282

View File

@ -35,7 +35,7 @@ DATE=$(date "+%Y%m%d%H%M")
DATE30=$(date -d "-30day" "+%Y%m%d")
SNAPNAME="backup_$DATE"
echo "$(date +%Y-%m-%d_%H:%M): Starting Backup of $VM"
echo "$(date +%Y-%m-%d_%H:%M:%S): Starting Backup of $VM"
abortFunction()
{
@ -57,6 +57,7 @@ zfs create tank/snapshots/$VM
# create virsh xml-dump of vm
virsh dumpxml $VM > /srv/vms/$VM/vm-$VM.xml
echo "$(date +%Y-%m-%d_%H:%M:%S): Create VM snapshot of $VM"
virsh snapshot-create-as --domain $VM --name $SNAPNAME --quiesce --atomic --disk-only $DISKSPEC >/dev/null 2>&1
if [ $? -ne 0 ]
then
@ -70,6 +71,7 @@ then
fi
fi
echo "$(date +%Y-%m-%d_%H:%M:%S): Creating ZFS snapshot $SRCZFS@$SNAPNAME"
zfs snapshot "$SRCZFS"@"$SNAPNAME"
if [ $? -ne 0 ]
then
@ -77,6 +79,7 @@ then
abortFunction
fi
echo "$(date +%Y-%m-%d_%H:%M:%S): Deleting vm disk snapshots"
for disk in $VMDISKS
do
virsh blockcommit $VM $disk --pivot --active >/dev/null
@ -92,23 +95,23 @@ do
fi
done
virsh snapshot-delete $VM $SNAPNAME --metadata
virsh snapshot-delete $VM $SNAPNAME --metadata > /dev/null
SUCCESS=$(zfs get backup:success -H -o value $SRCZFS)
LASTBACKUP=$(zfs get backup:date -H -o value $SRCZFS)
if [[ $SUCCESS == "true" ]]
then
LASTSNAP="backup_$LASTBACKUP"
echo "Sending incremental snapshot from $LASTSNAP"
echo "$(date +%Y-%m-%d_%H:%M:%S): Sending incremental snapshot from $LASTSNAP"
zfs send -R -i "$SRCZFS"@"$LASTSNAP" "$SRCZFS"@"$SNAPNAME" | pv -L $LIMIT | ssh $DESTHOST sudo zfs recv -F $DESTZFS
else
if [[ $LASTBACKUP != "-" ]]
then
LASTSNAP="backup_$LASTBACKUP"
echo "Trying to send incremental snapshot from $LASTSNAP"
echo "$(date +%Y-%m-%d_%H:%M:%S): Trying to send incremental snapshot from $LASTSNAP"
zfs send -R -i "$SRCZFS"@"$LASTSNAP" "$SRCZFS"@"$SNAPNAME" | pv -L $LIMIT | ssh $DESTHOST sudo zfs recv -F $DESTZFS
else
echo "Sending full snapshot"
echo "$(date +%Y-%m-%d_%H:%M:%S): Sending full snapshot"
zfs send -R "$SRCZFS"@"$SNAPNAME" | pv -L $LIMIT | ssh $DESTHOST sudo zfs recv -F $DESTZFS
fi
fi
@ -116,9 +119,10 @@ if [ $? -eq 0 ]
then
zfs set backup:success="true" $SRCZFS
zfs set backup:date="$DATE" $SRCZFS
zfs set backup:failed="-" $SRCZFS
if [ -n $LASTSNAP ]
then
echo "Deleting old ZFS snapshot $LASTSNAP"
echo "$(date +%Y-%m-%d_%H:%M:%S): Deleting old ZFS snapshot $LASTSNAP"
zfs destroy "$SRCZFS"@"$LASTSNAP"
fi
else
@ -134,8 +138,11 @@ if [ ${#OLDSNAPS} -gt 0 ]
then
for oldsnap in $OLDSNAPS
do
echo "$(date +%Y-%m-%d_%H:%M:%S): Deleting old snapshots on target"
ssh $DESTHOST sudo zfs destroy $oldsnap
done
else
echo "No old snapshots to cleanup"
fi
echo "$(date +%Y-%m-%d_%H:%M:%S): Backup Completed"