This tutorial shows you how to use auto-protection policies and resource labels
to automatically protect Compute Engine instances and disks at scale
in Backup and DR.

Instead of manually selecting individual resources to protect, auto-protection
policies evaluate user-defined resource labels to identify qualifying resources
across projects and automatically assign a specified backup plan to them.

## Objectives

- Configure required Identity and Access Management roles and permissions.
- Prepare your environment with resource labels.
- Create an auto-protection policy.
- View auto-protection policies and protected resources.
- View applied auto-protection policies in a workload project.
- Edit an auto-protection policy.
- Update resource projects associated with an auto-protection policy.
- Delete an auto-protection policy.

## Costs

This tutorial uses the following billable components of Google Cloud:

- Backup and DR
- Compute Engine instances and disks

To generate a cost estimate based on your projected usage, use the
[pricing calculator](https://cloud.google.com/products/calculator).

## Before you begin

1. Select or create a Google Cloud project for your backup vault
   resources and workload resources:

   - **Backup vault project**: The project that contains your
     backup vaults, backup plans, and auto-protection policies.

   - **Workload project**: The project that contains the Compute Engine
     instances or disks to protect.

   [Go to Project Selector](https://console.cloud.google.com/projectselector2/home/dashboard)
2. Enable billing for your projects.

3. Enable the Backup and DR API and Compute Engine API in both your backup
   vault project and workload projects:

   [Enable the APIs](https://console.cloud.google.com/flows/enableapi?apiid=backupdr.googleapis.com,compute.googleapis.com)
4. [Create a backup vault](https://docs.cloud.google.com/backup-disaster-recovery/docs/cloud-console/backup-vault-create.md.txt)
   in your backup vault project to store your backups.

5. [Create a backup plan](https://docs.cloud.google.com/backup-disaster-recovery/docs/cloud-console/backup-plan-create.md.txt)
   in your backup vault project for the resource type (Compute Engine
   instances or disks) and region that you want to protect.

6. Ensure that you have the [required IAM roles and permissions](https://docs.cloud.google.com/backup-disaster-recovery/docs/protect/automate-protection.md.txt#required-roles).

7. Optional: If your backup vault project or workload project is protected by a
   [VPC Service Controls](https://docs.cloud.google.com/vpc-service-controls/docs/overview.md.txt) service perimeter,
   [configure VPC Service Controls for auto-protection policies](https://docs.cloud.google.com/backup-disaster-recovery/docs/protect/vpc-sc.md.txt).

### Required roles and permissions

To get the permissions that you need to create, manage, and view auto-protection
policies and policy bindings, ask your administrator to grant you the following
IAM roles on the specified projects:

- **Backup vault project:**
  - To create, edit, and delete auto-protection policies and policy bindings: **Backup and DR Admin** (`roles/backupdr.admin`) or **Backupdr Editor** (`roles/backupdr.editor`)
  - To view auto-protection policies, policy bindings, and matching resources: **Backup and DR Viewer** (`roles/backupdr.viewer`)
- **Workload projects:**
  - To authorize auto-protection policies to be applied to the project and manage bindings: **Backup and DR Admin** (`roles/backupdr.admin`) or **Backupdr Editor** (`roles/backupdr.editor`)
  - To view applied auto-protection policies and matching resources: **Backup and DR Viewer** (`roles/backupdr.viewer`)

#### Service agent roles for cross-project protection

If your workload resources (Compute Engine instances or disks) reside in a
different project from your backup vault project, you must grant the
appropriate operator role to the backup vault service agent in each target
workload project:

- For Compute Engine instances: **Backup and DR Compute Engine Operator** (`roles/backupdr.computeEngineOperator`)
- For disks: **Backup and DR Disk Operator** (`roles/backupdr.diskOperator`)

These roles grant the backup vault service agent the necessary permissions to
perform backup operations on workload resources in the workload
project. For more information, see
[Grant a role to the service agent](https://docs.cloud.google.com/backup-disaster-recovery/docs/cloud-console/compute/compute-instance-backup.md.txt#backup-agent).

**Note:** In both same-project and cross-project configurations, the workload
project's Backup and DR service agent is automatically granted the
**Backup and DR Service Agent** (`roles/backupdr.serviceAgent`) role to manage
backup plan associations within the project.

#### Granular permissions for custom roles

If you prefer to use custom roles instead of predefined roles, ensure that your
custom roles include the granular permissions listed in the following table:

| Action or operation | Granular permissions | Project where permission is required |
|---|---|---|
| **Create auto-protection policy** | `backupdr.autoProtectionPolicies.create` `backupdr.backupPlans.useForComputeInstance` (for VM instances) or `backupdr.backupPlans.useForComputeDisk` (for disks) | Backup vault project |
| **Update auto-protection policy** | `backupdr.autoProtectionPolicies.update` `backupdr.backupPlans.useForComputeInstance` (for VM instances) or `backupdr.backupPlans.useForComputeDisk` (for disks), required when updating the `criteria` or `backup_plan_details` fields | Backup vault project |
| **Delete auto-protection policy** | `backupdr.autoProtectionPolicies.delete` | Backup vault project |
| **View auto-protection policies** | `backupdr.autoProtectionPolicies.get` `backupdr.autoProtectionPolicies.list` | Backup vault project |
| **Create policy binding** (Add project to policy) | `backupdr.autoProtectionBindings.create` `backupdr.appliedAutoProtectionPolicies.authorize` | Backup vault project Workload project |
| **Delete policy binding** (Remove project from policy) | `backupdr.autoProtectionBindings.delete` | Backup vault project |
| **View policy bindings** | `backupdr.autoProtectionBindings.get` `backupdr.autoProtectionBindings.list` | Backup vault project |
| **View matching resources** | `backupdr.bindingMatchingResources.list` `backupdr.appliedAutoProtectionPolicies.viewMatchingResources` | Backup vault project Workload project |
| **View applied policies** | `backupdr.appliedAutoProtectionPolicies.list` | Workload project |
| **Initialize service agent** (For VPC Service Controls perimeters) | `backupdr.serviceConfig.initialize` | Workload project |

### Limitations

Keep the following limitations in mind:

- **Supported resources**: Auto-protection policies support Compute Engine
  instances and disks only.

- **Scale limits**: By default, you can create up to 10 auto-protection
  policies per project per region, and add up to 50 workload projects to each
  policy. Auto-protection evaluates up to 1,000 matching resources per workload
  project in each reconciliation. Resources beyond this limit are reported as
  failed to protect.

- **Label key and value constraints** : Within a workload project, all
  auto-protection policies for the same resource type must use the same label
  key, and each policy must use a different label value (for example,
  `env=prod` and `env=test`). Policies for different resource types (for
  example, a Compute Engine instance policy that uses `env=prod` and a disk
  policy that uses `tier=gold`) can use different label keys.

- **Single backup vault project per workload project**: A workload project can
  be added only to auto-protection policies that are in the same backup vault
  project for a given region.

- **Organization requirement**: For cross-project protection, the backup vault
  project and each workload project must belong to the same Google Cloud
  organization. If a project isn't part of an organization, you can add that
  project only to an auto-protection policy in the same project.

- **Backup vault access restrictions** : For cross-project protection,
  the backup vault used by the policy's backup plan can't have its
  [access restriction](https://docs.cloud.google.com/backup-disaster-recovery/docs/concepts/backup-vault.md.txt#access-restrictions)
  set to `WITHIN_PROJECT` (`within-project`). Use an access restriction that
  allows cross-project access within your organization, such as
  `WITHIN_ORGANIZATION` (`within-org`) or `UNRESTRICTED` (`unrestricted`).

- **Existing backup plan associations**: When a resource that already has a
  manually created backup plan association matches an auto-protection policy:

  - If the existing backup plan uses the same backup vault as the policy's backup plan, auto-protection takes over the association and updates it to the policy's backup plan if needed.
  - If the existing backup plan uses a different backup vault, auto-protection deletes the existing association and replaces it with a new association that uses the policy's backup plan. Before you change a policy to a backup plan in a different backup vault, grant that vault's service agent the required operator role in each workload project. Otherwise, the resources lose protection.
  - You can't update or delete associations managed by auto-protection directly, and you can't manually create an association with a different backup plan for a resource that matches a policy. On-demand backups are still supported.
- **Management interfaces** : You can manage auto-protection policies and policy
  bindings by using the Google Cloud console, the [gcloud CLI](https://docs.cloud.google.com/sdk/gcloud.md.txt), the REST API, and
  Terraform. For Terraform configuration details, see the Terraform provider
  documentation for the
  [`google_backup_dr_auto_protection_policy` resource](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/backup_dr_auto_protection_policy)
  and the
  [`google_backup_dr_auto_protection_policy_binding` resource](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/backup_dr_auto_protection_policy_binding).

- **Single-region support**: An auto-protection policy protects resources
  located in a single region. To protect resources across multiple regions,
  create separate policies for each region.

- **Supported regions** : Auto-protection policies are available in all
  [Backup and DR regions](https://docs.cloud.google.com/backup-disaster-recovery/docs/concepts/backup-vault.md.txt#regions)
  except `northamerica-northeast1`.

## Prepare your environment

Before creating an auto-protection policy, apply resource labels
(key-value pairs) to the Compute Engine resources you want to protect.

### Console

1. In the Google Cloud console, go to the **VM instances** page or **Disks** page.

   [Go to VM instances](https://console.cloud.google.com/compute/instances)

   [Go to Disks](https://console.cloud.google.com/compute/disks)
2. Select the resource you want to label.

3. Add a label key and value (for example, key `env` and value `prod`).

4. Click **Save**.

### gcloud

To label a Compute Engine instance:

```
gcloud compute instances add-labels INSTANCE_NAME \
    --labels=KEY=VALUE \
    --zone=ZONE \
    --project=WORKLOAD_PROJECT_ID
```

To label a disk:

```
gcloud compute disks add-labels DISK_NAME \
    --labels=KEY=VALUE \
    --zone=ZONE \
    --project=WORKLOAD_PROJECT_ID
```

Replace the following:

- `INSTANCE_NAME`: the name of the VM instance.
- `DISK_NAME`: the name of the disk.
- `KEY`: the label key.
- `VALUE`: the label value.
- `ZONE`: the zone where the resource resides. For regional disks, use `--region` instead of `--zone`.
- `WORKLOAD_PROJECT_ID`: the project ID of the workload project where the resource resides.

### REST

To label a Compute Engine instance, send a `POST` request to the
[`instances.setLabels`](https://docs.cloud.google.com/compute/docs/reference/rest/v1/instances/setLabels.md.txt)
method:

```
curl -X POST \
    -H "Authorization: Bearer $(gcloud auth print-access-token)" \
    -H "Content-Type: application/json" \
    -d '{
      "labels": {
        "KEY": "VALUE"
      },
      "labelFingerprint": "LABEL_FINGERPRINT"
    }' \
    "https://compute.googleapis.com/compute/v1/projects/WORKLOAD_PROJECT_ID/zones/ZONE/instances/INSTANCE_NAME/setLabels"
```

To label a disk, send a `POST` request to the
[`disks.setLabels`](https://docs.cloud.google.com/compute/docs/reference/rest/v1/disks/setLabels.md.txt)
method:

```
curl -X POST \
    -H "Authorization: Bearer $(gcloud auth print-access-token)" \
    -H "Content-Type: application/json" \
    -d '{
      "labels": {
        "KEY": "VALUE"
      },
      "labelFingerprint": "LABEL_FINGERPRINT"
    }' \
    "https://compute.googleapis.com/compute/v1/projects/WORKLOAD_PROJECT_ID/zones/ZONE/disks/DISK_NAME/setLabels"
```

Replace the following:

- `WORKLOAD_PROJECT_ID`: the project ID of the workload project where the resource resides.
- `ZONE`: the zone where the resource resides. For regional disks, use the [`regionDisks.setLabels`](https://docs.cloud.google.com/compute/docs/reference/rest/v1/regionDisks/setLabels.md.txt) method with `regions/REGION` instead of `zones/ZONE`.
- `INSTANCE_NAME`: the name of the VM instance.
- `DISK_NAME`: the name of the disk.
- `KEY`: the label key.
- `VALUE`: the label value.
- `LABEL_FINGERPRINT`: the current `labelFingerprint` of the resource. To retrieve the fingerprint, send a `GET` request to the resource before updating its labels.

## Create an auto-protection policy

To create an auto-protection policy that automatically assigns a backup plan to
labeled resources:

### Console

1. In the Google Cloud console, go to the **Backup and DR** page.

   [Go to Backup and DR](https://console.cloud.google.com/backupdr)
2. In the navigation menu, select **Auto-protection policies**.

3. Click **Create Auto-protection policy**.

4. In the **Name your auto-protection policy** section enter the following details:

   1. In the **Auto-protection policy name** field, enter a name for the policy.

   2. In the **Policy description** field, enter an optional description.

   3. Click **Continue**.

5. In the **Choose the region where your resources are located and select a backup plan**
   section, enter the following details:

   1. From the **Region** drop-down list, select the region where your resources are located.

   2. In the **This policy is for** section:

      1. Select the resource type to protect, either Compute Engine
         instances or disks.

      2. In the **Backup plan** field, click **Select** to choose the backup
         plan to apply to qualifying resources.

      3. From the **Select a backup plan** pane, select the backup plan you
         want to associate with the auto-protection policy.

      4. Click **Select**.

   3. Click **Continue**.

6. In the **Choose projects where your resources are** section, select one or
   multiple projects in the same organization to protect.

7. In the **Choose resources to protect** section, enter key-value pairs
   associated with the resources you want to protect. For example, `env` = `prod`.

8. Click **Create policy**.

### gcloud

1. Create an auto-protection policy.

   ```
   gcloud backup-dr auto-protection-policies create POLICY_NAME \
       --location=LOCATION \
       --project=BACKUP_VAULT_PROJECT \
       --criteria=key=LABEL_KEY,values=LABEL_VALUE \
       --backup-plan-details="resource-type=RESOURCE_TYPE,backup-plan=projects/BACKUP_VAULT_PROJECT/locations/LOCATION/backupPlans/BACKUP_PLAN" \
       --description="DESCRIPTION" \
       --no-async
   ```

   Replace the following:
   - `POLICY_NAME`: the name of the auto-protection policy.
   - `LOCATION`: the region where the policy and resources reside.
   - `BACKUP_VAULT_PROJECT`: the project ID where the backup vault and backup plan reside.
   - `LABEL_KEY`: the label key to use for matching resources.
   - `LABEL_VALUE`: the label value to use for matching resources.
   - `RESOURCE_TYPE`: the resource type to protect. Use `compute.googleapis.com/Instance` for Compute Engine instances, or `compute.googleapis.com/Disk` for zonal and regional persistent disks. Don't use `compute.googleapis.com/RegionDisk`.
   - `BACKUP_PLAN`: the name of the backup plan to assign to matching resources.
   - `DESCRIPTION`: an optional description for the policy.
2. Add workload projects to the policy by creating a policy binding.

   ```
   gcloud backup-dr auto-protection-bindings create BINDING_NAME \
       --auto-protection-policy=POLICY_NAME \
       --location=LOCATION \
       --project=BACKUP_VAULT_PROJECT \
       --scope=projects/WORKLOAD_PROJECT_ID
   ```

   Replace the following:
   - `BINDING_NAME`: a name for the policy binding.
   - `POLICY_NAME`: the name of the auto-protection policy.
   - `LOCATION`: the region where the policy resides.
   - `BACKUP_VAULT_PROJECT`: the project ID where the auto-protection policy resides.
   - `WORKLOAD_PROJECT_ID`: the project ID of the workload project to protect.

   Repeat this step for each workload project you want to add to the policy.

### REST

1. Create an auto-protection policy by sending a `POST` request to the
   [`projects.locations.autoProtectionPolicies.create`](https://docs.cloud.google.com/backup-disaster-recovery/docs/reference/rest/v1/projects.locations.autoProtectionPolicies/create.md.txt)
   method:

   ```
   curl -X POST \
       -H "Authorization: Bearer $(gcloud auth print-access-token)" \
       -H "Content-Type: application/json" \
       -d '{
         "description": "DESCRIPTION",
         "backupPlanDetails": [
           {
             "resourceType": "RESOURCE_TYPE",
             "backupPlan": "projects/BACKUP_VAULT_PROJECT/locations/LOCATION/backupPlans/BACKUP_PLAN"
           }
         ],
         "criteria": {
           "matchingConditions": [
             {
               "labelCondition": {
                 "key": "LABEL_KEY",
                 "values": [
                   "LABEL_VALUE"
                 ]
               }
             }
           ]
         }
       }' \
       "https://backupdr.googleapis.com/v1/projects/BACKUP_VAULT_PROJECT/locations/LOCATION/autoProtectionPolicies?autoProtectionPolicyId=POLICY_NAME"
   ```

   Replace the following:
   - `BACKUP_VAULT_PROJECT`: the project ID where the backup vault and backup plan reside.
   - `LOCATION`: the region where the policy and resources reside.
   - `POLICY_NAME`: the name of the auto-protection policy.
   - `DESCRIPTION`: an optional description for the policy.
   - `RESOURCE_TYPE`: the resource type to protect. Use `compute.googleapis.com/Instance` for Compute Engine instances, or `compute.googleapis.com/Disk` for zonal and regional persistent disks. Don't use `compute.googleapis.com/RegionDisk`.
   - `BACKUP_PLAN`: the name of the backup plan to assign to matching resources.
   - `LABEL_KEY`: the label key to use for matching resources.
   - `LABEL_VALUE`: the label value to use for matching resources.
2. Add workload projects to the policy by sending a `POST` request to the
   [`projects.locations.autoProtectionPolicies.bindings.create`](https://docs.cloud.google.com/backup-disaster-recovery/docs/reference/rest/v1/projects.locations.autoProtectionPolicies.bindings/create.md.txt)
   method:

   ```
   curl -X POST \
       -H "Authorization: Bearer $(gcloud auth print-access-token)" \
       -H "Content-Type: application/json" \
       -d '{
         "scope": "projects/WORKLOAD_PROJECT_ID"
       }' \
       "https://backupdr.googleapis.com/v1/projects/BACKUP_VAULT_PROJECT/locations/LOCATION/autoProtectionPolicies/POLICY_NAME/bindings?autoProtectionPolicyBindingId=BINDING_NAME"
   ```

   Replace the following:
   - `BACKUP_VAULT_PROJECT`: the project ID where the auto-protection policy resides.
   - `LOCATION`: the region where the policy resides.
   - `POLICY_NAME`: the name of the auto-protection policy.
   - `BINDING_NAME`: a name for the policy binding.
   - `WORKLOAD_PROJECT_ID`: the project ID of the workload project to protect.

   Repeat this step for each workload project you want to add to the policy.

Once created, the policy automatically identifies matching resources and
schedules backup plan associations. Initial configuration and protection
scheduling typically takes up to 2 hours, but in some cases can take up to
8 hours.

## View auto-protection policies

As a backup administrator, you can view auto-protection policies created in your
backup vault project and monitor the workload projects and resources
they protect.

To view auto-protection policies and their protected resources:

### Console

1. In the Google Cloud console, select your backup vault project.

2. Go to the **Backup and DR** page.

   [Go to Backup and DR](https://console.cloud.google.com/backupdr)
3. In the navigation menu, select **Auto-protection policies**.

4. On the **Policies created in this project** tab, select the
   **Auto-protection policies** view.

5. Click the policy you want to view.

   The **Auto-protection policy details** page appears.
6. To view the workload projects associated with the policy and their
   matching, protected, and failed resource counts, select the
   **Protected resources** tab.

### gcloud

To list all auto-protection policies in a specific location:

```
gcloud backup-dr auto-protection-policies list \
    --location=LOCATION \
    --project=BACKUP_VAULT_PROJECT
```

To view details of a specific auto-protection policy:

```
gcloud backup-dr auto-protection-policies describe POLICY_NAME \
    --location=LOCATION \
    --project=BACKUP_VAULT_PROJECT
```

To list the policy bindings for a specific auto-protection policy:

```
gcloud backup-dr auto-protection-bindings list \
    --auto-protection-policy=POLICY_NAME \
    --location=LOCATION \
    --project=BACKUP_VAULT_PROJECT
```

To list resources matching a specific policy binding (workload project):

```
gcloud backup-dr binding-matching-resources list \
    --auto-protection-policy-binding=BINDING_NAME \
    --auto-protection-policy=POLICY_NAME \
    --location=LOCATION \
    --project=BACKUP_VAULT_PROJECT
```

Replace the following:

- `LOCATION`: the location of the auto-protection policies.
- `BACKUP_VAULT_PROJECT`: the project ID where the auto-protection policies reside.
- `POLICY_NAME`: the name of the auto-protection policy.
- `BINDING_NAME`: the name of the policy binding.

### REST

To list all auto-protection policies in a specific location, send a `GET`
request to the
[`projects.locations.autoProtectionPolicies.list`](https://docs.cloud.google.com/backup-disaster-recovery/docs/reference/rest/v1/projects.locations.autoProtectionPolicies/list.md.txt)
method:

```
curl -X GET \
    -H "Authorization: Bearer $(gcloud auth print-access-token)" \
    "https://backupdr.googleapis.com/v1/projects/BACKUP_VAULT_PROJECT/locations/LOCATION/autoProtectionPolicies"
```

To view details of a specific auto-protection policy, send a `GET` request
to the
[`projects.locations.autoProtectionPolicies.get`](https://docs.cloud.google.com/backup-disaster-recovery/docs/reference/rest/v1/projects.locations.autoProtectionPolicies/get.md.txt)
method:

```
curl -X GET \
    -H "Authorization: Bearer $(gcloud auth print-access-token)" \
    "https://backupdr.googleapis.com/v1/projects/BACKUP_VAULT_PROJECT/locations/LOCATION/autoProtectionPolicies/POLICY_NAME"
```

To list the policy bindings for a specific auto-protection policy, send a
`GET` request to the
[`projects.locations.autoProtectionPolicies.bindings.list`](https://docs.cloud.google.com/backup-disaster-recovery/docs/reference/rest/v1/projects.locations.autoProtectionPolicies.bindings/list.md.txt)
method:

```
curl -X GET \
    -H "Authorization: Bearer $(gcloud auth print-access-token)" \
    "https://backupdr.googleapis.com/v1/projects/BACKUP_VAULT_PROJECT/locations/LOCATION/autoProtectionPolicies/POLICY_NAME/bindings"
```

To list resources matching a specific policy binding (workload project),
send a `GET` request to the
[`projects.locations.autoProtectionPolicies.bindings.matchingResources.list`](https://docs.cloud.google.com/backup-disaster-recovery/docs/reference/rest/v1/projects.locations.autoProtectionPolicies.bindings.matchingResources/list.md.txt)
method:

```
curl -X GET \
    -H "Authorization: Bearer $(gcloud auth print-access-token)" \
    "https://backupdr.googleapis.com/v1/projects/BACKUP_VAULT_PROJECT/locations/LOCATION/autoProtectionPolicies/POLICY_NAME/bindings/BINDING_NAME/matchingResources"
```

Replace the following:

- `BACKUP_VAULT_PROJECT`: the project ID where the auto-protection policies reside.
- `LOCATION`: the location of the auto-protection policies.
- `POLICY_NAME`: the name of the auto-protection policy.
- `BINDING_NAME`: the name of the policy binding.

## View applied auto-protection policies

As a workload administrator, you can view auto-protection policies applied to
resources in your workload project. These policies are created and managed
within their respective backup vault projects.

To view applied auto-protection policies in a workload project:

### Console

1. In the Google Cloud console, select your workload project.

2. Go to the **Backup and DR** page.

   [Go to Backup and DR](https://console.cloud.google.com/backupdr)
3. In the navigation menu, select **Auto-protection policies**.

4. Select the **Policies applied on this project** tab.

### gcloud

To list applied auto-protection policies in a specific location:

```
gcloud backup-dr applied-auto-protection-policies list \
    --location=LOCATION \
    --project=WORKLOAD_PROJECT_ID
```

Replace the following:

- `LOCATION`: the location of the applied auto-protection policies.
- `WORKLOAD_PROJECT_ID`: the project ID of the workload project.

### REST

To list applied auto-protection policies in a specific location, send a
`GET` request to the
[`projects.locations.appliedAutoProtectionPolicies.list`](https://docs.cloud.google.com/backup-disaster-recovery/docs/reference/rest/v1/projects.locations.appliedAutoProtectionPolicies/list.md.txt)
method:

```
curl -X GET \
    -H "Authorization: Bearer $(gcloud auth print-access-token)" \
    "https://backupdr.googleapis.com/v1/projects/WORKLOAD_PROJECT_ID/locations/LOCATION/appliedAutoProtectionPolicies"
```

Replace the following:

- `WORKLOAD_PROJECT_ID`: the project ID of the workload project.
- `LOCATION`: the location of the applied auto-protection policies.

The **Applied auto-protection policies** table displays the policies applied
to resources in the project, along with the following information:

- **Policy name**: The name of the auto-protection policy. Click the policy name to view policy details in the backup vault project.
- **Resource type**: The resource type protected by the policy (Compute Engine instances or disks).
- **Criteria**: The resource labels (key-value pairs) used to match resources in the project.
- **Backup vault project**: The backup vault project where the policy and backup vault are managed.
- **Status** : The status of the policy binding (for example, `Active`).
- **Region**: The region where the policy and protected resources reside.
- **Backup plan**: The backup plan assigned to matching resources. Click the backup plan name to view its details in the backup vault project.
- **Matching resources**: The number of resources in the workload project that match the label criteria.
- **Protected resources**: The number of matching resources successfully protected by the backup plan.
- **Resources failed to protect**: The number of matching resources that failed protection.

## Edit an auto-protection policy

You can update an auto-protection policy's description, label criteria, or
backup plan.

> [!NOTE]
> **Note:** You can change a policy's backup plan only to another backup plan for the same resource type in the same project and region. You can't change the resource type of a policy after you create it, and you can't delete a backup plan while an auto-protection policy uses it.

To edit an auto-protection policy:

### Console

1. In the Google Cloud console, go to the **Backup and DR** page.

   [Go to Backup and DR](https://console.cloud.google.com/backupdr)
2. In the navigation menu, select **Auto-protection policies**.

   The **Auto-protection policies** page appears.
3. Click the auto-protection policy you want to edit.

4. In the **Auto-protection policy details** page, click **Edit policy**.

5. Make the necessary changes to the auto-protection policy.

6. Click **Save changes**.

### gcloud

To update the description of an auto-protection policy:

```
gcloud backup-dr auto-protection-policies update POLICY_NAME \
    --location=LOCATION \
    --project=BACKUP_VAULT_PROJECT \
    --description="UPDATED_DESCRIPTION" \
    --no-async
```

To update the criteria (labels) of an auto-protection policy:

```
gcloud backup-dr auto-protection-policies update POLICY_NAME \
    --location=LOCATION \
    --project=BACKUP_VAULT_PROJECT \
    --criteria=key=NEW_LABEL_KEY,values=NEW_LABEL_VALUE \
    --no-async
```

To update the backup plan details:

```
gcloud backup-dr auto-protection-policies update POLICY_NAME \
    --location=LOCATION \
    --project=BACKUP_VAULT_PROJECT \
    --backup-plan-details="resource-type=RESOURCE_TYPE,backup-plan=projects/BACKUP_VAULT_PROJECT/locations/LOCATION/backupPlans/NEW_BACKUP_PLAN" \
    --no-async
```

Replace the following:

- `POLICY_NAME`: the name of the auto-protection policy to update.
- `LOCATION`: the location of the auto-protection policy.
- `BACKUP_VAULT_PROJECT`: the project ID where the backup vault and backup plan reside.
- `UPDATED_DESCRIPTION`: the updated description for the policy.
- `NEW_LABEL_KEY`: the new label key to use for matching resources.
- `NEW_LABEL_VALUE`: the new label value to use for matching resources.
- `RESOURCE_TYPE`: the resource type to protect. Use `compute.googleapis.com/Instance` for Compute Engine instances, or `compute.googleapis.com/Disk` for zonal and regional persistent disks. Don't use `compute.googleapis.com/RegionDisk`.
- `NEW_BACKUP_PLAN`: the name of the new backup plan to assign.

### REST

To update the description of an auto-protection policy, send a `PATCH`
request to the
[`projects.locations.autoProtectionPolicies.patch`](https://docs.cloud.google.com/backup-disaster-recovery/docs/reference/rest/v1/projects.locations.autoProtectionPolicies/patch.md.txt)
method with `updateMask=description`:

```
curl -X PATCH \
    -H "Authorization: Bearer $(gcloud auth print-access-token)" \
    -H "Content-Type: application/json" \
    -d '{
      "description": "UPDATED_DESCRIPTION"
    }' \
    "https://backupdr.googleapis.com/v1/projects/BACKUP_VAULT_PROJECT/locations/LOCATION/autoProtectionPolicies/POLICY_NAME?updateMask=description"
```

To update the criteria (labels) of an auto-protection policy, send a `PATCH`
request to the
[`projects.locations.autoProtectionPolicies.patch`](https://docs.cloud.google.com/backup-disaster-recovery/docs/reference/rest/v1/projects.locations.autoProtectionPolicies/patch.md.txt)
method with `updateMask=criteria`:

```
curl -X PATCH \
    -H "Authorization: Bearer $(gcloud auth print-access-token)" \
    -H "Content-Type: application/json" \
    -d '{
      "criteria": {
        "matchingConditions": [
          {
            "labelCondition": {
              "key": "NEW_LABEL_KEY",
              "values": [
                "NEW_LABEL_VALUE"
              ]
            }
          }
        ]
      }
    }' \
    "https://backupdr.googleapis.com/v1/projects/BACKUP_VAULT_PROJECT/locations/LOCATION/autoProtectionPolicies/POLICY_NAME?updateMask=criteria"
```

To update the backup plan details of an auto-protection policy, send a
`PATCH` request to the
[`projects.locations.autoProtectionPolicies.patch`](https://docs.cloud.google.com/backup-disaster-recovery/docs/reference/rest/v1/projects.locations.autoProtectionPolicies/patch.md.txt)
method with `updateMask=backupPlanDetails`:

```
curl -X PATCH \
    -H "Authorization: Bearer $(gcloud auth print-access-token)" \
    -H "Content-Type: application/json" \
    -d '{
      "backupPlanDetails": [
        {
          "resourceType": "RESOURCE_TYPE",
          "backupPlan": "projects/BACKUP_VAULT_PROJECT/locations/LOCATION/backupPlans/NEW_BACKUP_PLAN"
        }
      ]
    }' \
    "https://backupdr.googleapis.com/v1/projects/BACKUP_VAULT_PROJECT/locations/LOCATION/autoProtectionPolicies/POLICY_NAME?updateMask=backupPlanDetails"
```

Replace the following:

- `BACKUP_VAULT_PROJECT`: the project ID where the backup vault and backup plan reside.
- `LOCATION`: the location of the auto-protection policy.
- `POLICY_NAME`: the name of the auto-protection policy to update.
- `UPDATED_DESCRIPTION`: the updated description for the policy.
- `NEW_LABEL_KEY`: the new label key to use for matching resources.
- `NEW_LABEL_VALUE`: the new label value to use for matching resources.
- `RESOURCE_TYPE`: the resource type to protect. Use `compute.googleapis.com/Instance` for Compute Engine instances, or `compute.googleapis.com/Disk` for zonal and regional persistent disks. Don't use `compute.googleapis.com/RegionDisk`.
- `NEW_BACKUP_PLAN`: the name of the new backup plan to assign.

## Update resource projects in an auto-protection policy

You can modify the scope of an existing auto-protection policy by adding or
removing workload projects.

### Add projects to a policy

### Console

1. In the Google Cloud console, go to the **Backup and DR** page.

   [Go to Backup and DR](https://console.cloud.google.com/backupdr)
2. In the navigation menu, select **Auto-protection policies**.

3. Click the policy you want to update.

4. Click **Add projects**.

   The **Add projects to policy** page appears.
5. Click **Select projects**.

6. In the **Search projects** field, enter the name of the project you want to
   add to the policy.

7. Check the box next to the name of the project you want to add to the policy.

8. Click **Select**.

9. Click **Add**.

   A confirmation message appears.

### gcloud

To add a workload project to an auto-protection policy, create a policy binding:

```
gcloud backup-dr auto-protection-bindings create BINDING_NAME \
    --auto-protection-policy=POLICY_NAME \
    --location=LOCATION \
    --project=BACKUP_VAULT_PROJECT \
    --scope=projects/WORKLOAD_PROJECT_ID
```

Replace the following:

- `BINDING_NAME`: a name for the policy binding.
- `POLICY_NAME`: the name of the auto-protection policy.
- `LOCATION`: the location of the auto-protection policy.
- `BACKUP_VAULT_PROJECT`: the project ID where the auto-protection policy resides.
- `WORKLOAD_PROJECT_ID`: the project ID of the workload project to protect.

### REST

To add a workload project to an auto-protection policy, create a policy
binding by sending a `POST` request to the
[`projects.locations.autoProtectionPolicies.bindings.create`](https://docs.cloud.google.com/backup-disaster-recovery/docs/reference/rest/v1/projects.locations.autoProtectionPolicies.bindings/create.md.txt)
method:

```
curl -X POST \
    -H "Authorization: Bearer $(gcloud auth print-access-token)" \
    -H "Content-Type: application/json" \
    -d '{
      "scope": "projects/WORKLOAD_PROJECT_ID"
    }' \
    "https://backupdr.googleapis.com/v1/projects/BACKUP_VAULT_PROJECT/locations/LOCATION/autoProtectionPolicies/POLICY_NAME/bindings?autoProtectionPolicyBindingId=BINDING_NAME"
```

Replace the following:

- `BACKUP_VAULT_PROJECT`: the project ID where the auto-protection policy resides.
- `LOCATION`: the location of the auto-protection policy.
- `POLICY_NAME`: the name of the auto-protection policy.
- `BINDING_NAME`: a name for the policy binding.
- `WORKLOAD_PROJECT_ID`: the project ID of the workload project to protect.

### Remove projects from a policy

### Console

1. In the Google Cloud console, go to the **Backup and DR** page.

   [Go to Backup and DR](https://console.cloud.google.com/backupdr)
2. In the navigation menu, select **Auto-protection policies**.

3. Click the policy you want to update.

4. Select the **Protected resources** tab.

5. Select the projects you want to remove from the policy scope.

6. Click **Remove projects**.

7. To confirm that you want to remove the auto-protection policy from the
   project, enter `confirm`.

8. Click **Remove**.

   A confirmation notification appears.

### gcloud

To remove a workload project from an auto-protection policy, delete the policy binding:

```
gcloud backup-dr auto-protection-bindings delete BINDING_NAME \
    --auto-protection-policy=POLICY_NAME \
    --location=LOCATION \
    --project=BACKUP_VAULT_PROJECT
```

Replace the following:

- `BINDING_NAME`: the name of the policy binding to delete.
- `POLICY_NAME`: the name of the auto-protection policy.
- `LOCATION`: the location of the auto-protection policy.
- `BACKUP_VAULT_PROJECT`: the project ID where the auto-protection policy resides.

### REST

To remove a workload project from an auto-protection policy, initiate
deletion of the policy binding by sending a `POST` request to the
[`projects.locations.autoProtectionPolicies.bindings.initiateDelete`](https://docs.cloud.google.com/backup-disaster-recovery/docs/reference/rest/v1/projects.locations.autoProtectionPolicies.bindings/initiateDelete.md.txt)
method:

```
curl -X POST \
    -H "Authorization: Bearer $(gcloud auth print-access-token)" \
    -H "Content-Type: application/json" \
    -d '{}' \
    "https://backupdr.googleapis.com/v1/projects/BACKUP_VAULT_PROJECT/locations/LOCATION/autoProtectionPolicies/POLICY_NAME/bindings/BINDING_NAME:initiateDelete"
```

Replace the following:

- `BACKUP_VAULT_PROJECT`: the project ID where the auto-protection policy resides.
- `LOCATION`: the location of the auto-protection policy.
- `POLICY_NAME`: the name of the auto-protection policy.
- `BINDING_NAME`: the name of the policy binding to delete.

Unprotecting resources and removing projects typically takes up to 2 hours, but
in some cases can take up to 8 hours.

## Delete an auto-protection policy

Before deleting an auto-protection policy, you must remove all workload
projects from the policy's scope. For instructions, see
[Remove projects from a policy](https://docs.cloud.google.com/backup-disaster-recovery/docs/protect/automate-protection.md.txt#remove-projects-from-policy).

Removing a project starts an asynchronous process that removes auto-protection
from the project's resources. The policy binding stays in the
`DELETION_INITIATED` state until this process completes, which typically takes
up to 2 hours, but in some cases can take up to 8 hours. Until all bindings are
removed, deleting the policy fails with a `POLICY_IN_USE_BY_BINDING` error.

To delete an auto-protection policy:

### Console

1. In the Google Cloud console, go to the **Backup and DR** page.

   [Go to Backup and DR](https://console.cloud.google.com/backupdr)
2. In the navigation menu, select **Auto-protection policies**.

3. Click the policy you want to delete.

4. Click **Delete**.

5. In the confirmation dialog, to confirm that you want to delete the
   policy, enter `delete`.

6. Click **Delete**.

### gcloud

To delete an auto-protection policy:

```
gcloud backup-dr auto-protection-policies delete POLICY_NAME \
    --location=LOCATION \
    --project=BACKUP_VAULT_PROJECT
```

Replace the following:

- `POLICY_NAME`: the name of the auto-protection policy to delete.
- `LOCATION`: the location of the auto-protection policy.
- `BACKUP_VAULT_PROJECT`: the project ID where the auto-protection policy resides.

### REST

To delete an auto-protection policy, send a `DELETE` request to the
[`projects.locations.autoProtectionPolicies.delete`](https://docs.cloud.google.com/backup-disaster-recovery/docs/reference/rest/v1/projects.locations.autoProtectionPolicies/delete.md.txt)
method:

```
curl -X DELETE \
    -H "Authorization: Bearer $(gcloud auth print-access-token)" \
    "https://backupdr.googleapis.com/v1/projects/BACKUP_VAULT_PROJECT/locations/LOCATION/autoProtectionPolicies/POLICY_NAME"
```

Replace the following:

- `BACKUP_VAULT_PROJECT`: the project ID where the auto-protection policy resides.
- `LOCATION`: the location of the auto-protection policy.
- `POLICY_NAME`: the name of the auto-protection policy to delete.

## Clean up

To avoid incurring charges to your Google Cloud account for the resources
used in this tutorial, delete the resources that you created or delete your
dedicated test projects:

1. Remove all workload projects by deleting the policy bindings from any
   auto-protection policies that you created during this tutorial. Wait until
   unprotection completes and no bindings remain in the `DELETION_INITIATED`
   state. For instructions, see
   [Remove projects from a policy](https://docs.cloud.google.com/backup-disaster-recovery/docs/protect/automate-protection.md.txt#remove-projects-from-policy).

2. Delete any auto-protection policies that you created during this tutorial.
   For instructions, see
   [Delete an auto-protection policy](https://docs.cloud.google.com/backup-disaster-recovery/docs/protect/automate-protection.md.txt#delete-protection-policy).

3. To avoid ongoing storage charges, delete any test backup plans and
   backup vaults that you created for this tutorial. Before you delete
   a backup vault, delete all of its backups or wait for them to
   expire. You can't delete backups until the backup vault's minimum
   enforced retention period ends. For instructions, see
   [Delete a backup plan](https://docs.cloud.google.com/backup-disaster-recovery/docs/cloud-console/backup-plan-create.md.txt#delete_a_backup_plan),
   [Delete a backup](https://docs.cloud.google.com/backup-disaster-recovery/docs/cloud-console/bv-backup-manage.md.txt#delete_a_backup),
   and
   [Delete a backup vault](https://docs.cloud.google.com/backup-disaster-recovery/docs/cloud-console/backup-vault-create.md.txt#delete).

4. If you no longer want test Compute Engine instances and disks protected,
   remove their resource labels.

5. Optional: If you created dedicated Google Cloud projects for this
   tutorial, delete the projects:

   1. In the Google Cloud console, go to the **Manage resources** page.

      [Go to Manage resources](https://console.cloud.google.com/cloud-resource-manager)
   2. Select the project and click **Delete**.

   3. Enter the project ID and click **Shut down**.

## What's next

- Learn more about [Backup plans in Google Cloud console](https://docs.cloud.google.com/backup-disaster-recovery/docs/cloud-console/backup-plan.md.txt).
- Learn how to [Back up Compute Engine instances](https://docs.cloud.google.com/backup-disaster-recovery/docs/cloud-console/compute/compute-instance-backup.md.txt).
- Learn about [Customer-managed encryption keys (CMEK) with Backup and DR](https://docs.cloud.google.com/backup-disaster-recovery/docs/concepts/cmek.md.txt).
- Try out other Google Cloud features for yourself. Have a look at our [tutorials](https://cloud.google.com/docs/tutorials).