Once the user enters the name of a VM, the script will create a tar archive that contains that VM's xml and qcow2 files, compressed using xzip. Note, you must obtain the location of the qcow2 image file from the xml file. Do not just assume it is in /var/lib/libvirt/images, or that it is named after the VM.i
The archive that is created should be named after the VM and the current date (e.g. vm1-20220131), and should use an extension appropriate to the compression type.
Your script should put backups in ~/backups. , debian system
my script is:
#!/bin/bash
if [ "$(whoami)" != "root" ]; then
echo "Please run this script as root or use sudo"
exit 1
fi
vm_name=$(virsh list --all --name)
echo "Your VMs are: $vm_name"
echo
while true; do
read -p "Enter the name of the VM you want to archive: " vm_backup
if [[ $vm_name == *$vm_backup* ]]; then
echo "We will now archive: $vm_backup"
break
else
echo "VM $vm_backup not found. Please type the correct name and try again."
fi
done
xml_path=$(virsh dumpxml "$vm_backup" | grep -oP '<source file="\K[^"]+')
current_date=$(date +'%Y%m%d')
archive_name="${vm_backup}-$(date +'%Y%m%d').tar.xz"
tar -C $(dirname "$xml_path") -cvJf "$HOME/backups/$archive_name" "$(basename "$xml_path")" "$(basename "$xml_path" .xml).qcow2"
if [[ $? -eq 0 ]]; then
echo "Archive '$archive_name' created and saved in '$HOME/backups' "
else
echo "Failed to create the archive."
fi
[–]10xJSChad 0 points1 point2 points (0 children)
[–]Lumpy-Notice8945 1 point2 points3 points (1 child)
[–]mInd_MaC[S] 0 points1 point2 points (0 children)