使用 gcloud 指令建立GCE快照排程套用到磁碟小記
今天來實作 使用 gcloud 指令建立GCE快照排程並套用到 GCE 上面的磁碟
想法: 想對專案的 GCE 機器建立快照排程, 套用到磁碟以防萬一
建立快照排程
- 這邊要注意自已 Google SDK 版本是否夠新? 之前我環境 google cloud sdk 245 就沒有支援, 所以這次就順手把他升級到 google cloud sdk 271.
- 升級方式: gcloud components update
# gcloud compute resource-policies create snapshot-schedule daily-3day-1400 --max-retention-days 3 --start-time 14:00 --daily-schedule --region asia-east1 --on-source-disk-delete keep-auto-snapshots --snapshot-labels env=lab,media=images --storage-location asia-east1 --project sakanatest
- snapshot-schedule 只能用小寫以及-
- --on-source-disk-delete
- keep-auto-snapshots: keep automatically-created snapshots when the source disk is deleted. This is the default behavior.
- 這邊的 start-time 是以 UTC 時間為主, 要找個時間看看如何設定 local time
列出排程
# gcloud compute resource-policies list --project sakanatest
NAME DESCRIPTION REGION CREATION_TIMESTAMP
daily-3day-1400 https://www.googleapis.com/compute/v1/projects/sakanatest/regions/asia-east1 2019-11-16T00:48:02.826-08:00
接下來就是將 GCE 上面磁碟套用這個快照排程
# gcloud compute disks add-resource-policies test20191114 --resource-policies daily-3day-1400 --zone asia-east1-b --project sakanatest
Updated [https://www.googleapis.com/compute/v1/projects/sakanatest/zones/asia-east1-b/disks/test20191114].
- test20191114 是磁碟名稱
接下來想到一個問題, 如果哪天排程時間想要改變該如何??
做法
- 建立一個新的快照排程
- 將舊的快照排程從磁碟卸載
- 將新的快照排程加到磁碟
接下來進行實驗
建立一個 保留5 天, 18:00 建立快照的排程
# gcloud compute resource-policies create snapshot-schedule daily-5day-1800 --max-retention-days 5 --start-time 18:00 --daily-schedule --region asia-east1 --on-source-disk-delete keep-auto-snapshots --snapshot-labels env=lab,media=images --storage-location asia-east1 --project sakanatest
嘗試套用到剛剛的磁碟, 會得到錯誤訊息, 因為一次只能套用一個快照排程
# gcloud compute disks add-resource-policies test20191114 --resource-policies daily-5day-1800 --zone asia-east1-b --project sakanatest
ERROR: (gcloud.compute.disks.add-resource-policies) Could not fetch resource:
- Invalid resource usage: 'Disk already has resource policy attached. Attaching multiple policies are not supported.'.
將剛剛的磁碟移除舊的快照排程
# gcloud compute disks remove-resource-policies test20191114 --resource-policies daily-3day-1400 --zone asia-east1-b --project sakanatest
Updated [https://www.googleapis.com/compute/v1/projects/sakanatest/zones/asia-east1-b/disks/test20191114].
將新的快照排程加入
# gcloud compute disks add-resource-policies test20191114 --resource-policies daily-5day-1800 --zone asia-east1-b --project sakanatest
Updated [https://www.googleapis.com/compute/v1/projects/sakanatest/zones/asia-east1-b/disks/test20191114].
這樣就可以了 :)
如果時間到也可以列出快照驗證
列出快照指令
# gcloud compute snapshots list --project sakanatest
先記下來
~ enjoy it
Reference:
- 宏庭科技 Joy 提供基本方向
沒有留言:
張貼留言