From 4921a32c14ac8c48ab2f277f03f2337bcbbb6cd3 Mon Sep 17 00:00:00 2001 From: IntenseWebs Date: Sat, 6 Apr 2024 11:17:37 -0500 Subject: [PATCH] Update KVM Export-Import Help --- kvm-export-import.sh | 69 +++++++++++++++++++++++++++++++++++++++++++ kvm-export-import.txt | 4 +++ mariadb.txt | 0 3 files changed, 73 insertions(+) create mode 100644 kvm-export-import.sh create mode 100644 mariadb.txt diff --git a/kvm-export-import.sh b/kvm-export-import.sh new file mode 100644 index 0000000..96be879 --- /dev/null +++ b/kvm-export-import.sh @@ -0,0 +1,69 @@ +#!/usr/bin/env bash + +# ---------------------------------------------------------------------------------------------------- +# AUTHOR : KARTHICK S +# PURPOSE : THIS SCRIPT WILL EXPORT/IMPORT THE CONFIG AND VM DISK. +# +# usage: +# export function will take care of exporting the necessary for all VM. Run as " export" +# import function will take care of importing the necessary for all VM. Run as " import" +# +# NOTE: Do not add trailing / for the directory when giving export and import path. +#------------------------------------------------------------------------------------------------------ + +# Trigger the script with root user or exit. +if [[ ${UID} -ne 0 ]]; then + echo -e "[EXIT] - Run the script as root user or with sudo privilege..." + exit +fi + +function export_vm(){ + + # Get the export location. + read -p "Provide the directory path where disk and config files to be exported: " EXPORT_LOCATION + + # Create the destination directory if not exists. + [[ -d ${EXPORT_LOCATION} ]] || mkdir -p ${EXPORT_LOCATION} + + # Exporting the config using virsh dumpxml command. + VM_NAMES=($(virsh list --all| awk '(NR>2)' | awk '{ print $2 }')) + for VM in ${VM_NAMES[@]}; do + virsh dumpxml ${VM} > ${EXPORT_LOCATION}/${VM}.xml + done + + # Using rsync copy the entire directory from default location. + echo -e "\n[ Copying disk images ]\n" && sudo rsync -avxp --progress /var/lib/libvirt/images ${EXPORT_LOCATION} + + echo -e "\n[ Exported Files ] \n" && ls -lR ${EXPORT_LOCATION} + +} + +function import_vm(){ + + # Get the source location. + read -p "Provide the directory path where disk and config files are stored: " SOURCE_LOCATION + + # Throws error if directory is not available and exit. + [[ -d ${SOURCE_LOCATION} ]] || { echo "Directory not available"; exit 1 ; } + + # Copy all the files to default disk location. + echo -e "[ Copying disk images ]\n" && sudo rsync -avxp --progress ${SOURCE_LOCATION}/images /var/lib/libvirt/ + + if [[ $? -eq 0 ]]; then + # Define VM + echo -e "\n[ Defining VM ]\n" + for XML_FILE in ${SOURCE_LOCATION}/*.xml; do + virsh define --file ${XML_FILE} + done + echo -e "\n[ Imported VM List ]\n" && virsh list --all + fi + +} + +case $1 in + export ) export_vm ;; + import ) import_vm ;; + *) echo -e "USAGE : +kvm_export_import.sh export - Export config and disk +kvm_export_import.sh import - Define VM and copy the disk"; exit +esac \ No newline at end of file diff --git a/kvm-export-import.txt b/kvm-export-import.txt index 6e5e7a2..9874b89 100644 --- a/kvm-export-import.txt +++ b/kvm-export-import.txt @@ -1,3 +1,7 @@ +virsh list --all +virsh shutdown ns1.intensewebs.com +virsh dumpxml ns1.intensewebs.com /home/iw/ + # EXAMPLE virsh dumpxml vm_name | less virsh dumpxml ubuntu20.04-clone | grep -i "source" virsh dumpxml ubuntu20.04-clone | grep -i "MEMORY" diff --git a/mariadb.txt b/mariadb.txt new file mode 100644 index 0000000..e69de29