The document describes the process to create fine-grained restores that use filters
to target specific resources in virtual machine (VM) and VM disk backups. You can
create a restore for a backup created manually or automatically by a backup plan.
The restored VMs and their associated disks have their name prefixed with the
value of the `restoredResourceName` field.

VM restores support fine-grained resource selection using filters. Use a filter
to restore a subset of resources from the previously created
`VirtualMachineBackup` resource. This filter is an optional field. If the filter
is not provided then the complete set of resources in the `VirtualMachineBackup`
resource are restored.

This document is for developers in platform administrator or application operator
groups that manage VM backup and restore operations. For more information, see
[Audiences for GDC air-gapped documentation](https://docs.cloud.google.com/distributed-cloud/hosted/docs/latest/gdcag/resources/audiences.md.txt).

## Before you begin

To create a restore, ensure you have an existing backup available. You can
[create backups on a schedule](https://docs.cloud.google.com/distributed-cloud/hosted/docs/latest/gdcag/platform-application/pa-ao-operations/vm-backup/backup-plans/create-scheduled-backup-plan.md.txt)
using backup plans, or create backups manually.

### Request IAM roles

To create a fine-grained restore, ask your IAM Org Admin to grant you the
**Organization Backup Admin** (`organization-backup-admin`) role. This role lets you
create, get, list, watch, update, patch, and delete VM backup repositories, VM backup plan
templates, VM backup requests, and VM restore requests.

## Create a fine-grained restore

To create a fine-grained restore for a previously created backup, follow these steps:

1. Get a list of VMs and VM disks that were backed up in your target `VirtualMachineBackup` resource:

   1. Get the list of VMs:

      ```bash
      kubectl get virtualmachinebackups.virtualmachine.gdc.goog VM_BACKUP_NAME -n
      VM_BACKUP_NAMESPACE -o jsonpath=
      "{.status.backedUpVirtualMachines}{'\n'}"
      ```

      Replace the following:
      - `VM_BACKUP_NAME`: the name of the backup containing the VM resources you want to restore.
      - `VM_BACKUP_NAMESPACE`: the namespace containing the backup.
   2. Get the list of VM disks:

   ```bash
   kubectl get virtualmachinebackups.virtualmachine.gdc.goog VM_BACKUP_NAME -n
   VM_BACKUP_NAMESPACE -o
   jsonpath="{.status.backedUpVirtualMachineDisks}{'\n'}"
   ```
2. Use the values from this list to specify the target for your fine-grained
   restore. Create one of the following fine-grained restores:

   - [Restore specific VMs](https://docs.cloud.google.com/distributed-cloud/hosted/docs/latest/gdcag/platform-application/pa-ao-operations/vm-backup/restore-backup/create-fine-grained-restore.md.txt#restore-specific-vms)
   - [Restore specific VM disks](https://docs.cloud.google.com/distributed-cloud/hosted/docs/latest/gdcag/platform-application/pa-ao-operations/vm-backup/restore-backup/create-fine-grained-restore.md.txt#restore-specific-vm-disks)
   - [Restore specific VMs and VM disks](https://docs.cloud.google.com/distributed-cloud/hosted/docs/latest/gdcag/platform-application/pa-ao-operations/vm-backup/restore-backup/create-fine-grained-restore.md.txt#restore-both-vm-and-disk)
   - [Restore all VM disks](https://docs.cloud.google.com/distributed-cloud/hosted/docs/latest/gdcag/platform-application/pa-ao-operations/vm-backup/restore-backup/create-fine-grained-restore.md.txt#restore-all-vm-disks)

### Restore specific VMs

- Use the `targetedVirtualMachines` filter to target a list of VMs with this
  restore. This option restores the provided VMs and their associated disks.
  Create and apply a `VirtualMachineRestoreRequest` resource and supply your list of
  VMs to restore in the `targetedVirtualMachines` field:

  ```yaml
  apiVersion: virtualmachine.gdc.goog/v1
  kind: VirtualMachineRestoreRequest
  metadata:
    name: RESTORE_REQUEST_NAME
    namespace: PROJECT_NAME
  spec:
    virtualMachineBackup: VM_BACKUP_NAME
    restoreName: RESTORE_NAME
    restoredResourceName: RESTORE_PREFIX
    restoredResourceDescription: RESTORED_RESOURCE_DESCRIPTION
  filter:
      targetedVirtualMachines:
      - resourceName: VM_NAME
  ```

  Replace the following:
  - `VM_BACKUP_NAME`: the name of the `VirtualMachineBackup` resource to restore from.
  - `PROJECT_NAME`: the name of the GDC project.
  - `RESTORE_REQUEST_NAME`: the name to give the restore request that is being initiated.
  - `RESTORE_NAME`: the name to give the restore that is being initiated.
  - `RESTORE_PREFIX`: the prefix given to newly created VM resources. The restored VM and VM disk names are prefixed with `restoredResourceName` value in the restore request.
  - `RESTORED_RESOURCE_DESCRIPTION`: a description of the restore being performed.
  - `VM_NAME`: A list of VMs that is restored by this
    `VirtualMachineRestoreRequest` resource. If more than one VM is specified, the
    list must be in the following format:

          targetedVirtualMachines:
              - resourceName: "virtual-machine"
              - resourceName: "virtual-machine-2"
              ...
              - resourceName: "virtual-machine-n"
          ```

### Restore specific VM disks

- Use the `targetedVirtualMachineDisks` filter to target a set of VM disks to restore.
  This option restores any disks that were backed up, and these disks don't have
  to be attached to the same VM. This option restores only the disks and not the
  VMs to which the disks were attached. Create and apply a
  `VirtualMachineRestoreRequest` resource and supply the list of VM disks to
  restore in the `targetedVirtualMachineDisks` field:

  ```yaml
  apiVersion: virtualmachine.gdc.goog/v1
  kind: VirtualMachineRestoreRequest
  metadata:
    name: RESTORE_REQUEST_NAME
    namespace: PROJECT_NAME
  spec:
    virtualMachineBackup: VM_BACKUP_NAME
    restoreName: RESTORE_NAME
    restoredResourceName: RESTORE_PREFIX
    restoredResourceDescription: RESTORED_RESOURCE_DESCRIPTION
    filter:
      targetedVirtualMachineDisks:
      - VM_DISK_NAME
  ```

  Replace `VM_DISK_NAME` with the list of VM disks that is
  restored by this `VirtualMachineRestoreRequest` resource. If more than one VM
  disk is specified, the list must be in the following format:

      targetedVirtualMachineDisks:
          - resourceName: "disk-1"
          - resourceName: "disk-2"
          ...
          - resourceName: "disk-n"

### Restore specific VMs and VM disks

- To target combination of VMs and VM disks, use the `targetedVirtualMachines`
  and `targetedVirtualMachineDisks` filters together, this method restore the
  VMs specified in the `targetedVirtualMachines` filter along with their
  attached disks, and the VM disks mentioned in `targetedVirtualMachineDisks`
  filter. If the disk is already attached to the VM specified in the
  `targetedVirtualMachineDisks` list. In the `targetedVirtualMachineDisks`
  filter, specify only the disks that are not already included as part of the
  `targetedVirtualMachines`.

  ```yaml
  apiVersion: virtualmachine.gdc.goog/v1
  kind: VirtualMachineRestoreRequest
  metadata:
    name: RESTORE_REQUEST_NAME
    namespace: PROJECT_NAME
  spec:
    virtualMachineBackup: VM_BACKUP_NAME
    restoreName: RESTORE_NAME
    restoredResourceName: RESTORE_PREFIX
    restoredResourceDescription: RESTORED_RESOURCE_DESCRIPTION
    filter:
      targetedVirtualMachines:
      - resourceName: VM_NAME
      targetedVirtualMachineDisks:
      - resourceName: VM_DISK_NAME
  ```

  Replace the following:
  - `VM_NAME`:The list of VMs that is restored by this
    `VirtualMachineRestoreRequest` resource. If more than one VM is specified, the
    list must be in the following format:

              targetedVirtualMachines:
                  - resourceName: "virtual-machine"
                  - resourceName: "virtual-machine-2"
                  ...
                  - resourceName: "virtual-machine-n"
          ```

  - `VM_DISK_NAME`: the list of
    VM disks that is restored by this `VirtualMachineRestoreRequest` resource. If more
    than one VM disk is specified, the list must be in the following format:

        targetedVirtualMachineDisks:
            - resourceName: "disk-1"
            - resourceName: "disk-2"
            ...
            - resourceName: "disk-n"

### Restore all VM disks

- Set the filter value `virtualMachineDiskOptions.allDisks` to true to restore
  all of the disks that were protected as part of `VirtualMachineBackup`. This
  action won't restore the VMs to which the disks were attached:

  ```yaml
  apiVersion: virtualmachine.gdc.goog/v1
  kind: VirtualMachineRestoreRequest
  metadata:
    name: RESTORE_REQUEST_NAME
    namespace: PROJECT_NAME
  spec:
    virtualMachineBackup: VM_BACKUP_NAME
    restoreName: RESTORE_NAME
    restoredResourceName: RESTORE_PREFIX
    restoredResourceDescription: RESTORED_RESOURCE_DESCRIPTION
    filter:
      virtualMachineDiskOptions:
          allDisks: true
  ```

## What's next

- [Clean up labels after restoring VM-attached disks](https://docs.cloud.google.com/distributed-cloud/hosted/docs/latest/gdcag/platform-application/pa-ao-operations/vm-backup/clean-up-labels-after-restore.md.txt)