You can kick off ad-hoc backups of KVM guest Virtual Machines easily:
virsh backup-begin vmName
This will backup all the disks attached to the specified VM and store them in the same directory where the original VM disk is stored along with a timestamp in the filename.
This will not backup the configuration XML file for the VM
You can create a backup configuration file to get a specific configuration on any given guest VM. This will allow you to include or exclude disks or choose the type of backup. Like this:
virsh backup-begin --domain vmName --backupxml /path/to/config.xml
The Bash script below will create a backup configuration XML file in /root/backupConfigs
for each running virtual machine on the host. It will ignore virtual machines that are not in a running state. It will also configure a destination target for each of your VM disks at /mnt/nfs/backup
. This just means that when a backup job runs, it will store the backup in that location. The location doesn't have to exist at this point, but it must exist before jobs run with the generated configuration.
After reading the manual, you may want to further customize one of those backup configuration XML files. If you have virtual machines with more than one disk or you wish to only backup one or some but not all disks on a virtual machine, this is a good idea.
Assumptions:
/root/backupConfigs
/mnt/nfs/backup/
#!/bin/bash
machines=$(virsh list | awk '/running/{print $2}')
for machine in $machines
do
diskName=$(virsh domblklist $machine --details | awk '/disk/{print $4}')
disk=$(virsh domblklist $machine --details | awk '/disk/{print $3}')
diskFile=$(basename $diskName)
if test -f /root/backupConfigs/${machine}.xml; then
rm -f /root/backupConfigs/${machine}.xml
fi
cat << EOF >> /root/backupConfigs/${machine}.xml
<domainbackup>
<disks>
<disk name='${disk}' backup='yes' type='file'>
<target file='/mnt/nfs/backup/${diskFile}'/>
</disk>
</disks>
</domainbackup>
EOF
done
~/scripts/createBackupConfig.sh
chmod +x ~/scripts/createBackupConfig.sh
cd ~/scripts && ./createBackupConfig.sh
We will use a cron job to execute our backup jobs at 03:00 every Sunday. First step is to save a script that executes the job for each running virtual machine.
This script will save each running virtual machine's configuration file as $Name.xml
to /mnt/nfs/backup
(this will be very fast) and then it will begin a job that backs up the disk specified in the configuration file to that same location (this will not be very fast).
Assumptions:
/root/backupConfigs/
/mnt/nfs/backup/
#!/bin/bash
machines=$(virsh list | awk '/running/{print $2}')
for machine in $machines
do
virsh dumpxml $machine > /mnt/nfs/backup/${machine}.xml
virsh backup-begin --domain $machine --backupxml /root/backupConfigs/${machine}.xml
done
~/scripts/run-backup.sh
chmod +x ~/scripts/run-backup.sh
I like to use crontab.guru to validate my schedule. The example below sets the backup script to run every Sunday at 03:00.
0 3 * * 0 /root/scripts/run-backup.sh
Assumptions:
/root/backupConfigs
for each running virtual machine/root/scripts/run-backup.sh
Procedure:
EDITOR=/usr/bin/nano crontab -e
0 3 * * 0 /root/scripts/run-backup.sh
on the bottom line of the file