6.10.2.1 Method 1: Back Up All of the KVM Guests

You can back up all of the guests by backing up the storage repository under /EXAVMIMAGES.

The backup destination should be separate from the KVM host server, such as a writable NFS location, and be large enough to hold the backup. The space needed for the backup is proportional to the number of guests deployed on the system. The space needed for each guest backup is approximately 200 GB.

  1. Prepare the guest images.

    Use the following script to prepare the guest image backups under /EXAVMIMAGES/Backup.

    #!/bin/bash
    
    ScriptStarttime=$(date +%s)
    printf "This script is going to remove the directory /EXAVMIMAGES/Backup if it exists.  If that is not acceptable, exit the script by typing n, manually remove /EXAVMIMAGES/Backup and come back to rerun the script.  Otherwise, press y to continue :"
    read proceed
    if [[ ${proceed} == "n" ]] || [[ ${proceed} == "N" ]]
    then
      exit 0
    elif [[ ${proceed} != "n" ]] && [[ ${proceed} != "N" ]] && [[ ${proceed} != "y" ]] && [[ ${proceed} != "Y" ]]
    then
      echo "Invalid input"
      exit 1
    fi
    rm -rf /EXAVMIMAGES/Backup
    
    ## Create the Backup Directory
    
    mkdirStartTime=$(date +%s)
    find /EXAVMIMAGES -type d|grep -v 'lost+found'| awk '{print "mkdir -p /EXAVMIMAGES/Backup"$1}'|sh
    mkdir -p /EXAVMIMAGES/Backup/XML
    mkdirEndTime=$(date +%s)
    mkdirTime=$(expr ${mkdirEndTime} - ${mkdirStartTime})
    echo "Backup Directory creation time :" ${mkdirTime}" seconds"
    
    ## Create reflinks for files not in /EXAVMIMAGES/GuestImages
    
    relinkothesStartTime=$(date +%s)
    
    find /EXAVMIMAGES/ -not -path "/EXAVMIMAGES/GuestImages/*" -not -path "/EXAVMIMAGES/Backup/*" -type f|awk '{print "cp --reflink",$0,"/EXAVMIMAGES/Backup"$0}'|sh
    
    relinkothesEndTime=$(date +%s)
    reflinkothesTime=$(expr ${relinkothesEndTime} - ${relinkothesStartTime})
    
    echo "Reflink creation time for files other than in /EXAVMIMAGES/GuestImages :" ${reflinkothesTime}" seconds"
    
    cp /etc/libvirt/qemu/*.xml /EXAVMIMAGES/Backup/XML
    
    for hostName in $(virsh list|egrep -v 'Id|^-'|awk '{print $2}'|sed '/^$/d')
    do
    
    ## Pause the guests
    
      PauseStartTime=$(date +%s)
      virsh suspend ${hostName}
      PauseEndTime=$(date +%s)
      PauseTime=$(expr ${PauseEndTime} - ${PauseStartTime})
      echo "SuspendTime for guest - ${hostName} :" ${PauseTime}" seconds"
    
    ## Create reflinks for all the files in /EXAVMIMAGES/GuestImages
    
      relinkStartTime=$(date +%s)
    
      find /EXAVMIMAGES/GuestImages/${hostName} -type f|awk '{print "cp --reflink", $0,"/EXAVMIMAGES/Backup"$0}'|sh
    
      relinkEndTime=$(date +%s)
      reflinkTime=$(expr ${relinkEndTime} - ${relinkStartTime})
      echo "Reflink creation time for guest - ${hostName} :" ${reflinkTime}" seconds"
    
    ## Unpause the guest
    
      unPauseStartTime=$(date +%s)
      virsh resume ${hostName}
      unPauseEndTime=$(date +%s)
      unPauseTime=$(expr ${unPauseEndTime} - ${unPauseStartTime})
      echo "ResumeTime for guest - ${hostName} :" ${unPauseTime}" seconds"
    
    done
    
    ScriptEndtime=$(date +%s)
    ScriptRunTime=$(expr ${ScriptEndtime} - ${ScriptStarttime})
    echo ScriptRunTime ${ScriptRunTime}" seconds"
  2. Create a backup of the guest images.

    Back up all of the reflink files under /EXAVMIMAGES/Backup to a remote location. The backup enables restoration if the KVM host is permanently damaged or lost.

    For example:

    # mkdir -p /remote_FS
    # mount -t nfs -o rw,intr,soft,proto=tcp,nolock ip_address:/nfs_location/ /remote_FS
    # cd /EXAVMIMAGES/Backup
    # tar --acls --xattrs --xattrs-include=* --format=pax -pjcvf /remote_FS/exavmimages.tar.bz2 * > /tmp/exavmimages_tar.stdout 2> /tmp/exavmimages_tar.stderr

    In the mount command, ip_address is the IP address of the NFS server, and nfs_location is the NFS location holding the backup.

    After the backup completes, check for any significant errors from the tar command. In the previous example, the tar command writes errors to the file at /tmp/exavmimages_tar.stderr.

  3. Remove the /EXAVMIMAGES/Backup directory and its contents.

    For example:

    # cd /
    # rm -rf /EXAVMIMAGES/Backup
  4. Unmount the NFS backup location and remove the mount point directory.

    For example:

    # umount /remote_FS
    # rmdir /remote_FS