To preserve a snapshot of your AlloyDB for PostgreSQL cluster data outside your
automated backup schedule---such as before a schema migration or major
application update---you can create an on-demand backup.

When you create an on-demand backup, AlloyDB verifies that the
cluster isn't in the `CREATING` or `DELETING` state and has initialized
storage. AlloyDB then starts a long-running operation to back
up the cluster data.

AlloyDB rejects backup requests for clusters without initialized
storage, such as newly created clusters before their first instance initializes
storage.

After a cluster's storage is initialized, you can create an on-demand backup on
a secondary cluster, or on a primary cluster even when its primary instance is
in the `STOPPED` state or after you delete the primary instance.


## Before you begin

- The Google Cloud project you are using must have been [enabled to access AlloyDB](https://docs.cloud.google.com/alloydb/docs/quickstart/create-and-connect.md.txt#before-you-begin).
- You must have one of these IAM roles in the Google Cloud project you are using:
  - `roles/alloydb.admin` (the AlloyDB Admin predefined IAM role)
  - `roles/owner` (the Owner basic IAM role)
  - `roles/editor` (the Editor basic IAM role)

  If you don't have any of these roles, contact your Organization Administrator to request
  access.

<br />

## Procedure

### Console

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

   [Go to Clusters](https://console.cloud.google.com/alloydb/clusters)
2. Click a cluster in the **Resource Name** column.
3. In the navigation menu, click **Data protection**.
4. Click **Create backup**.
5. Enter an ID for the backup.
6. Optional: Enter a description to help you identify this backup.
7. If you want to encrypt this backup using [Customer-managed encryption key (CMEK)](https://docs.cloud.google.com/alloydb/docs/cmek.md.txt)
   instead of Google-managed encryption, follow these
   additional steps:

   1. Click **Advanced encryption options**.
   2. Select **Customer-managed encryption key (CMEK)**.
   3. Select a
      customer-managed key from the menu that appears.

      The Google Cloud console limits this list to keys within
      the same Google Cloud project and region as the new
      cluster. To use a key that is not on this list, click
      **Don't see your key? Enter key resource name**,
      and then type the key's resource name into the resulting
      dialog.

      Note that using CMEK with AlloyDB requires
      some additional setup. For more information, see [Using CMEK with
      AlloyDB](https://docs.cloud.google.com/alloydb/docs/use-cmek).
8. Click **Create**.

AlloyDB verifies that the source cluster isn't in the
`CREATING` or `DELETING` state and has initialized
storage. AlloyDB then starts a long-running operation to
perform the backup. The **Data protection** page shows the backup with a
status of **In progress** until the operation completes.

### gcloud

To use the gcloud CLI, you can
[install and initialize](https://docs.cloud.google.com/sdk/docs/install-sdk.md.txt) the Google Cloud CLI, or you
can use [Cloud Shell](https://docs.cloud.google.com/shell/docs/using-cloud-shell.md.txt).

Use the [`gcloud alloydb backups create`](https://docs.cloud.google.com/sdk/gcloud/reference/alloydb/backups/create.md.txt) command to create an on-demand backup.

```
gcloud alloydb backups create BACKUP_ID \
    --cluster=CLUSTER_ID \
    --region=REGION_ID \
    --project=PROJECT_ID \
    --async
```

This command returns an operation, whose status you can query using the [`gcloud alloydb operations describe`](https://docs.cloud.google.com/sdk/gcloud/reference/alloydb/operations/describe.md.txt) command.

```
gcloud alloydb operations describe OPERATION_ID \
    --region=REGION_ID \
    --project=PROJECT_ID
```

- <var translate="no">BACKUP_ID</var>: The ID for the backup to create.   
  To create a backup in a [cross-region location](https://docs.cloud.google.com/alloydb/docs/backup/overview.md.txt#cross-region-backup-location) using the gcloud CLI, replace with the full backup path in the following format:  
  `projects/PROJECT_ID/locations/REGION_ID/backups/BACKUP_ID`  

  > [!NOTE]
  > Cross-region backups require gcloud CLI version 484.0.0 or higher.

- <var translate="no">CLUSTER_ID</var>: The ID of the cluster you want to back up.
- <var translate="no">REGION_ID</var>: The ID of the region where the cluster is placed.
- <var translate="no">PROJECT_ID</var>: The ID of the project where the cluster is placed.

If you want to encrypt this backup using a [customer-managed encryption key (CMEK)](https://docs.cloud.google.com/alloydb/docs/cmek.md.txt)
instead of the default Google-managed encryption, then you must provide these additional arguments:

- `--kms-key=KEY_ID`: The ID of the CMEK key to use.
- `--kms-keyring=KEYRING_ID`: The ID of the key's keyring.
- `--kms-location=LOCATION_ID`: The ID of that keyring's region. Note that it must match the backup's region that you set in the `--region` argument, or in the full backup path for a cross-region backup.
- `--kms-project=PROJECT_ID`: The keyring's project ID.

> [!NOTE]
> Using CMEK with AlloyDB requires some additional setup. For more information, see [Using CMEK with
> AlloyDB](https://docs.cloud.google.com/alloydb/docs/use-cmek).

When you run this command, AlloyDB verifies that the source
cluster isn't in the `CREATING` or `DELETING` state and
has initialized storage. AlloyDB then starts a long-running
operation to perform the backup and displays information about the
long-running operation.

You can track completion of the long-running operation using the
`operations describe` command:

```
gcloud alloydb operations describe OPERATION_ID \
    --region=REGION_ID \
    --project=PROJECT_ID
```

<var translate="no">OPERATION_ID</var>: The operation ID reported when you ran the
`backups create` command.