GCP触ってみた③

# スナップショット(GCE)

### 事前準備
インスタンスを起動する
```shell
gcloud compute instances create gcp --preemptible --zone=us-west1-a --machine-type=e2-micro -q
```

## スナップショットの取得


スナップショットを取得する
```shell
gcloud compute disks snapshot gcp --zone=us-west1-a --description "sample snapshot" --snapshot-names=sample-snapshot
Creating snapshot(s) sample-snapshot...done.
```
30秒ぐらい？でできた　データほぼないとはいえかなり優秀なのではないか

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/704575/09bc592d-0190-e03e-a203-d6a922f45099.png)

GUIから見ても問題なくできているように見える

## スナップショットの複製
スナップショット取るということは複製もやってみなければ

```shell
gcloud compute disks create "clone-instance" --size=10 --zone=asia-east1-a --source-snapshot=sample-snapshot  
WARNING: You have selected a disk size of under [200GB]. This may result in poor I/O performance. For more information, see: https://developers.google.com/compute/docs/disks#performance.
Created [https://www.googleapis.com/compute/v1/projects/<projectID>/zones/asia-east1-a/disks/clone-instance].
NAME: clone-instance
ZONE: asia-east1-a
SIZE_GB: 10
TYPE: pd-standard
STATUS: READY

gcloud compute instances create clone-gcp --zone=asia-east1-a --disk "name=clone-instance,device-name=sample-snapshot,mode=rw,boot=yes"
Created [https://www.googleapis.com/compute/v1/projects/<projectID>/zones/asia-east1-a/instances/clone-gcp].
NAME: clone-gcp
ZONE: asia-east1-a
MACHINE_TYPE: n1-standard-1
PREEMPTIBLE:
INTERNAL_IP: 10.140.0.2
EXTERNAL_IP: 34.80.202.218
STATUS: RUNNING
```

ここよくわからなかったので苦戦しましたが、
```shell
gcloud compute disks create "clone-instance" --size=10 --zone=asia-east1-a --source-snapshot=sample-snapshot
```
上記コマンドでclone-instanceというディスクを作成して、

```shell
gcloud compute instances create clone-gcp --zone=asia-east1-a --disk "name=clone-instance,device-name=sample-snapshot,mode=rw,boot=yes"
```
でclone-gcpというインスタンスをさっき作成したclone-instanceというディスクイメージから作成するよ
という意味だと理解しました。

### GUIでインスタンスが複製されているか確認してみる
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/704575/ee49f8c8-203c-2662-1c89-90fc841ebdfe.png)

おお、いいですね
ゾーンまたいで簡単にインスタンスを複製できるのはクラウドの良さを感じますね

## 後片付け

```shell
#インスタンスの一覧を表示
gcloud compute instances list
NAME: gcp
ZONE: us-west1-a
MACHINE_TYPE: e2-micro
PREEMPTIBLE: true
INTERNAL_IP: 10.138.0.5
EXTERNAL_IP: 35.230.46.48
STATUS: RUNNING

NAME: clone-gcp
ZONE: asia-east1-a
MACHINE_TYPE: n1-standard-1
PREEMPTIBLE:
INTERNAL_IP: 10.140.0.2
EXTERNAL_IP: 34.80.202.218
STATUS: RUNNING

#インスタンス"gcp"を削除
gcloud compute instances delete gcp --zone=us-west1-a
The following instances will be deleted. Any attached disks configured to be auto-deleted will be deleted unless they are attached to any other instances or the `--keep-disks` flag is given and
specifies them for keeping. Deleting a disk is irreversible and any data on the disk will be lost.
 - [gcp] in [us-west1-a]

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

Deleted [https://www.googleapis.com/compute/v1/projects/<projectID>/zones/us-west1-a/instances/gcp].

#インスタンス"clone-gcp"を削除
gcloud compute instances delete clone-gcp --zone=asia-east1-a
The following instances will be deleted. Any attached disks configured to be auto-deleted will be deleted unless they are attached to any other instances or the `--keep-disks` flag is given and
specifies them for keeping. Deleting a disk is irreversible and any data on the disk will be lost.
 - [clone-gcp] in [asia-east1-a]

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

Deleted [https://www.googleapis.com/compute/v1/projects/<projectID>/zones/asia-east1-a/instances/clone-gcp].

# スナップショットの一覧を表示
gcloud compute snapshots list
NAME: sample-snapshot
DISK_SIZE_GB: 10
SRC_DISK: us-west1-a/disks/gcp
STATUS: READY

#スナップショット"sample-snapshot"を削除
gcloud compute snapshots delete sample-snapshot
The following snapshots will be deleted:
 - [sample-snapshot]

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

Deleted [https://www.googleapis.com/compute/v1/projects/<projectID>/global/snapshots/sample-snapshot].
```

### GUIからも確認
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/704575/16334629-f097-844c-4f78-a327c861f26f.png)

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/704575/70daf7e8-681a-2a6d-f913-a5aff060f806.png)

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/704575/92598762-0e0c-f528-0154-b7f4e04316f5.png)

ディスク消してないか

```shell
# ディスクの一覧を表示
gcloud compute disks list
NAME: clone-instance
LOCATION: asia-east1-a
LOCATION_SCOPE: zone
SIZE_GB: 10
TYPE: pd-standard
STATUS: READY
# ディスク"clone-instance"を削除
gcloud compute disks delete clone-instance
Did you mean zone [asia-east1-a] for disk: [clone-instance] (Y/n)?  y

The following disks will be deleted:
 - [clone-instance] in [asia-east1-a]

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

Deleted [https://www.googleapis.com/compute/v1/projects/noble-feat-376216/zones/asia-east1-a/disks/clone-instance].
```

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/704575/688c0a01-a43e-23dc-0a23-b0d77acdd1e6.png)

ヨシ！

今日はここまで、お疲れ様でした。