バックアップを一覧表示する

指定したクラスタとテーブルのバックアップを一覧表示します。

コードサンプル

Node.js

Bigtable 用のクライアント ライブラリをインストールして使用する方法については、Bigtable クライアント ライブラリをご覧ください。

Bigtable で認証を行うには、アプリケーションのデフォルト認証情報を設定します。詳細については、ローカル開発環境の認証を設定するをご覧ください。

'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

Bigtable 用のクライアント ライブラリをインストールして使用する方法については、Bigtable クライアント ライブラリをご覧ください。

Bigtable で認証を行うには、アプリケーションのデフォルト認証情報を設定します。詳細については、ローカル開発環境の認証を設定するをご覧ください。

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}")

次のステップ

他の Google Cloud プロダクトのコードサンプルを検索およびフィルタするには、Google Cloud サンプル ブラウザをご覧ください。