List backups

Lists backups from a specified cluster and table.

Code sample

Node.js

To learn how to install and use the client library for Bigtable, see Bigtable client libraries.

To authenticate to Bigtable, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

'use strict';

const {BigtableTableAdminClient} = require('@google-cloud/bigtable').v2;
const {status} = require('@grpc/grpc-js');

const client = new BigtableTableAdminClient();

/**
 * Lists Bigtable backups in a specified cluster or across all clusters in an instance.
 *
 * @param {string} projectId - The Google Cloud project ID.
 * @param {string} instanceId - The ID of the Bigtable instance hosting the cluster.
 * @param {string} clusterId - The ID of the Bigtable cluster, or '-' to list backups across all clusters in the instance.
 */
async function listBackups(projectId, instanceId, clusterId) {
  try {
    const parent = `projects/${projectId}/instances/${instanceId}/clusters/${clusterId}`;
    const request = {
      parent,
    };

    const [backups] = await client.listBackups(request);
    console.log(`Listing backups in ${parent}:`);
    for (const backup of backups) {
      console.log(`- Backup name: ${backup.name}`);
      console.log(`  Source table: ${backup.sourceTable}`);
      console.log(`  State: ${backup.state}`);
      console.log(`  Size in bytes: ${backup.sizeBytes}`);
    }
  } catch (err) {
    if (err.code === status.NOT_FOUND) {
      console.error(`Resource not found: ${err.message}`);
    } else if (err.code === status.PERMISSION_DENIED) {
      console.error(`Permission denied: ${err.message}`);
    } else if (err.code !== undefined) {
      console.error(`API error code ${err.code}: ${err.message}`);
    } else {
      throw err;
    }
  }
}

module.exports = {listBackups};

Python

To learn how to install and use the client library for Bigtable, see Bigtable client libraries.

To authenticate to Bigtable, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

import google.api_core.exceptions
from google.cloud import bigtable_admin_v2


def list_backups(project_id: str, instance_id: str, cluster_id: str) -> None:
    """Lists Bigtable backups in a cluster or across all clusters in an instance.

    Args:
        project_id: The Google Cloud project ID (for example, 'example-project-id').
        instance_id: The ID of the Bigtable instance (for example, 'example-instance-id').
        cluster_id: The ID of the Bigtable cluster, or '-' to list backups across all clusters.
    """
    client = bigtable_admin_v2.BigtableTableAdminClient()
    parent = f"projects/{project_id}/instances/{instance_id}/clusters/{cluster_id}"

    try:
        response = client.list_backups(parent=parent)
        for backup in response:
            print(f"Backup name: {backup.name}")
    except google.api_core.exceptions.GoogleAPICallError as error:
        print(f"API call failed: {error}")

What's next

To search and filter code samples for other Google Cloud products, see the Google Cloud sample browser.