---
title: "Spanner のディザスターリカバリ | grasys blog"
url: "https://blog.grasys.io/post/shimichan/cloud_spanner_disaster_recovery"
description: "boom boom! Hello Cloud! どうも、しみちゃんでございます。 今回は Spanner のディザスターリカバリ対応方法について、紹介をしたいと思います。 Cloud Spanner とは？ ざっくりと説明すると リレーショナル データベースの構造 を持ちながら、 非リレーショナル データベースの水平…"
---

# Spanner のディザスターリカバリ

-   ![](/_astro/noicon.CTHOhNiB_1HMs6A.webp)[shimichan](/authors/shimichan/)
-   公開日：2020年7月17日
-   カテゴリー：[Tech](/categories/tech/)
-   タグ：[#recovery](/tags/recovery/)[#spanner](/tags/spanner/)

![Spanner のディザスターリカバリ](/_astro/og-image.DDSiu2Ny_XHpDd.webp)

boom boom! Hello Cloud!  
どうも、しみちゃんでございます。  
今回は Spanner のディザスターリカバリ対応方法について、紹介をしたいと思います。

## Cloud Spanner とは？

ざっくりと説明すると **リレーショナル データベースの構造** を持ちながら、 **非リレーショナル データベースの水平スケーリング機能** を備えたフルマネージドな Google のデータベースサービスです。

公式ドキュメント: [Cloud Spanner](https://cloud.google.com/spanner)

### バックアップについて

実は、下記の spanner のバックアップでは **リージョンをまたいで復元ができません**

Plain textcontent\_copy

```
 gcloud spanner backups create ${instance}-${database}-${database} \
      --instance=${instance} \
      --database=${database} \
      --retention-period=${period} \
      --async
```

実行結果

Plain textcontent\_copy

```
# gcloud spanner databases restore --async \
  --destination-instance=shimizu-us-recovery \
  --destination-database=recovery_test \
  --source-instance=shimizu \
   --source-backup=shimizu-recovery_test-20200717_173700
ERROR: (gcloud.spanner.databases.restore) INVALID_ARGUMENT: Cannot create database projects/grasys-dev/instances/shimizu-us-recovery/databases/recovery_test from backup projects/grasys-dev/instances/shimizu/backups/shimizu-recovery_test-20200717_173700 because the backup and the database are in instances with different instance configurations. Database instance instance_configuration: projects/grasys-dev/instanceConfigs/nam3, Backup instance instance_configuration: projects/grasys-dev/instanceConfigs/asia1.
```

理由は **同じインスタンス構成じゃない** からです。

リージョンが違うと、動作しないようです。

> The new database must be in the same project as the backup and be in an instance with the same instance configuration as the backup. For example, if a backup is in an instance configured us-west3, it can be restored to any instance in the project that is also configured us-west3

引用: [https://cloud.google.com/spanner/docs/backup#restore](https://cloud.google.com/spanner/docs/backup#restore)

### じゃあ、どうすんねん

リージョンを跨いでバックアップを行うにはどうするか、というと **Data Flow** を使用して **GCS** へバックアップを行います

### ディザスターリカバリ

では早速、バックアップを作成して、別のリージョンに Spanner インスタンスにインポートを行ってみましょう

### コマンドラインから

GCPコンソールからでも簡単にできるのですが、 自動化したいので、 gcloud コマンドで実行を試してみましょう。

コマンドラインから実行するには、 `gcloud dataflow` コマンドを使用します。

DataFlowのテンプレートを使用してみます。

参照: [Cloud Spanner to Cloud Storage Avro](https://cloud.google.com/dataflow/docs/guides/templates/provided-batch#cloud_spanner_to_gcs_avro)

Bashcontent\_copy

```
gcloud dataflow jobs run [JOB_NAME] \
  --gcs-location='gs://dataflow-templates/[VERSION]/Cloud_Spanner_to_GCS_Avro' \
  --region=[DATAFLOW_REGION] \
  --parameters='instanceId=[YOUR_INSTANCE_ID],databaseId=[YOUR_DATABASE_ID],outputDir=[YOUR_GCS_DIRECTORY]'
```

ほうほう。さっそく実行してみましょう

Plain textcontent\_copy

```
# gcloud dataflow jobs run shimizu-recovery_test-20200717_192644 \
  --gcs-location=gs://dataflow-templates/latest/Cloud_Spanner_to_GCS_Avro \
  --region=us-east1 \
  --parameters=instanceId=shimizu,databaseId=recovery_test,outputDir=gs://shimizu_recv_test/shimizu/recovery_test
createTime: '2020-07-17T10:26:47.326894Z'
currentStateTime: '1970-01-01T00:00:00Z'
id: 2020-07-17_03_26_46-12461804407310305089
location: us-east1
name: shimizu-recovery_test-20200717_192644
projectId: grasys-dev
startTime: '2020-07-17T10:26:47.326894Z'
type: JOB_TYPE_BATCH
```

確認

Plain textcontent\_copy

```
# gcloud dataflow jobs list --region=us-east1
JOB_ID                                    NAME                                   TYPE   CREATION_TIME        STATE    REGION
2020-07-17_03_26_46-12461804407310305089  shimizu-recovery_test-20200717_192644  Batch  2020-07-17 10:26:47  Running  us-east1
2020-07-17_03_22_46-13106556587330949927  shimizu-recovery_test-20200717_192243  Batch  2020-07-17 10:22:47  Done     us-east1
```

うまく動いていそうですね。

Plain textcontent\_copy

```
# gcloud dataflow jobs list --region=us-east1
JOB_ID                                    NAME                                   TYPE   CREATION_TIME        STATE  REGION
2020-07-17_03_26_46-12461804407310305089  shimizu-recovery_test-20200717_192644  Batch  2020-07-17 10:26:47  Done   us-east1
2020-07-17_03_22_46-13106556587330949927  shimizu-recovery_test-20200717_192243  Batch  2020-07-17 10:22:47  Done   us-east1
```

では、復旧してみましょう。

GCPコンソールから簡単にできるので、それを試してみます。

![gcloud dataflow jobs listコマンドの実行結果](/_astro/01-01-1-1ff86917.CcSZ7RFl_1659oW.webp)

ジョブが完了したら、成功しているかチェックして終わりです

##### おまけ

ちなみに、コマンドラインから実行するには下記のテンプレートジョブを使用すると良いようです。

Bashcontent\_copy

```
gcloud dataflow jobs run [JOB_NAME] \
  --gcs-location='gs://dataflow-templates/[VERSION]/GCS_Avro_to_Cloud_Spanner' \
  --region=[DATAFLOW_REGION] \
  --parameters='instanceId=[YOUR_INSTANCE_ID],databaseId=[YOUR_DATABASE_ID],inputDir=[YOUR_GCS_DIRECTORY]'
```

参照: [Cloud Storage Avro to Cloud Spanner](https://cloud.google.com/dataflow/docs/guides/templates/provided-batch#gcs_avro_to_cloud_spanner)

## この記事を書いた人

[![](/_astro/shimichan.B6KTYu8c_Z1NDayG.webp)](/authors/shimichan/)

### [shimichan](/authors/shimichan/)

shimichanのプロフィールと執筆記事をご覧いただけます。

[プロフィールと記事一覧](/authors/shimichan/)