The seamless migration and backup of virtual machines (VMs) are crucial for maintaining system stability and data integrity. This article provides a detailed walkthrough of exporting and importing Kernel-based Virtual Machines (KVMs) on AlmaLinux 9 and Rocky Linux 9, offering both command-line instructions and a robust bash script for automation. The process involves two key steps: backing up the VM's disk image and exporting its configuration data. This allows for efficient restoration, either on the same host or a different one, significantly reducing downtime and the effort associated with rebuilding VMs from scratch.
Understanding the KVM Environment and Virtual Machine Identification
virsh list --all
virsh dumpxml <VM_Name>
virsh dumpxml AlmaLinux9 > AlmaLinux9.xml
Locating the Virtual Machine Disk Image
virsh dumpxml <VM_Name> | grep -i "source"
<source file='/var/lib/libvirt/images/AlmaLinux9.qcow2'/>
VM_NAMES=($(virsh list --all | awk '(NR>2)' | awk '{print $2}'))
for VM in ${VM_NAMES[@]}; do
DISK_PATH=$(virsh dumpxml ${VM} | grep -i "<source file")
echo "${VM} ${DISK_PATH}"
done | column -t
Exporting the Domain Configuration: The VM's configuration, including memory allocation, CPU settings, network interfaces, and other crucial parameters, is captured in an XML file using the virsh dumpxml command:virsh dumpxml <VM_Name> > /path/to/backup/<VM_Name>.xml
This command exports the VM's configuration to an XML file located in the specified backup directory. Replacing /path/to/backup/ with your chosen backup location and <VM_Name> with the VM's name is crucial. Backing Up the Disk Image: The actual contents of the VM's virtual hard drive, stored in the qcow2 image file, are copied to the backup location using the cp command:sudo cp /var/lib/libvirt/images/<disk_image_name.qcow2> /path/to/backup/
Remember to replace /var/lib/libvirt/images/<disk_image_name.qcow2> with the actual path to the disk image file as determined in the previous section.
Restoring the Disk Image: The backup of the disk image is copied back to the default location or a new designated location:sudo cp /path/to/backup/<disk_image_name.qcow2> /var/lib/libvirt/images/
Again, ensure the correct paths are used. Importing the Domain Configuration: Finally, the VM's configuration is imported using the virsh define command:virsh define /path/to/backup/<VM_Name>.xml
This command reads the XML configuration file and creates a new VM based on the settings within the file. The VM will be added to the list of managed VMs, ready for use.
#!/usr/bin/env bash
if [[ ${UID} -ne 0 ]]; then
echo "[EXIT] This script must be run as root."
exit 1
fi
function export_vm() {
read -p "Enter the export destination directory: " EXPORT_DIR
[[ -d ${EXPORT_DIR} ]] || mkdir -p ${EXPORT_DIR}
VM_NAMES=($(virsh list --all | awk '(NR>2)' | awk '{print $2}'))
for VM in ${VM_NAMES[@]}; do
virsh dumpxml ${VM} > ${EXPORT_DIR}/${VM}.xml
DISK_PATH=$(virsh dumpxml ${VM} | grep -i "<source file" | cut -d '"' -f 2)
sudo cp "${DISK_PATH}" "${EXPORT_DIR}/"
done
echo "VM configurations and disk images exported to ${EXPORT_DIR}"
}
function import_vm() {
read -p "Enter the import source directory: " IMPORT_DIR
[[ -d ${IMPORT_DIR} ]] || { echo "Directory not found"; exit 1; }
sudo rsync -av "${IMPORT_DIR}/" "/var/lib/libvirt/"
for XML_FILE in ${IMPORT_DIR}/*.xml; do
virsh define "${XML_FILE}"
done
echo "VMs imported successfully."
virsh list --all
}
case $1 in
export ) export_vm ;;
import ) import_vm ;;
* ) echo "Usage: $0 [export|import]" ;;
esac
0 comments:
Post a Comment