gcloud CLI를 사용하여 데이터베이스 백업 및 복원

이 페이지에서는 Google Cloud CLI를 사용하여 Spanner 백업 및 복원을 사용하는 방법을 설명합니다.

기본 요건

이 페이지의 예시는 다음을 가정하고 진행됩니다.

  • Spanner에 사용하도록 gcloud CLI가 이미 설정되었음. Spanner에서 Google Cloud CLI를 처음 사용하는 경우 Google Cloud CLI를 사용하여 Spanner 시작하기를 참조하세요.

  • Google Cloud CLI가 기본 프로젝트로 구성되어 있음. 예를 들면 다음과 같습니다.

    gcloud config set core/project my-project
    
  • test-instance라는 인스턴스와 example-db라는 데이터베이스가 있음

백업 만들기

example-db-backup-6이라는 백업을 만들고 만료 기간을 1년으로 설정하려면 gcloud spanner backups create를 사용하세요.

   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 copy \
    --source-instance=test-instance \
    --source-backup=example-db \
    --destination-instance=backup-instance \
    --destination-backup=copied-backup \
    --expiration-date=2022-03-30T10:49:41Z

다른 프로젝트의 다른 인스턴스에 백업을 복사하려면 다음 안내를 따르세요.

gcloud spanner backups copy \
  --source-backup=projects/my-project/instances/test-instance/backups/example-db \
  --destination-backup=projects/backup-project/instances/backup-instance-eu/backups/copied-backup \
  --expiration-date=2022-03-30T10:49:41Z

사용 참고사항:

  • 백업을 동일한 프로젝트의 다른 인스턴스로 복사하도록 선택한 경우 복사된 백업을 위한 새 인스턴스를 만들거나 인스턴스를 준비해야 합니다. 앞의 예시에서는 backup-instance가 사용되었습니다. 백업 복사 작업의 일부로 새 인스턴스를 만들 수 없습니다.
  • 백업을 다른 프로젝트에 복사하도록 선택한 경우 복사된 백업을 위해 자체 인스턴스가 준비된 다른 프로젝트가 있어야 합니다. 앞의 예시에서는 backup-project가 사용됩니다. 백업 복사 작업의 일부로 새 프로젝트를 만들 수 없습니다.
  • 백업 만료 시간은 현재 복사 요청이 처리된 시점부터 최소 6시간, 소스 백업 create_time 이후 최대 366일이어야 합니다.

결과는 다음과 유사합니다.

createTime: '2022-03-29T22:06:05.905823Z'
database: projects/my-project/instances/test-instance/databases/example-db
databaseDialect: GOOGLE_STANDARD_SQL
encryptionInfo:
  encryptionType: GOOGLE_DEFAULT_ENCRYPTION
expireTime: '2022-03-30T10:49:41Z'
maxExpireTime: '2023-03-17T20:46:33.479336Z'
name: projects/backup-project/instances/backup-instance-eu/backups/copied-backup
sizeBytes: '7957667'
state: READY
versionTime: '2022-03-16T20:46:33.479336Z'

복사 작업의 진행 상황을 확인하려면 작업 진행 상황 확인을 참조하세요.

백업에서 데이터베이스 복원

데이터베이스를 복원하려면 gcloud spanner databases restore를 사용합니다.

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 operations describe를 사용하여 작업 진행 상황을 확인합니다.

  1. 작업 ID를 가져옵니다.

    gcloud spanner operations list --instance=INSTANCE-NAME \
    --database=DATABASE-NAME --type=BACKUP
    

    다음을 바꿉니다.

    • INSTANCE-NAME을 Spanner 인스턴스 이름으로 바꿉니다.
    • DATABASE-NAME을 데이터베이스 이름으로 바꿉니다.

    사용 참고사항:

    • 목록을 제한하려면 --filter 태그를 지정합니다. 예를 들면 다음과 같습니다.

      • --filter="metadata.name:example-db"는 특정 데이터베이스의 작업만 나열합니다.
      • --filter="error:*"는 실패한 백업 작업만 나열합니다.

      필터 구문에 대한 자세한 내용은 gcloud topic filters를 참조하세요. 백업 작업 필터링에 대한 자세한 내용은 ListBackupOperationsRequestfilter 필드를 참조하세요.

    • --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
      ```
    
  2. gcloud spanner operations describe를 실행합니다.

    gcloud spanner operations describe OPERATION-ID \
    --instance=INSTANCE-NAME \
    --backup=BACKUP-NAME \
    

    다음을 바꿉니다.

    • OPERATION-ID: 확인할 작업의 작업 ID
    • INSTANCE-NAME: Spanner 인스턴스 이름
    • BACKUP-NAME: Spanner 백업 이름

    출력의 progress 섹션에 완료된 작업의 비율이 표시됩니다. 결과는 다음과 유사합니다.

    done: true
    metadata:
    ...
    progress:
    - endTime: '2022-03-01T00:28:06.691403Z'
      progressPercent: 100
      startTime: '2022-03-01T00:28:04.221401Z'
    - endTime: '2022-03-01T00:28:17.624588Z'
      startTime: '2022-03-01T00:28:06.691403Z'
      progressPercent: 100
    ...
    

작업에 시간이 너무 오래 걸리는 경우 취소할 수 있습니다. 자세한 내용은 장기 실행 백업 작업 취소를 참조하세요.

백업 정보 가져오기

백업에 대한 정보를 가져오려면 다음 안내를 따르세요.

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를 참조하세요. 백업 필터링에 대한 자세한 내용은 ListBackupsRequestfilter 필드를 참조하세요.

이 명령어에 대한 자세한 내용은 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을 참조하세요.