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