排定 Filestore Enterprise 快照


本文說明如何使用 Cloud Scheduler 排定 Filestore 快照的建立時間,協助您保護快照。

本文適用於 IT 決策者、備份管理員和災難復原 (DR) 管理員,協助他們瞭解或建立排程 Filestore 快照。

目標

  • 設定 Cloud Run 函式和 JSON 設定檔。
  • 建立 Cloud Scheduler 工作。
  • 建立並標記排程 Filestore 快照。
  • 監控快照建立程序。

費用

在本文件中,您會使用下列 Google Cloud的計費元件:

如要根據預測用量估算費用,請使用 Pricing Calculator

初次使用 Google Cloud 的使用者可能符合免費試用資格。

完成本教學課程後,您可以刪除自己建立的資源,避免資源繼續產生費用。詳情請參閱「清除」。

事前準備

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  3. Verify that billing is enabled for your Google Cloud project.

  4. Enable the Filestore, Cloud Scheduler, Cloud Functions, App Engine, and Cloud Build APIs.

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. Learn how to grant roles.

    Enable the APIs

  5. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  6. Verify that billing is enabled for your Google Cloud project.

  7. Enable the Filestore, Cloud Scheduler, Cloud Functions, App Engine, and Cloud Build APIs.

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. Learn how to grant roles.

    Enable the APIs

  8. 部署模式

    下圖顯示支援 Filestore 快照 Cloud Run 函式的部署模型。

    部署模型,支援 Filestore 快照 Cloud Run 函式。

    Cloud Scheduler 工作會與 Cloud Run 函式協調,排定及產生 Filestore 快照和記錄檔。快照會儲存在 Filestore 共用區。

    已知限制

    準備為 Filestore 執行個體設定快照排程器時,請考量下列限制:

    • 即使排程器快照數量超過保留政策設定檔中定義的數量,或保留政策設定檔更新後保留的快照數量較少,此函式也只會在必要時刪除單一快照。
    • 如果您在 JSON 設定檔中減少 snapshots 的數值,系統不會自動刪除執行個體中的多餘快照。變更設定後,您必須手動刪除多餘的快照。
    • 由於快照 ID 的長度上限為 75 個字元,因此保留政策名稱的長度上限為 50 個字元。

    準備環境

    在本節中,您將設定環境變數、複製存放區,並建立本教學課程所需的應用程式和檔案。

    設定環境變數

    • 請在 Cloud Shell 中輸入下列指令:
    gcloud components update
    
    • 在 Cloud Shell 中建立下列變數:

      export PROJECT_ID=PROJECT_ID
      export GCP_REGION=GCP_REGION
      export APP_ENGINE_REGION=APP_ENGINE_REGION
      export FUNCTION_NAME=FUNCTION_NAME
      export SCHEDULER_NAME=SCHEDULER_NAME
      export SCHEDULER_EXPRESSION="SCHEDULER_EXPRESSION"
      export SCHEDULER_TZ=SCHEDULER_TZ
      export SERVICE_ACCOUNT_NAME=SERVICE_ACCOUNT_NAME
      

      更改下列內容:

      • PROJECT_ID:您要安裝企業執行個體、Cloud Run 函式和 Cloud Scheduler 的 Google Cloud 專案 ID。
      • :您要安裝企業例項、Cloud Run 函式和 Cloud Scheduler 的 Google Cloud 區域。GCP_REGION
      • APP_ENGINE_REGION:App Engine 位置清單中的區域,您要在該處安裝 App Engine。請考量下列需求條件:
        • 一個 Google Cloud 專案只能有一個 App Engine 執行個體,且之後無法變更 App Engine 區域。
        • Cloud Scheduler 工作和 App Engine 執行個體必須位於相同區域。
          • 舉例來說,如果 Cloud Scheduler 資源使用 us-central1,請使用對應的 App Engine us-central
          • App Engine 會對部分地區採用獨特的命名慣例。 詳情請參閱 App Engine 位置清單
      • FUNCTION_NAME:您要為 Cloud Run 函式指定的名稱。
      • SCHEDULER_NAME:您要授予 Cloud Scheduler 的名稱。
      • SCHEDULER_EXPRESSION:Cloud Scheduler Cron 運算式,例如 10 0 * * *。詳情請參閱「設定 Cron 工作時間表」。
      • SCHEDULER_TZ:Cloud Scheduler 的時區,格式為 tz 資料庫時區清單中的名稱,例如 America/Los_Angeles
      • SERVICE_ACCOUNT_NAME:新建立的服務帳戶名稱,例如 scheduler-sa

    建立服務帳戶

    在本節中,您將建立專屬服務帳戶,以便建立及管理 Cloud Run 函式和 Cloud Scheduler 工作。

    執行這項程序時,服務帳戶需要下列權限:

    • file.instances.get
    • file.snapshots.list
    • file.snapshots.create
    • file.operations.get
    • file.snapshots.delete
    • cloudfunctions.functions.invoke

    詳情請參閱「Filestore IAM 權限」或「Cloud Run 函式 IAM 權限」。

    如要建立服務帳戶,請按照下列步驟操作:

    1. 在 Cloud Shell 中,為快照排程器建立專用角色,並授予必要權限:

      gcloud iam roles create snapshot_scheduler --project $PROJECT_ID --permissions file.instances.get,file.snapshots.list,file.snapshots.create,file.operations.get,file.snapshots.delete,logging.logEntries.create,cloudfunctions.functions.invoke --stage GA
      
    2. 建立服務帳戶:

      gcloud iam service-accounts create $SERVICE_ACCOUNT_NAME --project $PROJECT_ID
      
    3. 將角色繫結至服務帳戶:

      gcloud projects add-iam-policy-binding $PROJECT_ID --member serviceAccount:$SERVICE_ACCOUNT_NAME@$PROJECT_ID.iam.gserviceaccount.com --role projects/$PROJECT_ID/roles/snapshot_scheduler
      

    建立企業執行個體

    1. 如果您尚未為要使用的企業執行個體建立環境變數,請在 Cloud Shell 中建立下列變數:

      export FILESTORE_INSTANCE_ID=FILESTORE_INSTANCE_ID
      export FILESTORE_SHARE_NAME=SHARE_NAME
      export FILESTORE_SHARE_SIZE=NUMERIC_IN_GB
      export NETWORK_NAME=VPC_NAME
      

      更改下列內容:

      • FILESTORE_INSTANCE_ID:企業執行個體名稱
      • SHARE_NAME:企業執行個體共用名稱
      • NUMERIC_IN_GB:企業執行個體共用大小,介於 1024 到 10240 之間,以 256 為增量
      • VPC_NAME:要安裝企業版執行個體的虛擬私有雲網路名稱
    2. 如果沒有,請先建立 Filestore 執行個體:

      gcloud filestore instances create $FILESTORE_INSTANCE_ID --network name=$NETWORK_NAME --file-share=capacity=$FILESTORE_SHARE_SIZE,name=$FILESTORE_SHARE_NAME --tier ZONAL --project=$PROJECT_ID --location=$GCP_REGION
      

    如需詳細資訊,請參閱建立執行個體

    建立 App Engine

    在本節中,您將為Google Cloud 專案建立 App Engine。這樣您就能在本教學課程稍後建立 Cloud Scheduler 工作。

    一個 Google Cloud 專案只能有一個 App Engine,且之後無法變更區域。

    • 如果沒有,請在 Cloud Shell 中建立 App Engine:

      gcloud app create --region=$APP_ENGINE_REGION --project=$PROJECT_ID
      

    建立 JSON 設定檔

    由於 Cloud Run functions 的限制,JSON 設定檔最多可有八個 Filestore 執行個體。如要排定超過八個 Filestore 執行個體的快照建立時間,請使用額外的 Cloud Scheduler 工作。

    • 在 Cloud Shell 中建立名為 request.json 的新檔案,並根據您的環境編輯內容:

      {
                  "retention_policy": "RETENTION_NAME",
                  "instances": [
                      {"instance_path": "projects/PROJECT_ID/locations/GCP_REGION/instances/FILESTORE_INSTANCE_ID/",
                          "snapshots": NUMBER_OF_SNAPSHOTS
                      }
                  ]
      }
      

      更改下列內容:

      • RETENTION_NAME:快照名稱 保留類型,會成為快照名稱的一部分,例如 daily
      • NUMBER_OF_SNAPSHOTS:快照數量。

    複製存放區

    複製實驗室存放區,然後輸入目錄:

    1. 在 Cloud Shell 中,複製實驗室存放區:

      git clone https://github.com/GoogleCloudPlatform/Filestore-Snapshot-Scheduler
      

      輸出結果會與下列內容相似:

      Cloning into Filestore-Snapshot-Scheduler
      
    2. 輸入目錄:

      cd Filestore-Snapshot-Scheduler
      

    建立 Cloud Run 函式

    在 Cloud Shell 中,建立 Cloud Run 函式解決方案:

    gcloud functions deploy $FUNCTION_NAME --region $GCP_REGION --runtime=python39 --trigger-http --source scheduler/ --timeout 540 --service-account $SERVICE_ACCOUNT_NAME@$PROJECT_ID.iam.gserviceaccount.com --entry-point main --project $PROJECT_ID
    

    例如:

    gcloud functions deploy scheduler_func --region us-central1 --runtime=python39 --trigger-http --source scheduler/ --timeout 540 --service-account scheduler-sa@project1.iam.gserviceaccount.com --entry-point main --project project1
    

    建立 Cloud Scheduler 工作

    Cloud Scheduler 工作必須位於與 App Engine 區域對應的區域。

    在 Cloud Shell 中,使用相同 Filestore 執行個體上的一或多個排程器,建立 Cloud Scheduler 工作:

    gcloud scheduler jobs create http $SCHEDULER_NAME --schedule "$SCHEDULER_EXPRESSION" --uri "https://$GCP_REGION-$PROJECT_ID.cloudfunctions.net/$FUNCTION_NAME"  --http-method POST --message-body-from-file ../request.json --oidc-service-account-email $SERVICE_ACCOUNT_NAME@$PROJECT_ID.iam.gserviceaccount.com --time-zone $SCHEDULER_TZ --project $PROJECT_ID
    

    在下列範例中,每日排程會在晚上 8 點執行:

    gcloud scheduler jobs create http daily_scheduler --schedule "0 20 * * *" --uri "https://us-central1-project1.cloudfunctions.net/scheduler_func"  --http-method POST --message-body-from-file ../request.json --oidc-service-account-email scheduler-sa@project1.iam.gserviceaccount.com --time-zone America/Los_angeles --project project1
    

    驗證排定的 Filestore 快照

    1. 前往 Google Cloud 控制台的「Cloud Scheduler」頁面。

      前往 Cloud Scheduler

    2. 在您建立的工作資料列中,依序按一下「 Actions」(動作) >「Force a job run」(強制執行工作)

    3. 按一下 [更新]

      • 如果工作順利執行,「上次執行結果」欄會顯示上次成功執行工作時的時間戳記。
      • 如果工作失敗,請按照下列步驟操作:

        1. 前往「Cloud functions」頁面。

          前往 Cloud Functions 頁面

        2. 在您建立的函式資料列中,依序點選「 Actions」(動作) >「View logs」(查看記錄)

        3. 找出錯誤訊息,並視情況排解問題。

    4. 前往 Google Cloud 控制台的「Filestore instances」(Filestore 執行個體) 頁面

    5. 在「Instances」(執行個體) 清單中選取執行個體,並確認該執行個體有符合下列命名格式的快照:

      sched-RETENTION_NAME-DATE-TIME
      

      例如:sched-daily-20220315-120640

    清除所用資源

    清除所用資源

    如要避免系統向您的 Cloud Platform 帳戶收取本教學課程所用資源的費用,請刪除含有相關資源的專案。

    刪除專案

    1. In the Google Cloud console, go to the Manage resources page.

      Go to Manage resources

    2. In the project list, select the project that you want to delete, and then click Delete.
    3. In the dialog, type the project ID, and then click Shut down to delete the project.

    後續步驟