Backup and restore by using VM snapshots

You can back up and restore virtual machines (VMs) by using snapshots. Snapshots are supported by the following storage providers:

  • Red Hat OpenShift Data Foundation

  • Any other cloud storage provider with the Container Storage Interface (CSI) driver that supports the Kubernetes Volume Snapshot API

Online snapshots have a default time deadline of five minutes (5m) that can be changed, if needed.

Online snapshots are supported for virtual machines that have hot plugged virtual disks. However, hot plugged disks that are not in the virtual machine specification are not included in the snapshot.

To create snapshots of an online (Running state) VM with the highest integrity, install the QEMU guest agent if it is not included with your operating system. The QEMU guest agent is included with the default Red Hat templates.

The QEMU guest agent takes a consistent snapshot by attempting to quiesce the VM file system as much as possible, depending on the system workload. This ensures that in-flight I/O is written to the disk before the snapshot is taken. If the guest agent is not present, quiescing is not possible and a best-effort snapshot is taken. The conditions under which the snapshot was taken are reflected in the snapshot indications that are displayed in the web console or CLI.

About snapshots

A snapshot represents the state and data of a virtual machine (VM) at a specific point in time. You can use a snapshot to restore an existing VM to a previous state (represented by the snapshot) for backup and disaster recovery or to rapidly roll back to a previous development version.

A VM snapshot is created from a VM that is powered off (Stopped state) or powered on (Running state).

When taking a snapshot of a running VM, the controller checks that the QEMU guest agent is installed and running. If so, it freezes the VM file system before taking the snapshot, and thaws the file system after the snapshot is taken.

The snapshot stores a copy of each Container Storage Interface (CSI) volume attached to the VM and a copy of the VM specification and metadata. Snapshots cannot be changed after creation.

You can perform the following snapshot actions:

  • Create a new snapshot

  • List all snapshots attached to a specific VM

  • Restore a VM from a snapshot

  • Delete an existing VM snapshot

VM snapshot controller and custom resources

The VM snapshot feature introduces three new API objects defined as custom resource definitions (CRDs) for managing snapshots:

  • VirtualMachineSnapshot: Represents a user request to create a snapshot. It contains information about the current state of the VM.

  • VirtualMachineSnapshotContent: Represents a provisioned resource on the cluster (a snapshot). It is created by the VM snapshot controller and contains references to all resources required to restore the VM.

  • VirtualMachineRestore: Represents a user request to restore a VM from a snapshot.

The VM snapshot controller binds a VirtualMachineSnapshotContent object with the VirtualMachineSnapshot object for which it was created, with a one-to-one mapping.

Creating snapshots

You can create snapshots of virtual machines (VMs) by using the OKD web console or the command line.

Creating a snapshot by using the web console

You can create a snapshot of a virtual machine (VM) by using the OKD web console.

The VM snapshot includes disks that meet the following requirements:

  • Either a data volume or a persistent volume claim

  • Belong to a storage class that supports Container Storage Interface (CSI) volume snapshots

Procedure

  1. Navigate to VirtualizationVirtualMachines in the web console.

  2. Select a VM to open the VirtualMachine details page.

  3. If the VM is running, click the options menu kebab and select Stop to power it down.

  4. Click the Snapshots tab and then click Take Snapshot.

  5. Enter the snapshot name.

  6. Expand Disks included in this Snapshot to see the storage volumes to be included in the snapshot.

  7. If your VM has disks that cannot be included in the snapshot and you wish to proceed, select I am aware of this warning and wish to proceed.

  8. Click Save.

Creating a snapshot by using the command line

You can create a virtual machine (VM) snapshot for an offline or online VM by creating a VirtualMachineSnapshot object.

Prerequisites

  • Ensure that the persistent volume claims (PVCs) are in a storage class that supports Container Storage Interface (CSI) volume snapshots.

  • Install the OpenShift CLI (oc).

  • Optional: Power down the VM for which you want to create a snapshot.

Procedure

  1. Create a YAML file to define a VirtualMachineSnapshot object that specifies the name of the new VirtualMachineSnapshot and the name of the source VM as in the following example:

    1. apiVersion: snapshot.kubevirt.io/v1beta1
    2. kind: VirtualMachineSnapshot
    3. metadata:
    4. name: <snapshot_name>
    5. spec:
    6. source:
    7. apiGroup: kubevirt.io
    8. kind: VirtualMachine
    9. name: <vm_name>
  2. Create the VirtualMachineSnapshot object:

    1. $ oc create -f <snapshot_name>.yaml

    The snapshot controller creates a VirtualMachineSnapshotContent object, binds it to the VirtualMachineSnapshot, and updates the status and readyToUse fields of the VirtualMachineSnapshot object.

  3. Optional: If you are taking an online snapshot, you can use the wait command and monitor the status of the snapshot:

    1. Enter the following command:

      1. $ oc wait <vm_name> <snapshot_name> --for condition=Ready
    2. Verify the status of the snapshot:

      • InProgress - The online snapshot operation is still in progress.

      • Succeeded - The online snapshot operation completed successfully.

      • Failed - The online snapshot operaton failed.

        Online snapshots have a default time deadline of five minutes (5m). If the snapshot does not complete successfully in five minutes, the status is set to failed. Afterwards, the file system will be thawed and the VM unfrozen but the status remains failed until you delete the failed snapshot image.

        To change the default time deadline, add the FailureDeadline attribute to the VM snapshot spec with the time designated in minutes (m) or in seconds (s) that you want to specify before the snapshot operation times out.

        To set no deadline, you can specify 0, though this is generally not recommended, as it can result in an unresponsive VM.

        If you do not specify a unit of time such as m or s, the default is seconds (s).

Verification

  1. Verify that the VirtualMachineSnapshot object is created and bound with VirtualMachineSnapshotContent and that the readyToUse flag is set to true:

    1. $ oc describe vmsnapshot <snapshot_name>

    Example output

    1. apiVersion: snapshot.kubevirt.io/v1beta1
    2. kind: VirtualMachineSnapshot
    3. metadata:
    4. creationTimestamp: "2020-09-30T14:41:51Z"
    5. finalizers:
    6. - snapshot.kubevirt.io/vmsnapshot-protection
    7. generation: 5
    8. name: mysnap
    9. namespace: default
    10. resourceVersion: "3897"
    11. selfLink: /apis/snapshot.kubevirt.io/v1beta1/namespaces/default/virtualmachinesnapshots/my-vmsnapshot
    12. uid: 28eedf08-5d6a-42c1-969c-2eda58e2a78d
    13. spec:
    14. source:
    15. apiGroup: kubevirt.io
    16. kind: VirtualMachine
    17. name: my-vm
    18. status:
    19. conditions:
    20. - lastProbeTime: null
    21. lastTransitionTime: "2020-09-30T14:42:03Z"
    22. reason: Operation complete
    23. status: "False" (1)
    24. type: Progressing
    25. - lastProbeTime: null
    26. lastTransitionTime: "2020-09-30T14:42:03Z"
    27. reason: Operation complete
    28. status: "True" (2)
    29. type: Ready
    30. creationTime: "2020-09-30T14:42:03Z"
    31. readyToUse: true (3)
    32. sourceUID: 355897f3-73a0-4ec4-83d3-3c2df9486f4f
    33. virtualMachineSnapshotContentName: vmsnapshot-content-28eedf08-5d6a-42c1-969c-2eda58e2a78d (4)
    1The status field of the Progressing condition specifies if the snapshot is still being created.
    2The status field of the Ready condition specifies if the snapshot creation process is complete.
    3Specifies if the snapshot is ready to be used.
    4Specifies that the snapshot is bound to a VirtualMachineSnapshotContent object created by the snapshot controller.
  2. Check the spec:volumeBackups property of the VirtualMachineSnapshotContent resource to verify that the expected PVCs are included in the snapshot.

Verifying online snapshots by using snapshot indications

Snapshot indications are contextual information about online virtual machine (VM) snapshot operations. Indications are not available for offline virtual machine (VM) snapshot operations. Indications are helpful in describing details about the online snapshot creation.

Prerequisites

  • You must have attempted to create an online VM snapshot.

Procedure

  1. Display the output from the snapshot indications by doing one of the following:

    • For snapshots created by using the command line, view indicator output in the status stanza of the VirtualMachineSnapshot object YAML.

    • For snapshots created by using the web console, click VirtualMachineSnapshotStatus in the Snapshot details screen.

  2. Verify the status of your online VM snapshot:

    • Online indicates that the VM was running during online snapshot creation.

    • NoGuestAgent indicates that the QEMU guest agent was not running during online snapshot creation. The QEMU guest agent could not be used to freeze and thaw the file system, either because the QEMU guest agent was not installed or running or due to another error.

Restoring virtual machines from snapshots

You can restore virtual machines (VMs) from snapshots by using the OKD web console or the command line.

Restoring a VM from a snapshot by using the web console

You can restore a virtual machine (VM) to a previous configuration represented by a snapshot in the OKD web console.

Procedure

  1. Navigate to VirtualizationVirtualMachines in the web console.

  2. Select a VM to open the VirtualMachine details page.

  3. If the VM is running, click the options menu kebab and select Stop to power it down.

  4. Click the Snapshots tab to view a list of snapshots associated with the VM.

  5. Select a snapshot to open the Snapshot Details screen.

  6. Click the options menu kebab and select Restore VirtualMachineSnapshot.

  7. Click Restore.

Restoring a VM from a snapshot by using the command line

You can restore an existing virtual machine (VM) to a previous configuration by using the command line. You can only restore from an offline VM snapshot.

Prerequisites

  • Power down the VM you want to restore.

Procedure

  1. Create a YAML file to define a VirtualMachineRestore object that specifies the name of the VM you want to restore and the name of the snapshot to be used as the source as in the following example:

    1. apiVersion: snapshot.kubevirt.io/v1beta1
    2. kind: VirtualMachineRestore
    3. metadata:
    4. name: <vm_restore>
    5. spec:
    6. target:
    7. apiGroup: kubevirt.io
    8. kind: VirtualMachine
    9. name: <vm_name>
    10. virtualMachineSnapshotName: <snapshot_name>
  2. Create the VirtualMachineRestore object:

    1. $ oc create -f <vm_restore>.yaml

    The snapshot controller updates the status fields of the VirtualMachineRestore object and replaces the existing VM configuration with the snapshot content.

Verification

  • Verify that the VM is restored to the previous state represented by the snapshot and that the complete flag is set to true:

    1. $ oc get vmrestore <vm_restore>

    Example output

    1. apiVersion: snapshot.kubevirt.io/v1beta1
    2. kind: VirtualMachineRestore
    3. metadata:
    4. creationTimestamp: "2020-09-30T14:46:27Z"
    5. generation: 5
    6. name: my-vmrestore
    7. namespace: default
    8. ownerReferences:
    9. - apiVersion: kubevirt.io/v1
    10. blockOwnerDeletion: true
    11. controller: true
    12. kind: VirtualMachine
    13. name: my-vm
    14. uid: 355897f3-73a0-4ec4-83d3-3c2df9486f4f
    15. resourceVersion: "5512"
    16. selfLink: /apis/snapshot.kubevirt.io/v1beta1/namespaces/default/virtualmachinerestores/my-vmrestore
    17. uid: 71c679a8-136e-46b0-b9b5-f57175a6a041
    18. spec:
    19. target:
    20. apiGroup: kubevirt.io
    21. kind: VirtualMachine
    22. name: my-vm
    23. virtualMachineSnapshotName: my-vmsnapshot
    24. status:
    25. complete: true (1)
    26. conditions:
    27. - lastProbeTime: null
    28. lastTransitionTime: "2020-09-30T14:46:28Z"
    29. reason: Operation complete
    30. status: "False" (2)
    31. type: Progressing
    32. - lastProbeTime: null
    33. lastTransitionTime: "2020-09-30T14:46:28Z"
    34. reason: Operation complete
    35. status: "True" (3)
    36. type: Ready
    37. deletedDataVolumes:
    38. - test-dv1
    39. restoreTime: "2020-09-30T14:46:28Z"
    40. restores:
    41. - dataVolumeName: restore-71c679a8-136e-46b0-b9b5-f57175a6a041-datavolumedisk1
    42. persistentVolumeClaim: restore-71c679a8-136e-46b0-b9b5-f57175a6a041-datavolumedisk1
    43. volumeName: datavolumedisk1
    44. volumeSnapshotName: vmsnapshot-28eedf08-5d6a-42c1-969c-2eda58e2a78d-volume-datavolumedisk1
    1Specifies if the process of restoring the VM to the state represented by the snapshot is complete.
    2The status field of the Progressing condition specifies if the VM is still being restored.
    3The status field of the Ready condition specifies if the VM restoration process is complete.

Deleting snapshots

You can delete snapshots of virtual machines (VMs) by using the OKD web console or the command line.

Deleting a snapshot by using the web console

You can delete an existing virtual machine (VM) snapshot by using the web console.

Procedure

  1. Navigate to VirtualizationVirtualMachines in the web console.

  2. Select a VM to open the VirtualMachine details page.

  3. Click the Snapshots tab to view a list of snapshots associated with the VM.

  4. Click the options menu kebab beside a snapshot and select Delete VirtualMachineSnapshot.

  5. Click Delete.

Deleting a virtual machine snapshot in the CLI

You can delete an existing virtual machine (VM) snapshot by deleting the appropriate VirtualMachineSnapshot object.

Prerequisites

  • Install the OpenShift CLI (oc).

Procedure

  • Delete the VirtualMachineSnapshot object:

    1. $ oc delete vmsnapshot <snapshot_name>

    The snapshot controller deletes the VirtualMachineSnapshot along with the associated VirtualMachineSnapshotContent object.

Verification

  • Verify that the snapshot is deleted and no longer attached to this VM:

    1. $ oc get vmsnapshot

Additional resources