Skip to content

Instantly share code, notes, and snippets.

@fergalmoran
Created November 8, 2023 21:19
Show Gist options
  • Save fergalmoran/444899623b628fcfd2e9b5adb37acf3a to your computer and use it in GitHub Desktop.
Save fergalmoran/444899623b628fcfd2e9b5adb37acf3a to your computer and use it in GitHub Desktop.
Virsh backup
#!/usr/bin/env bash
DOMAIN=${1:-win11}
BACKUP_CONFIG=/home/fergalm/dotfiles/virsh-win11-backup.xml
BACKUP_PATH=/mnt/storage/backups/vms/libvirt/$DOMAIN
XML_BACKUP=$BACKUP_PATH/$DOMAIN.xml
BACKUP_IMAGE=$BACKUP_PATH/$DOMAIN.disk
OLD_BACKUP_IMAGE=$BACKUP_PATH/$DOMAIN.disk.$(date "+%Y.%m.%d-%H.%M.%S")
echo BACKUP_IMAGE $BACKUP_IMAGE
echo OLD_BACKUP_IMAGE $OLD_BACKUP_IMAGE
if [[ -f $BACKUP_IMAGE ]]; then
echo Moving old image
sudo mv $BACKUP_IMAGE $OLD_BACKUP_IMAGE
fi
echo Starting VM
# start VM if not running
virsh domstate $DOMAIN | grep running
if [ $? -ne 0 ] ; then
echo "Starting VM"
virsh start $DOMAIN
else
echo "Connecting to VM"
fi
# start the actual backup
echo Dumping XML
virsh dumpxml $DOMAIN > $XML_BACKUP
echo Backing up disk file
virsh backup-begin $DOMAIN --backupxml $BACKUP_CONFIG
echo Waiting for job exit
virsh event $DOMAIN --event block-job
#watch virsh domjobinfo win11
virsh domjobinfo win11 --completed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment