6.10.2.2 Method 2: Back Up an Individual Guest

You can back up an individual guest by backing up its specific folder 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 an individual guest backup is approximately 200 GB.

  1. Prepare the guest image.

    Use the following script to prepare the guest image backup 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
    
    printf "Enter the name of the KVM guest to be backed up :"
    
    read KVMGuestName
    
    ## Create the Backup Directory
    
    if [ ! -d /EXAVMIMAGES/GuestImages/${KVMGuestName} ]
    then
      echo "Guest ${KVMGuestName} does not exist"
      exit 1
    fi
    
    mkdirStartTime=$(date +%s)
    
    find /EXAVMIMAGES/GuestImages/${KVMGuestName} -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"
    
    cp /etc/libvirt/qemu/${KVMGuestName}.xml /EXAVMIMAGES/Backup/XML
    
    ## Pause the guest
    
    PauseStartTime=$(date +%s)
    virsh suspend ${KVMGuestName}
    PauseEndTime=$(date +%s)
    PauseTime=$(expr ${PauseEndTime} - ${PauseStartTime})
    echo "PauseTime for guest - ${KVMGuestName} :" ${PauseTime}" seconds"
    
    ## Create reflinks for all the files in /EXAVMIMAGES/GuestImages/${KVMGuestName}
    
    relinkStartTime=$(date +%s)
    
    find /EXAVMIMAGES/GuestImages/${KVMGuestName} -type f|awk '{print "cp --reflink", $0,"/EXAVMIMAGES/Backup"$0}'|sh
    
    relinkEndTime=$(date +%s)
    reflinkTime=$(expr ${relinkEndTime} - ${relinkStartTime})
    echo "Reflink creation time for guest - ${KVMGuestName} :" ${reflinkTime}" seconds"
    
    ## Unpause the guest
    
    unPauseStartTime=$(date +%s)
    virsh resume ${KVMGuestName}
    unPauseEndTime=$(date +%s)
    unPauseTime=$(expr ${unPauseEndTime} - ${unPauseStartTime})
    echo "unPauseTime for guest - ${KVMGuestName} :" ${unPauseTime}" seconds"
    
    ScriptEndtime=$(date +%s)
    ScriptRunTime=$(expr ${ScriptEndtime} - ${ScriptStarttime})
    echo ScriptRunTime ${ScriptRunTime}" seconds"
  2. Create a backup of the guest image.

    Back up the reflink files under /EXAVMIMAGES/Backup to a remote location. The backup enables restoration of the specific guest 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/exavmimage.tar.bz2 * > /tmp/exavmimage_tar.stdout 2> /tmp/exavmimage_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.

    In the example, the backup file is named exavmimage.tar.bz2. You may choose another name that identifies the guest being backed up.

    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/exavmimage_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