您可以建立快照排程,自動定期備份區域性和可用區永久磁碟和 Google Cloud Hyperdisk。快照排程是備份 Compute Engine 工作負載的最佳做法。
如果您想建立快照排程,以便在備份時擷取應用程式資料的狀態 (也稱為應用程式一致、guest-flush 或 VSS 快照),請參閱「建立與 Linux 應用程式一致的磁碟快照」或「建立與 Windows 應用程式一致的磁碟快照」。
如要進一步瞭解快照排程屬性,請參閱「快照排程屬性」。
事前準備
- 請參閱限制,瞭解快照排程。
-
如果尚未設定,請設定驗證機制。「驗證」是指驗證身分,以便存取 Google Cloud 服務和 API 的程序。如要在本機開發環境中執行程式碼或範例,您可以選取下列任一選項,對 Compute Engine 進行驗證:
Select the tab for how you plan to use the samples on this page:
Console
When you use the Google Cloud console to access Google Cloud services and APIs, you don't need to set up authentication.
gcloud
-
After installing the Google Cloud CLI, initialize it by running the following command:
gcloud init
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
- Set a default region and zone.
Go
To use the Go samples on this page in a local development environment, install and initialize the gcloud CLI, and then set up Application Default Credentials with your user credentials.
- Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
If you're using a local shell, then create local authentication credentials for your user account:
gcloud auth application-default login
You don't need to do this if you're using Cloud Shell.
If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.
For more information, see Set up authentication for a local development environment.
Java
To use the Java samples on this page in a local development environment, install and initialize the gcloud CLI, and then set up Application Default Credentials with your user credentials.
- Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
If you're using a local shell, then create local authentication credentials for your user account:
gcloud auth application-default login
You don't need to do this if you're using Cloud Shell.
If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.
For more information, see Set up authentication for a local development environment.
Node.js
To use the Node.js samples on this page in a local development environment, install and initialize the gcloud CLI, and then set up Application Default Credentials with your user credentials.
- Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
If you're using a local shell, then create local authentication credentials for your user account:
gcloud auth application-default login
You don't need to do this if you're using Cloud Shell.
If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.
For more information, see Set up authentication for a local development environment.
Python
To use the Python samples on this page in a local development environment, install and initialize the gcloud CLI, and then set up Application Default Credentials with your user credentials.
- Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
If you're using a local shell, then create local authentication credentials for your user account:
gcloud auth application-default login
You don't need to do this if you're using Cloud Shell.
If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.
For more information, see Set up authentication for a local development environment.
REST
To use the REST API samples on this page in a local development environment, you use the credentials you provide to the gcloud CLI.
After installing the Google Cloud CLI, initialize it by running the following command:
gcloud init
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
For more information, see Authenticate for using REST in the Google Cloud authentication documentation.
-
必要角色和權限
如要取得建立快照排程所需的權限,請要求管理員授予您專案的下列 IAM 角色:
-
Compute 執行個體管理員 (v1) (
roles/compute.instanceAdmin.v1
) -
如要連線至可以服務帳戶形式執行的 VM,請按照下列步驟操作:
服務帳戶使用者 (v1) (
roles/iam.serviceAccountUser
)
如要進一步瞭解如何授予角色,請參閱「管理專案、資料夾和機構的存取權」。
這些預先定義的角色包含建立快照排程所需的權限。如要查看確切的必要權限,請展開「必要權限」部分:
所需權限
如要建立快照排程,必須具備下列權限:
-
如要建立快照排程:
compute.resourcePolicies.create
專案或機構的 -
如要將快照排程附加至磁碟,請按照下列步驟操作:
-
磁碟的
compute.disks.addResourcePolicies
權限 -
資源政策的
compute.resourcePolicies.use
-
磁碟的
-
如要建立具有快照排程的磁碟,請按照下列步驟操作:
-
compute.disks.create
專案 -
compute.resourcePolicies.create
專案 -
磁碟的
compute.disks.addResourcePolicies
權限
-
建立快照排程的簡介
建立快照排程時,您會建立資源政策,可套用到一或多個永久磁碟或 Hyperdisk 磁碟區。
您可以透過下列方式建立快照時間表:
使用加密功能搭配快照排程
如果磁碟使用客戶自行管理的加密金鑰 (CMEK),當您使用快照排程建立該磁碟的快照時,系統會自動使用相同的金鑰對所有建立的快照進行加密。
您無法在使用客戶提供的加密金鑰 (CSEK) 的磁碟上使用快照排程。
建立快照排程
您可以使用 Google Cloud 主控台、Google Cloud CLI 或 REST,為磁碟建立快照排程。您必須在與磁碟相同的地區中建立快照排程。舉例來說,如果您的磁碟位於 us-west1-a
區域,則必須在 us-west1
地區建立快照排程。不過,您可以選擇將快照排程產生的快照儲存在其他位置。
主控台
-
前往 Google Cloud 控制台的「VM instances」(VM 執行個體) 頁面。
前往「VM 執行個體」
其餘步驟會自動顯示在 Google Cloud 控制台中。 - 選取含有 VM 執行個體的專案。
- 在「Name」(名稱) 欄中,按一下要建立快照排程的永久磁碟所在的 VM 名稱。
-
在
「Storage」(儲存空間) ,按一下要建立快照排程的「Boot disk」(開機磁碟) 或「Additional disk」(其他磁碟) 名稱。 - 按一下「編輯」圖示 。您可能需要按一下 「更多動作」選單,然後點選 「編輯」。
- 在「Snapshot schedule」(快照排程) 中選擇「Create a schedule」(建立排程)。
-
在「Name」(名稱) 中,為快照排程輸入下列其中一個名稱:
boot-disk-snapshot-schedule
attached-persistent-disk-snapshot-schedule
-
在「Location」(位置) 部分,選擇快照儲存位置。系統會自動選取快照設定中指定的預先定義或自訂預設位置。如要覆寫快照設定,將快照儲存在自訂儲存位置,請執行下列步驟:
-
選擇快照的儲存位置類型。
-
若選擇「Multi-regional」(多區域),費用較高但提供較高可用性。
- 若選擇「Regional snapshots」(區域快照),將能進一步控管資料的實際位置,而且費用也較低。
-
若選擇「Multi-regional」(多區域),費用較高但提供較高可用性。
-
在「Select location」(選取位置) 欄位,選擇要使用的特定區域或多區域。如要使用離來源磁碟最近的區域或多區域,請選取「Based on disk's location」(以磁碟位置為準)。
-
- 按一下「Create」(建立) 即可完成快照排程建立作業。
- 如要將這個快照排程附加至永久磁碟,請按一下「Save」(儲存)。
gcloud
如要為磁碟排定全域範圍的快照,請使用
gcloud compute resource-policies create snapshot-schedule
指令。並將排程頻率設定為每小時、每日或每週。gcloud compute resource-policies create snapshot-schedule SCHEDULE_NAME \ --description "SCHEDULE_DESCRIPTION" \ --max-retention-days MAX_RETENTION_DAYS \ --start-time START_TIME \ --hourly-schedule SNAPSHOT_INTERVAL \ --daily-schedule \ --weekly-schedule SNAPSHOT_INTERVAL or --weekly-schedule-from-file FILE_NAME \ --on-source-disk-delete DELETION_OPTION \ --storage-location=STORAGE_LOCATION
(預先發布) 如要為磁碟安排地區性快照,請使用
gcloud compute resource-policies create snapshot-schedule
指令並指定快照地區。gcloud beta compute resource-policies create snapshot-schedule SCHEDULE_NAME \ --description "SCHEDULE_DESCRIPTION" \ --max-retention-days MAX_RETENTION_DAYS \ --start-time START_TIME \ --hourly-schedule SNAPSHOT_INTERVAL \ --daily-schedule \ --weekly-schedule SNAPSHOT_INTERVAL or --weekly-schedule-from-file FILE_NAME \ --on-source-disk-delete DELETION_OPTION \ --storage-location=STORAGE_LOCATION \ --region REGION \ --snapshot-region SNAPSHOT_REGION
更改下列內容:
SCHEDULE_NAME
:快照排程的名稱。SCHEDULE_DESCRIPTION
:快照排程的說明。請在說明內容的前後加上引號。REGION
:快照排程資源政策的位置。SNAPSHOT_REGION
:排定快照的範圍。MAX_RETENTION_DAYS
:保留快照的天數。舉例來說,如果值為
3
,則代表快照會保留 3 天後刪除。您必須使用1
以上的值。START_TIME
:世界標準時間時區的開始時間。時間必須從每個小時的整點開始,例如:
- 太平洋時間下午 2:00 必須指定為
22:00
。 - 如果您將開始時間設為
22:13
,則會收到錯誤訊息。
如果您使用
--weekly-schedule-from-file
旗標,並在檔案中指定開始時間,則不需要加入這個旗標。- 太平洋時間下午 2:00 必須指定為
SNAPSHOT_INTERVAL
:建立連續快照之間的間隔。快照頻率標記hourly-schedule
、daily-schedule
、weekly-schedule
和weekly-schedule-from-file
是相互排斥的,您只能為快照排程使用其中一種。- 如要設定每日時間表,請加入
--daily-schedule
旗標,但不附加任何值。 - 設定每小時排程,並將
--hourly-schedule
旗標設為介於 1 到 23 的整數值。如要每天在同一時間產生快照,請選擇一個能夠將 24 整除的數字。例如,將--hourly-schedule
設為12
,即代表系統每 12 小時會產生一次快照。 - 設定每週排程,並將
--weekly-schedule
標記設為您要建立快照的星期幾。您必須填入星期幾,但不必區分大小寫。舉例來說,如要每週五備份磁碟,指令中就必須包含--weekly-schedule=friday
。 設定進階每週排程,指定一週中的不同日子和不同開始時間,方法是加入
--weekly-schedule-from-file
標記。請將FILE_NAME
替換為包含每週快照排程的檔案名稱。您可以使用檔案指定每週不同日子和開始時間,但無法直接在指令列上指定多個每週排程。舉例來說,您的檔案可能會指定星期一和星期三的兩個每週排程,但您無法在指令列上複製這項設定:[ {"day": "MONDAY", "startTime": "04:00"}, {"day": "WEDNESDAY", "startTime": "02:00"} ]
如果檔案中包含開始時間,則不需要在指令列上設定
--start-time
標記。時間表會使用世界標準時間時區。
- 如要設定每日時間表,請加入
DELETION_OPTION
:決定在來源磁碟遭到刪除時,系統會如何處理快照。如果您想要保留所有產生的快照,可以省略這個標記。否則,請指定apply-retention-policy
,以便使用保留政策中的設定。STORAGE_LOCATION
:選填欄位:儲存位置。如果您省略這個標記,系統會使用預設儲存位置。
範例
在下列所有範例中:
- 磁碟刪除規則皆包含在內。
--on-source-disk-delete
旗標的預設值設為keep-auto-snapshots
,會永久保留所有自動產生的快照。另一種做法則是將這個旗標設為apply-retention-policy
,即可使用您的快照保留政策。 - 儲存位置已手動設為
US
,因此所有產生的快照都會儲存在美國多地區位置。 - 標籤
env=dev
和media=images
會套用至所有產生的快照。 - 保留政策的設定為 10 天。
每小時排程:在本範例中,快照排程會在世界標準時間 22:00 (太平洋標準時間 14:00) 開始作業,而且每 4 小時執行一次。
gcloud compute resource-policies create snapshot-schedule hourly-schedule1 \
--description "MY HOURLY SNAPSHOT SCHEDULE" \
--max-retention-days 10 \
--start-time 22:00 \
--hourly-schedule 4 \
--region us-west1 \
--on-source-disk-delete keep-auto-snapshots \
--snapshot-labels env=dev,media=images \
--storage-location US
每日排程:在本範例中,快照排程會在世界標準時間 22:00 (太平洋標準時間 14:00) 開始作業,而且會在每天的同一時間執行。--daily-schedule
旗標必須存在,但沒有相關值。
gcloud compute resource-policies create snapshot-schedule daily-schedule2 \
--description "MY DAILY SNAPSHOT SCHEDULE" \
--max-retention-days 10 \
--start-time 22:00 \
--daily-schedule \
--region us-west1 \
--on-source-disk-delete keep-auto-snapshots \
--snapshot-labels env=dev,media=images \
--storage-location US
每週排程:在本範例中,快照排程會在世界標準時間 22:00 (太平洋標準時間 14:00) 開始作業,而且會在每週的星期二執行。
gcloud compute resource-policies create snapshot-schedule weekly-schedule3 \
--description "MY WEEKLY SNAPSHOT SCHEDULE" \
--max-retention-days 10 \
--start-time 22:00 \
--weekly-schedule tuesday \
--region us-west1 \
--on-source-disk-delete keep-auto-snapshots \
--snapshot-labels env=dev,media=images \
--storage-location US
Go
Java
Node.js
Python
REST
如要為全域範圍的快照建立快照排程,請建構對
resourcePolicies.insert
的POST
要求。您必須提供快照排程名稱和快照頻率。您也可以手動指定快照儲存位置,並在要求中新增資源標籤。
根據預設,
onSourceDiskDelete
參數會設為keepAutoSnapshots
。這代表如果來源磁碟遭到刪除,系統會無限期保留該磁碟自動產生的快照。或者,您也可以將標記設為applyRetentionPolicy
,以套用您的保留政策。下列範例設定的每日快照排程每天會在世界標準時間 12:00 (太平洋標準時間 04:00) 開始作業,而且會每天重複執行。此外,此範例同時設定了 5 天的保留政策,因此系統會在 5 天後自動移除快照。
POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/regions/REGION/resourcePolicies { "name": "SCHEDULE_NAME", "description": "SCHEDULE_DESCRIPTION", "snapshotSchedulePolicy": { "schedule": { "dailySchedule": { "startTime": "12:00", "daysInCycle": "1" } }, "retentionPolicy": { "maxRetentionDays": "5" }, "snapshotProperties": { "guestFlush": "False", "labels": { "env": "dev", "media": "images" }, "storageLocations": "STORAGE_LOCATION" } } }
(預先發布版) 如要為區域範圍的快照建立快照排程,請建構目標為
resourcePolicies.insert
的POST
要求,並指定快照區域。POST https://compute.googleapis.com/compute/beta/projects/PROJECT_ID/regions/REGION/resourcePolicies { "name": "SCHEDULE_NAME", "description": "SCHEDULE_DESCRIPTION", "snapshotSchedulePolicy": { "schedule": { "dailySchedule": { "startTime": "12:00", "daysInCycle": "1" } }, "retentionPolicy": { "maxRetentionDays": "5" }, "snapshotProperties": { "guestFlush": "False", "region": "SNAPSHOT_REGION" } } }
更改下列內容:
PROJECT_ID
:專案名稱REGION
:快照排程資源政策的位置SNAPSHOT_REGION
:排定快照的範圍SCHEDULE_DESCRIPTION
:快照排程的說明SCHEDULE_NAME
:快照排程的名稱STORAGE_LOCATION
:選填欄位:儲存位置。如果您省略這個標記,系統會使用預設儲存位置。
同樣地,您可以建立每週或每月排程。如要進一步瞭解每週或每月排程的設定,請參閱 resourcePolicies.insert
方法。
舉例來說,下列要求會建立每週排程,在星期二的 9:00 (世界標準時間) 執行作業。
POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/regions/REGION/resourcePolicies
{
"name": "SCHEDULE_NAME",
"description": "SCHEDULE_DESCRIPTION",
"snapshotSchedulePolicy": {
"schedule": {
"weeklySchedule": {
"dayOfWeeks": [
{
"day": "Tuesday",
"startTime": "9:00"
}
]
}
},
"retentionPolicy": {
"maxRetentionDays": "5"
},
"snapshotProperties": {
"guestFlush": "False",
"labels": {
"production": "webserver"
},
"storageLocations": "US"
}
}
}
將快照排程附加至磁碟
建立排程後,請將排程附加至現有的磁碟。使用主控台、gcloud CLI 或 Compute Engine API。
主控台
將快照排程附加至現有的磁碟。
前往 Google Cloud 控制台的「磁碟」頁面。
選取要附加快照排程的磁碟名稱。系統隨即會開啟「管理磁碟」頁面。
在「管理磁碟」頁面中,按一下「編輯」圖示
。您可能需要先按一下「更多動作」 選單。使用「Snapshot schedule」(快照排程) 下拉式選單,將排程加入磁碟,或是建立新的排程。
如要建立新的排程,請按一下 [Create] (建立)。
按一下 [Save] (儲存) 來完成工作。
gcloud
如要將快照排程附加至磁碟,請使用 gcloud disks add-resource-policies
指令。
gcloud compute disks add-resource-policies DISK_NAME \
--resource-policies SCHEDULE_NAME \
--zone ZONE
更改下列內容:
DISK_NAME
:現有磁碟的名稱SCHEDULE_NAME
:快照排程的名稱ZONE
:磁碟的位置
Go
Java
Python
REST
建構目標為 disks.addResourcePolicies
的 POST
要求,以將快照排程附加至現有磁碟。
POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/disks/DISK_NAME/addResourcePolicies
{
"resourcePolicies": [
"regions/REGION/resourcePolicies/SCHEDULE_NAME"
]
}
更改下列內容:
PROJECT_ID
:專案名稱ZONE
:磁碟所在的區域REGION
:建立快照排程的區域DISK_NAME
:磁碟名稱SCHEDULE_NAME
:要套用至這個磁碟的快照排程名稱
建立具有快照排程的磁碟
您可以使用 Google Cloud 控制台或 gcloud CLI 同時建立磁碟和快照排程。
主控台
前往 Google Cloud 控制台的「磁碟」頁面。
按一下 [Create Disk] (建立磁碟)。
完成必填欄位來建立區域或地區磁碟。
在快照排程所在的地區中建立磁碟。
填寫「Snapshot schedule」(快照排程) 的欄位。
使用下拉式選單並填寫欄位以建立排程。
按一下 [Create] (建立) 來建立排程。
按一下 [Create] (建立) 來建立磁碟。
gcloud
使用 gcloud disks create
指令建立區域或地區永久磁碟或 Hyperdisk,並附加快照排程。
gcloud compute disks create DISK_NAME \
--resource-policies SCHEDULE_NAME \
--zone ZONE
更改下列內容:
DISK_NAME
:新磁碟的名稱SCHEDULE_NAME
:快照排程的名稱ZONE
:建立磁碟的位置。磁碟必須位於與快照排程相同的區域。
Go
Java
後續步驟
- 瞭解如何管理快照排程。
- 如要瞭解排程快照的頻率、保留政策和命名規則,請參閱「關於磁碟的快照排程」。
- 瞭解如何從快照建立自訂映像檔。
- 瞭解如何查看記錄。