使用 gcloud 备份和恢复数据库

本页面介绍了如何将 Cloud Spanner 备份和恢复与 gcloud 命令行界面配合使用。

前提条件

本页中的示例进行如下假设:

  • 您已设置 gcloud SDK,以用于 Cloud Spanner。如果您是第一次将 gcloud 与 Cloud Spanner 配合使用,请参阅 Cloud Spanner 入门指南 (gcloud)

  • 您已为默认项目配置了 gcloud。例如:

    gcloud config set core/project my-project
    
  • 您有一个名为 test-instance 的实例和一个名为 example-db 的数据库。

创建备份

要创建名为 example-db-backup-6 的备份并使其在 1 年后到期,请执行以下操作:

gcloud spanner backups create example-db-backup-6 --instance=test-instance \
    --database=example-db --retention-period=1y --async

以下是一些使用说明:

  • 要设置备份的到期日期,请指定 --retention-period--expiration-date 标志。如需了解日期语法,请参阅 gcloud topic datetimes
  • 备份的名称在实例中必须是唯一的。
  • 由于 --async 标志,该命令会立即返回。如果没有此标志,该命令将等待备份操作完成。
  • 请指定 --help 标志以获取有关任何命令的帮助。

以下是输出结果:

Create request issued for: [example-db-backup-6]
Check operation [projects/my-project/instances/test-instance/backups/example-db-backup-6/operations/_auto_op_234567] for status.

在下一部分中,我们将使用备份操作对备份进度进行检查。

如需详细了解此命令,请参阅 gcloud spanner backups create

显示备份进度

  1. 要跟踪备份操作的进度,请使用 gcloud spanner operations describe

    gcloud spanner operations describe _auto_op_234567 \
        --instance=test-instance --backup=example-db-backup-6
    

    输出如下:

    done: true
    metadata:
      '@type': type.googleapis.com/google.spanner.admin.database.v1.CreateBackupMetadata
      database: projects/my-project/instances/test-instance/databases/example-db
      name: projects/my-project/instances/test-instance/backups/example-db-backup-6
      progress:
        endTime: '2020-02-04T02:07:59.089820Z'
        progressPercent: 100
        startTime: '2020-02-04T02:05:43.920377Z'
    name: projects/my-project/instances/test-instance/backups/example-db-backup-6/operations/_auto_op_234567
    response:
      '@type': type.googleapis.com/google.spanner.admin.database.v1.Backup
      createTime: '2020-02-04T02:05:43.920377Z'
      database: projects/my-project/instances/test-instance/databases/example-db
      expireTime: '2021-02-04T02:05:43.268327Z'
      name: projects/my-project/instances/test-instance/backups/example-db-backup-6
      sizeBytes: '1000000000'
      state: READY
    
  2. 如果您没有该操作的名称,请使用 gcloud spanner operations list 进行查找:

    gcloud spanner operations list --instance=test-instance --type=BACKUP
    

    使用说明:

    • 要限制列表,请指定 --filter 标志。

      • 例如,--filter="metadata.name:example-db"仅列出对特定数据库的备份操作。
      • 例如,--filter="error:*" 仅列出失败的备份操作。

      如需了解过滤条件语法,请参阅 gcloud topic filters。如需了解如何过滤备份操作,请参阅 ListBackupOperationsRequest 中的 filter 字段。

    • --type 标志不区分大小写。

    输出如下:

    OPERATION_ID               DONE  @TYPE                 BACKUP               SOURCE_DATABASE  START_TIME                   END_TIME
    _auto_op_123456  True  CreateBackupMetadata  example-db-backup-7  example-db       2020-02-04T02:12:38.075515Z  2020-02-04T02:22:40.581170Z
    _auto_op_234567  True  CreateBackupMetadata  example-db-backup-6  example-db       2020-02-04T02:05:43.920377Z  2020-02-04T02:07:59.089820Z
    

从备份中恢复数据库

要恢复数据库,请使用:

gcloud spanner databases restore --async \
    --destination-instance=test-instance --destination-database=example-db-restored \
    --source-instance=test-instance --source-backup=example-db-backup-6

使用说明:

  • 由于 --async 标志,该命令会立即返回。如果没有此标志,该命令将等待恢复操作完成。
  • 如果来源实例和目标实例不同,则它们必须具有相同的实例配置
  • 如果目标数据库已存在,则操作将失败。

输出如下:

Restore database in progress. Operation name=projects/my-project/instances/test-instance/databases/example-db-restored-1/operations/_auto_op_bb8e360b256b04bf

如需详细了解此命令,请参阅 gcloud spanner databases restore

显示恢复和优化进度

  1. 要跟踪恢复操作的进度,请使用 gcloud spanner operations describe

    gcloud spanner operations describe  _auto_op_bb8e360b256b04bf \
        --instance=test-instance --database=example-db-restored
    

    输出如下:

    done: true
    metadata:
      '@type': type.googleapis.com/google.spanner.admin.database.v1.RestoreDatabaseMetadata
      backupInfo:
        backup: projects/my-project/instances/test-instance/backups/example-db-backup-6
        createTime: '2020-02-03T23:48:06.259296Z'
        sourceDatabase: projects/my-project/instances/test-instance/databases/example-db
      name: projects/my-project/instances/test-instance/databases/example-db-restored
      optimizeDatabaseOperationName: projects/my-project/instances/test-instance/databases/example-db-restored/operations/_auto_op_7702f3f19fa99616
      progress:
        endTime: '2020-02-04T02:40:56.424382Z'
        progressPercent: 100
        startTime: '2020-02-04T02:35:30.683330Z'
      sourceType: BACKUP
    name: projects/my-project/instances/test-instance/databases/example-db-restored/operations/_auto_op_91ff1f270832a22b
    response:
      '@type': type.googleapis.com/google.spanner.admin.database.v1.Database
      createTime: '2020-02-04T02:35:30.683330Z'
      name: projects/my-project/instances/test-instance/databases/example-db-restored
      restoreInfo:
        backupInfo:
          backup: projects/my-project/instances/test-instance/backups/example-db-backup-6
          createTime: '2020-02-03T23:48:06.259296Z'
          sourceDatabase: projects/my-project/instances/test-instance/databases/example-db
        sourceType: BACKUP
      state: READY_OPTIMIZING
    

    从输出中,我们可以看到恢复操作已完成,并且现在处于 READY_OPTIMIZING 状态,如恢复的工作原理所述。我们还获得了 optimizeDatabaseOperationName,用它来跟踪优化操作的进度。

  2. 要跟踪优化操作的进度,请使用 gcloud spanner operations describe

    gcloud-testing spanner operations describe _auto_op_7702f3f19fa99616 \
        --instance=test-instance --database=example-db-restored
    

    输出如下:

    done: true
    metadata:
      '@type': type.googleapis.com/google.spanner.admin.database.v1.OptimizeRestoredDatabaseMetadata
      name: projects/my-project/instances/test-instance/databases/example-db-restored
      progress:
        endTime: '2020-02-04T22:11:21.731739Z'
        progressPercent: 100
        startTime: '2020-02-04T22:10:48.308090Z'
    name: projects/my-project/instances/test-instance/databases/example-db-restored/operations/_auto_op_7702f3f19fa99616
    response:
      '@type': type.googleapis.com/google.spanner.admin.database.v1.Database
      createTime: '2020-02-04T22:03:34.282186Z'
      name: projects/my-project/instances/test-instance/databases/example-db-restored
      restoreInfo:
        backupInfo:
          backup: projects/my-project/instances/test-instance/backups/example-db-backup-6
          createTime: '2020-02-03T23:48:06.259296Z'
          sourceDatabase: projects/my-project/instances/test-instance/databases/example-db
        sourceType: BACKUP
      state: READY
    
  3. 如果您没有恢复或优化操作的名称,请使用 gcloud spanner operations list 进行查找。如果恢复的数据库与备份不在同一实例中,请确保在 instance 标志中指定目标实例:

    gcloud spanner operations list --instance=test-instance \
        --type=DATABASE_RESTORE
    

    输出如下:

    OPERATION_ID               DONE  @TYPE                             RESTORED_DATABASE    SOURCE_BACKUP        START_TIME                   END_TIME
    _auto_op_7702f3f19fa99616  True  OptimizeRestoredDatabaseMetadata  example-db-restored                       2020-02-04T02:40:56.424382Z  2020-02-04T02:41:37.765157Z
    _auto_op_91ff1f270832a22b  True  RestoreDatabaseMetadata           example-db-restored  example-db-backup-6  2020-02-04T02:35:30.683330Z  2020-02-04T02:40:56.424382Z
    

获取备份信息

要获取备份的相关信息,请使用:

gcloud spanner backups describe example-db-backup-6 --instance=test-instance

输出如下:

createTime: '2020-02-04T02:05:43.920377Z'
database: projects/my-project/instances/test-instance/databases/example-db
expireTime: '2021-02-04T02:05:43.268327Z'
name: projects/my-project/instances/test-instance/backups/example-db-backup-6
sizeBytes: '1000000000'
state: READY

如需详细了解此命令,请参阅 gcloud spanner backups describe

列出备份

要列出实例中的所有备份,请使用:

gcloud spanner backups list --instance=test-instance

输出如下:

BACKUP               SOURCE_DATABASE  CREATION_TIME                EXPIRATION_TIME              STATE  BACKUP_SIZE_IN_BYTES  IN_USE_BY
example-db-backup-6  example-db       2020-02-04T02:05:43.920377Z  2021-02-04T02:05:43.268327Z  CREATING
example-db-backup-4  example-db       2020-02-04T01:21:20.873839Z  2021-02-04T01:21:20.530151Z  READY  32
example-db-backup-3  example-db       2020-02-03T23:59:18.936433Z  2021-02-03T23:59:18.203083Z  READY  32
example-db-backup-5  example-db       2020-02-03T23:48:06.259296Z  2021-02-03T23:48:05.830937Z  READY  32
example-db-backup-2  example-db       2020-01-30T19:49:00.616338Z  2021-01-30T19:49:00.283917Z  READY  32
example-db-backup-1  example-db       2020-01-30T19:47:09.492551Z  2021-01-30T19:47:09.097804Z  READY  32

要限制列表,请指定 --filter 标志。例如,--filter="state:creating" 仅列出仍在创建中的备份。 如需了解过滤条件语法,请参阅 gcloud topic filters。如需了解如何过滤备份,请参阅 ListBackupsRequest 中的 filter 字段。

如需详细了解此命令,请参阅 gcloud spanner backups list

更新备份元数据

要更新备份的到期日期,请执行以下操作:

gcloud spanner backups update-metadata example-db-backup-6 \
    --instance=test-instance --expiration-date=2020-05-05T00:00:00Z

输出如下:

createTime: '2020-02-04T02:05:43.920377Z'
database: projects/my-project/instances/test-instance/databases/example-db
expireTime: '2020-05-05T00:00:00Z'
name: projects/my-project/instances/test-instance/backups/example-db-backup-6
sizeBytes: '1000000000'
state: READY

如需详细了解此命令,请参阅 gcloud spanner backups update-metadata

删除备份

要删除备份,请使用:

gcloud spanner backups delete example-db-backup-1 --instance=test-instance

您必须确认操作:

You are about to delete backup [example-db-backup-1]

Do you want to continue (Y/n)?  Y

Deleted backup [example-db-backup-1].

如需详细了解此命令,请参阅 gcloud spanner backups delete