디스크 스냅샷 일정 만들기


영역 및 리전 Persistent DiskGoogle Cloud Hyperdisk를 정기적으로 자동 백업하는 스냅샷 일정을 만듭니다. Compute Engine 워크로드 백업 권장사항은 스냅샷 일정을 사용하는 것입니다.

백업 시 애플리케이션 데이터 상태를 캡처하는 스냅샷 일정을 만들려는 경우(애플리케이션 일관성, 게스트 플러시, 또는 VSS 스냅샷이라고도 함), 자세한 내용은 Linux 애플리케이션 일관성이 있는 디스크 스냅샷 만들기 또는 Windows 애플리케이션 일관성이 있는 디스크 스냅샷 만들기를 참조하세요.

스냅샷 일정 속성에 관한 자세한 내용은 스냅샷 일정 속성을 참조하세요.

시작하기 전에

  • 스냅샷 일정의 제한사항을 검토합니다.
  • 아직 인증을 설정하지 않았다면 설정합니다. 인증은 Google Cloud 서비스 및 API에 액세스하기 위해 ID를 확인하는 프로세스입니다. 로컬 개발 환경에서 코드 또는 샘플을 실행하려면 다음 옵션 중 하나를 선택하여 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

    1. Install the Google Cloud CLI, then initialize it by running the following command:

      gcloud init
    2. Set a default region and zone.
    3. Go

      로컬 개발 환경에서 이 페이지의 Go 샘플을 사용하려면 gcloud CLI를 설치 및 초기화한 다음 사용자 인증 정보로 애플리케이션 기본 사용자 인증 정보를 설정하세요.

      1. Install the Google Cloud CLI.
      2. To initialize the gcloud CLI, run the following command:

        gcloud init
      3. 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.

      자세한 내용은 다음을 참조하세요: Set up authentication for a local development environment.

      Java

      로컬 개발 환경에서 이 페이지의 Java 샘플을 사용하려면 gcloud CLI를 설치 및 초기화한 다음 사용자 인증 정보로 애플리케이션 기본 사용자 인증 정보를 설정하세요.

      1. Install the Google Cloud CLI.
      2. To initialize the gcloud CLI, run the following command:

        gcloud init
      3. 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.

      자세한 내용은 다음을 참조하세요: Set up authentication for a local development environment.

      Node.js

      로컬 개발 환경에서 이 페이지의 Node.js 샘플을 사용하려면 gcloud CLI를 설치 및 초기화한 다음 사용자 인증 정보로 애플리케이션 기본 사용자 인증 정보를 설정하세요.

      1. Install the Google Cloud CLI.
      2. To initialize the gcloud CLI, run the following command:

        gcloud init
      3. 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.

      자세한 내용은 다음을 참조하세요: Set up authentication for a local development environment.

      Python

      로컬 개발 환경에서 이 페이지의 Python 샘플을 사용하려면 gcloud CLI를 설치 및 초기화한 다음 사용자 인증 정보로 애플리케이션 기본 사용자 인증 정보를 설정하세요.

      1. Install the Google Cloud CLI.
      2. To initialize the gcloud CLI, run the following command:

        gcloud init
      3. 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.

      자세한 내용은 다음을 참조하세요: Set up authentication for a local development environment.

      REST

      로컬 개발 환경에서 이 페이지의 REST API 샘플을 사용하려면 gcloud CLI에 제공한 사용자 인증 정보를 사용합니다.

        Install the Google Cloud CLI, then initialize it by running the following command:

        gcloud init

      자세한 내용은 Google Cloud 인증 문서의 REST 사용을 위한 인증을 참조하세요.

필수 역할 및 권한

스냅샷 일정을 만드는 데 필요한 권한을 얻으려면 관리자에게 프로젝트에 대한 다음 IAM 역할을 부여해 달라고 요청하세요.

역할 부여에 대한 자세한 내용은 프로젝트, 폴더, 조직에 대한 액세스 관리를 참조하세요.

이러한 사전 정의된 역할에는 스냅샷 일정을 만드는 데 필요한 권한이 포함되어 있습니다. 필요한 정확한 권한을 보려면 필수 권한 섹션을 펼치세요.

필수 권한

스냅샷 일정을 만들려면 다음 권한이 필요합니다.

  • 스냅샷 일정 만들기: 프로젝트 또는 조직에 대한 compute.resourcePolicies.create 권한
  • 디스크에 스냅샷 일정 연결:
    • 디스크에 대한 compute.disks.addResourcePolicies 권한
    • 리소스 정책에 대한 compute.resourcePolicies.use 권한
  • 스냅샷 일정이 있는 디스크 만들기:
    • 프로젝트에 대한 compute.disks.create 권한
    • 프로젝트에 대한 compute.resourcePolicies.create 권한
    • 디스크에 대한 compute.disks.addResourcePolicies 권한

커스텀 역할이나 다른 사전 정의된 역할을 사용하여 이 권한을 부여받을 수도 있습니다.

스냅샷 일정 만들기 개요

스냅샷 일정을 만들 때 하나 이상의 Persistent Disk 또는 Hyperdisk 볼륨에 적용할 수 있는 리소스 정책을 만듭니다.

다음과 같은 방법으로 스냅샷 일정을 만들 수 있습니다.

스냅샷 일정에 암호화 사용

디스크에서 고객 관리 암호화 키(CMEK)를 사용하는 경우 스냅샷 일정을 사용하여 해당 디스크의 스냅샷을 만들면 생성된 모든 스냅샷이 동일한 키로 자동으로 암호화됩니다.

고객 제공 암호화 키(CSEK)를 사용하는 디스크에서는 스냅샷 일정을 사용할 수 없습니다.

스냅샷 일정 만들기

Google Cloud 콘솔, Google Cloud CLI 또는 REST를 사용하여 디스크의 스냅샷 일정을 만들 수 있습니다. 디스크가 있는 리전과 동일한 리전에 스냅샷 일정을 만들어야 합니다. 예를 들어 디스크가 us-west1-a 영역에 있다면 us-west1 리전에서 스냅샷 일정을 만들어야 합니다. 그러나 스냅샷 일정에 따라 생성된 스냅샷을 다른 위치에 저장하도록 선택할 수 있습니다.

콘솔

  1. Google Cloud 콘솔에서 VM 인스턴스 페이지로 이동합니다.

    VM 인스턴스로 이동
    남은 단계가 Google Cloud 콘솔에 자동으로 표시됩니다.

  2. VM 인스턴스가 포함된 프로젝트를 선택합니다.
  3. 이름 열에서 스냅샷 일정을 만들 영구 디스크가 있는 VM의 이름을 클릭합니다.
  4. 스토리지에서 스냅샷 일정을 만들 부팅 디스크 또는 추가 디스크의 이름을 클릭합니다.
  5. 수정을 클릭합니다. 작업 더보기 메뉴를 클릭한 후 수정을 클릭해야 할 수 있습니다.
  6. 스냅샷 일정에서 일정 만들기를 선택합니다.
  7. 이름에 다음의 스냅샷 일정 이름 중 하나를 입력합니다.
    • boot-disk-snapshot-schedule
    • attached-persistent-disk-snapshot-schedule
  8. 위치 섹션에서 스냅샷 스토리지 위치를 선택합니다. 스냅샷 설정에 정의된 사전 정의되었거나 맞춤설정된 기본 위치가 자동으로 선택됩니다. 선택적으로 다음을 수행해서 스냅샷 설정을 무효화하고 커스텀 스토리지 위치에 스냅샷을 저장할 수 있습니다.

    1. 스냅샷에 대해 원하는 스토리지 위치 유형을 선택합니다.

      • 더 높은 가용성을 위해서는 더 높은 비용으로 멀티 리전을 선택합니다.
      • 리전 스냅샷을 선택하여 저렴한 비용으로 데이터의 물리적 위치를 더 세부적으로 제어합니다.
    2. 위치 선택 필드에서 사용하려는 특정 리전 또는 멀티 리전을 선택합니다. 소스 디스크에 가장 가까운 리전 또는 다중 리전을 사용하려면 디스크 위치 기준을 선택합니다.

  9. 스냅샷 일정 만들기를 완료하려면 만들기를 클릭합니다.
  10. 이 스냅샷 일정을 영구 디스크에 연결하려면 저장을 클릭합니다.

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

다음을 바꿉니다.

  • SCHEDULE_NAME: 스냅샷 일정의 이름
  • SCHEDULE_DESCRIPTION: 스냅샷 일정에 대한 설명입니다. 설명을 따옴표를 사용하여 묶습니다.
  • MAX_RETENTION_DAYS: 스냅샷 보관 일수입니다.

    예를 들어 값이 3이면 스냅샷이 3일 동안 보관된 후 삭제됩니다. 1 이상의 값을 사용해야 합니다.

  • START_TIME: UTC 시간대의 시작 시간입니다. 시작 시간은 정시에 시작해야 합니다.

    예를 들면 다음과 같습니다.

    • 오후 2시(PST)는 22:00으로 지정해야 합니다.
    • 시작 시간을 22:13으로 설정하면 오류가 발생합니다.

    --weekly-schedule-from-file 플래그를 사용하고 파일 내에 시작 시간을 지정하는 경우 이 플래그를 포함하지 않아도 됩니다.

  • SNAPSHOT_INTERVAL: 연속되는 스냅샷의 생성 간격입니다. 스냅샷 빈도 플래그 hourly-schedule, daily-schedule, weekly-schedule, weekly-schedule-from-file을 함께 사용할 수 없습니다. 스냅샷 일정에 하나만 사용할 수 있습니다.

    • 값 없이 --daily-schedule 플래그를 포함하여 일일 일정을 설정합니다.
    • --hourly-schedule 플래그를 1~23 사이의 정수 값으로 설정하여 시간 단위 일정을 설정합니다. 매일 같은 시간에 스냅샷을 생성하려면 24로 균등하게 나누어지는 시간 단위 숫자를 선택합니다. 예를 들어 --hourly-schedule12로 설정하면 스냅샷이 12시간마다 생성됩니다.
    • --weekly-schedule 플래그를 스냅샷을 만들 요일로 설정하여 주 단위 일정을 설정합니다. 요일을 정확히 입력해야 하며 값은 대소문자를 구분하지 않습니다. 예를 들어 매주 금요일에 디스크를 백업하려면 명령어에 --weekly-schedule=friday를 포함합니다.
    • --weekly-schedule-from-file 플래그를 포함하여 시작 시간을 다르게 지정하고 다른 요일을 지정하는 고급 주 단위 일정을 설정합니다. FILE_NAME을 주간 스냅샷 일정이 포함된 파일 이름으로 바꿉니다. 파일을 사용하여 다른 요일과 다른 시작 시간을 지정할 수 있지만 명령줄에서 직접 여러 주간 일정을 지정할 수는 없습니다. 예를 들어 파일에서 주간 일정 2개를 월요일과 수요일로 지정해도 명령줄에서 이 설정을 복제할 수 없습니다.

      [
        {"day": "MONDAY", "startTime": "04:00"},
        {"day": "WEDNESDAY", "startTime": "02:00"}
      ]
      

      파일에 시작 시간을 포함하면 명령줄에서 --start-time 플래그를 설정하지 않아도 됩니다. 일정은 UTC 시간대를 사용합니다.

  • DELETION_OPTION: 소스 디스크가 삭제된 경우 스냅샷 처리 방식을 결정합니다. 생성된 모든 스냅샷을 보존하려면 이 플래그를 생략할 수 있습니다. 그렇지 않은 경우 apply-retention-policy를 지정하여 보관 정책의 설정을 사용합니다.

  • STORAGE_LOCATION: 선택사항인 스토리지 위치입니다. 이 플래그를 생략하면 기본 스토리지 위치가 사용됩니다.

예시

모든 예시에는 다음 사항이 적용됩니다.

  • 디스크 삭제 규칙이 포함됩니다. --on-source-disk-delete 플래그가 기본 keep-auto-snapshots으로 설정되어 자동 생성된 모든 스냅샷을 무기한 유지합니다. 다른 방법으로는 이 플래그를 apply-retention-policy로 설정하여 스냅샷 보관 정책을 사용합니다.
  • 스토리지 위치가 수동으로 US로 설정되므로 생성된 모든 스냅샷이 US 멀티 리전에 저장됩니다.
  • env=devmedia=images 라벨이 생성된 모든 스냅샷에 적용됩니다.
  • 보관 정책이 10일로 설정됩니다.

시간 단위 일정:이 예시에서 스냅샷 일정은 22:00 UTC(14:00 PST)에 시작하고 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 UTC(14:00 PST)에 시작하고 매일 같은 시간에 발생합니다. --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 UTC(14:00 PST)에 시작하고 매주 화요일에 발생합니다.

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

import (
	"context"
	"fmt"
	"io"

	compute "cloud.google.com/go/compute/apiv1"
	computepb "cloud.google.com/go/compute/apiv1/computepb"
	"google.golang.org/protobuf/proto"
)

// createSnapshotSchedule creates a snapshot schedule.
func createSnapshotSchedule(w io.Writer, projectID, scheduleName, region string) error {
	// projectID := "your_project_id"
	// snapshotName := "your_snapshot_name"
	// region := "eupore-central2"

	ctx := context.Background()

	snapshotsClient, err := compute.NewResourcePoliciesRESTClient(ctx)
	if err != nil {
		return fmt.Errorf("NewResourcePoliciesRESTClient: %w", err)
	}
	defer snapshotsClient.Close()

	req := &computepb.InsertResourcePolicyRequest{
		Project: projectID,
		Region:  region,
		ResourcePolicyResource: &computepb.ResourcePolicy{
			Name:        proto.String(scheduleName),
			Description: proto.String("MY DAILY SNAPSHOT SCHEDULE"),
			Region:      proto.String("europe-central2"),
			SnapshotSchedulePolicy: &computepb.ResourcePolicySnapshotSchedulePolicy{
				RetentionPolicy: &computepb.ResourcePolicySnapshotSchedulePolicyRetentionPolicy{
					MaxRetentionDays: proto.Int32(10),
					// Check the OnSourceDiskDelete enum for the list of possible values.
					OnSourceDiskDelete: proto.String("KEEP_AUTO_SNAPSHOTS"),
				},
				Schedule: &computepb.ResourcePolicySnapshotSchedulePolicySchedule{
					DailySchedule: &computepb.ResourcePolicyDailyCycle{
						DaysInCycle: proto.Int32(1),
						StartTime:   proto.String("22:00"),
					},
				},
				SnapshotProperties: &computepb.ResourcePolicySnapshotSchedulePolicySnapshotProperties{
					StorageLocations: []string{"eu"},
					Labels: map[string]string{
						"env":   "dev",
						"media": "images",
					},
				},
			},
		},
	}
	op, err := snapshotsClient.Insert(ctx, req)
	if err != nil {
		return fmt.Errorf("unable to create snapshot schedule: %w", err)
	}

	if err = op.Wait(ctx); err != nil {
		return fmt.Errorf("unable to wait for the operation: %w", err)
	}

	fmt.Fprint(w, "Snapshot schedule created\n")

	return nil
}

Java

import com.google.cloud.compute.v1.InsertResourcePolicyRequest;
import com.google.cloud.compute.v1.Operation;
import com.google.cloud.compute.v1.Operation.Status;
import com.google.cloud.compute.v1.ResourcePoliciesClient;
import com.google.cloud.compute.v1.ResourcePolicy;
import com.google.cloud.compute.v1.ResourcePolicyHourlyCycle;
import com.google.cloud.compute.v1.ResourcePolicySnapshotSchedulePolicy;
import com.google.cloud.compute.v1.ResourcePolicySnapshotSchedulePolicyRetentionPolicy;
import com.google.cloud.compute.v1.ResourcePolicySnapshotSchedulePolicyRetentionPolicy.OnSourceDiskDelete;
import com.google.cloud.compute.v1.ResourcePolicySnapshotSchedulePolicySchedule;
import com.google.cloud.compute.v1.ResourcePolicySnapshotSchedulePolicySnapshotProperties;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class CreateSnapshotSchedule {
  public static void main(String[] args)
          throws IOException, ExecutionException, InterruptedException, TimeoutException {
    // TODO(developer): Replace these variables before running the sample.
    // Project ID or project number of the Cloud project you want to use.
    String projectId = "YOUR_PROJECT_ID";
    // Name of the region in which you want to create the snapshot schedule.
    String region = "us-central1";
    // Name of the snapshot schedule you want to create.
    String snapshotScheduleName = "YOUR_SCHEDULE_NAME";
    // Description of the snapshot schedule.
    String scheduleDescription = "YOUR_SCHEDULE_DESCRIPTION";
    // Maximum number of days to retain snapshots.
    int maxRetentionDays = 10;
    // Storage location for the snapshots.
    // More about storage locations:
    // https://cloud.google.com/compute/docs/disks/snapshots?authuser=0#selecting_a_storage_location
    String storageLocation = "US";

    createSnapshotSchedule(projectId, region, snapshotScheduleName, scheduleDescription,
            maxRetentionDays, storageLocation);
  }

  // Creates a snapshot schedule policy.
  public static Status createSnapshotSchedule(String projectId, String region,
            String snapshotScheduleName, String scheduleDescription, int maxRetentionDays,
            String storageLocation)
          throws IOException, ExecutionException, InterruptedException, TimeoutException {
    // Initialize client that will be used to send requests. This client only needs to be created
    // once, and can be reused for multiple requests.
    try (ResourcePoliciesClient resourcePoliciesClient = ResourcePoliciesClient.create()) {
      int snapshotInterval = 10; // Create a snapshot every 10 hours
      String startTime = "08:00"; // Define the hourly schedule

      ResourcePolicyHourlyCycle hourlyCycle = ResourcePolicyHourlyCycle.newBuilder()
              .setHoursInCycle(snapshotInterval)
              .setStartTime(startTime)
              .build();

      ResourcePolicySnapshotSchedulePolicyRetentionPolicy retentionPolicy =
              ResourcePolicySnapshotSchedulePolicyRetentionPolicy.newBuilder()
                      .setMaxRetentionDays(maxRetentionDays)
                      .setOnSourceDiskDelete(OnSourceDiskDelete.KEEP_AUTO_SNAPSHOTS.toString())
              .build();

      ResourcePolicySnapshotSchedulePolicySnapshotProperties snapshotProperties =
              ResourcePolicySnapshotSchedulePolicySnapshotProperties.newBuilder()
                      .addStorageLocations(storageLocation)
                      .build();

      ResourcePolicySnapshotSchedulePolicy snapshotSchedulePolicy =
              ResourcePolicySnapshotSchedulePolicy.newBuilder()
                      .setRetentionPolicy(retentionPolicy)
                      .setSchedule(ResourcePolicySnapshotSchedulePolicySchedule.newBuilder()
                               .setHourlySchedule(hourlyCycle)
                               .build())
                      .setSnapshotProperties(snapshotProperties)
                      .build();

      ResourcePolicy resourcePolicy = ResourcePolicy.newBuilder()
              .setName(snapshotScheduleName)
              .setDescription(scheduleDescription)
              .setSnapshotSchedulePolicy(snapshotSchedulePolicy)
              .build();
      InsertResourcePolicyRequest request = InsertResourcePolicyRequest.newBuilder()
              .setProject(projectId)
              .setRegion(region)
              .setResourcePolicyResource(resourcePolicy)
              .build();

      Operation response = resourcePoliciesClient.insertAsync(request)
              .get(3, TimeUnit.MINUTES);

      if (response.hasError()) {
        throw new Error("Snapshot schedule creation failed! " + response.getError());
      }
      return response.getStatus();
    }
  }
}

Node.js

// Import the Compute library
const computeLib = require('@google-cloud/compute');
const compute = computeLib.protos.google.cloud.compute.v1;

// Instantiate a resourcePoliciesClient
const resourcePoliciesClient = new computeLib.ResourcePoliciesClient();
// Instantiate a regionOperationsClient
const regionOperationsClient = new computeLib.RegionOperationsClient();

/**
 * TODO(developer): Update/uncomment these variables before running the sample.
 */
// The project name.
const projectId = await resourcePoliciesClient.getProjectId();

// The location of the snapshot schedule resource policy.
// region = 'us-central1';

// The name of the snapshot schedule.
// snapshotScheduleName = 'snapshot-schedule-name';

// The description of the snapshot schedule.
const snapshotScheduleDescription = 'snapshot schedule description...';

async function callCreateSnapshotSchedule() {
  const [response] = await resourcePoliciesClient.insert({
    project: projectId,
    region,
    resourcePolicyResource: new compute.ResourcePolicy({
      name: snapshotScheduleName,
      description: snapshotScheduleDescription,
      snapshotSchedulePolicy:
        new compute.ResourcePolicyInstanceSchedulePolicySchedule({
          retentionPolicy:
            new compute.ResourcePolicySnapshotSchedulePolicyRetentionPolicy({
              maxRetentionDays: 5,
            }),
          schedule: new compute.ResourcePolicySnapshotSchedulePolicySchedule({
            // Similarly, you can create a weekly or monthly schedule.
            // Review the resourcePolicies.insert method for details specific to setting a weekly or monthly schedule.
            // To see more details, open: `https://cloud.google.com/compute/docs/disks/scheduled-snapshots?authuser=0#create_snapshot_schedule`
            dailySchedule: new compute.ResourcePolicyDailyCycle({
              startTime: '12:00',
              daysInCycle: 1,
            }),
          }),
          snapshotProperties:
            new compute.ResourcePolicySnapshotSchedulePolicySnapshotProperties(
              {
                guestFlush: false,
                labels: {
                  env: 'dev',
                  media: 'images',
                },
                // OPTIONAL: the storage location. If you omit this flag, the default storage location is used.
                // storageLocations: 'storage-location',
              }
            ),
        }),
    }),
  });

  let operation = response.latestResponse;

  // Wait for the create operation to complete.
  while (operation.status !== 'DONE') {
    [operation] = await regionOperationsClient.wait({
      operation: operation.name,
      project: projectId,
      region,
    });
  }

  console.log(`Snapshot schedule: ${snapshotScheduleName} created.`);
}

await callCreateSnapshotSchedule();

Python

from __future__ import annotations

import sys
from typing import Any

from google.api_core.extended_operation import ExtendedOperation
from google.cloud import compute_v1


def wait_for_extended_operation(
    operation: ExtendedOperation, verbose_name: str = "operation", timeout: int = 300
) -> Any:
    """
    Waits for the extended (long-running) operation to complete.

    If the operation is successful, it will return its result.
    If the operation ends with an error, an exception will be raised.
    If there were any warnings during the execution of the operation
    they will be printed to sys.stderr.

    Args:
        operation: a long-running operation you want to wait on.
        verbose_name: (optional) a more verbose name of the operation,
            used only during error and warning reporting.
        timeout: how long (in seconds) to wait for operation to finish.
            If None, wait indefinitely.

    Returns:
        Whatever the operation.result() returns.

    Raises:
        This method will raise the exception received from `operation.exception()`
        or RuntimeError if there is no exception set, but there is an `error_code`
        set for the `operation`.

        In case of an operation taking longer than `timeout` seconds to complete,
        a `concurrent.futures.TimeoutError` will be raised.
    """
    result = operation.result(timeout=timeout)

    if operation.error_code:
        print(
            f"Error during {verbose_name}: [Code: {operation.error_code}]: {operation.error_message}",
            file=sys.stderr,
            flush=True,
        )
        print(f"Operation ID: {operation.name}", file=sys.stderr, flush=True)
        raise operation.exception() or RuntimeError(operation.error_message)

    if operation.warnings:
        print(f"Warnings during {verbose_name}:\n", file=sys.stderr, flush=True)
        for warning in operation.warnings:
            print(f" - {warning.code}: {warning.message}", file=sys.stderr, flush=True)

    return result


def snapshot_schedule_create(
    project_id: str,
    region: str,
    schedule_name: str,
    schedule_description: str,
    labels: dict,
) -> compute_v1.ResourcePolicy:
    """
    Creates a snapshot schedule for disks for a specified project and region.
    Args:
        project_id (str): The ID of the Google Cloud project.
        region (str): The region where the snapshot schedule will be created.
        schedule_name (str): The name of the snapshot schedule group.
        schedule_description (str): The description of the snapshot schedule group.
        labels (dict): The labels to apply to the snapshots. Example: {"env": "dev", "media": "images"}
    Returns:
        compute_v1.ResourcePolicy: The created resource policy.
    """

    # # Every hour, starts at 12:00 AM
    # hourly_schedule = compute_v1.ResourcePolicyHourlyCycle(
    #     hours_in_cycle=1, start_time="00:00"
    # )
    #
    # # Every Monday, starts between 12:00 AM and 1:00 AM
    # day = compute_v1.ResourcePolicyWeeklyCycleDayOfWeek(
    #     day="MONDAY", start_time="00:00"
    # )
    # weekly_schedule = compute_v1.ResourcePolicyWeeklyCycle(day_of_weeks=[day])

    # In this example we use daily_schedule - every day, starts between 12:00 AM and 1:00 AM
    daily_schedule = compute_v1.ResourcePolicyDailyCycle(
        days_in_cycle=1, start_time="00:00"
    )

    schedule = compute_v1.ResourcePolicySnapshotSchedulePolicySchedule()
    # You can change the schedule type to daily_schedule, weekly_schedule, or hourly_schedule
    schedule.daily_schedule = daily_schedule

    # Autodelete snapshots after 5 days
    retention_policy = compute_v1.ResourcePolicySnapshotSchedulePolicyRetentionPolicy(
        max_retention_days=5
    )
    snapshot_properties = (
        compute_v1.ResourcePolicySnapshotSchedulePolicySnapshotProperties(
            guest_flush=False, labels=labels
        )
    )

    snapshot_policy = compute_v1.ResourcePolicySnapshotSchedulePolicy()
    snapshot_policy.schedule = schedule
    snapshot_policy.retention_policy = retention_policy
    snapshot_policy.snapshot_properties = snapshot_properties

    resource_policy_resource = compute_v1.ResourcePolicy(
        name=schedule_name,
        description=schedule_description,
        snapshot_schedule_policy=snapshot_policy,
    )

    client = compute_v1.ResourcePoliciesClient()
    operation = client.insert(
        project=project_id,
        region=region,
        resource_policy_resource=resource_policy_resource,
    )
    wait_for_extended_operation(operation, "Resource Policy creation")

    return client.get(project=project_id, region=region, resource_policy=schedule_name)

REST

스냅샷 일정을 만들려면 resourcePolicies.insert에 대한 POST 요청을 작성합니다. 스냅샷 일정 이름과 스냅샷 빈도를 포함해야 합니다.

요청에 스냅샷 스토리지 위치를 수동으로 지정하고 리소스 라벨을 추가할 수도 있습니다.

기본적으로 onSourceDiskDelete 매개변수가 keepAutoSnapshots로 설정됩니다. 즉, 소스 디스크가 삭제되면 해당 디스크의 자동 생성된 스냅샷이 무기한 보관됩니다. 또는 플래그를 applyRetentionPolicy로 설정하여 보관 정책을 적용할 수 있습니다.

다음 예시에서는 12:00 UTC(04:00 PST)에 시작하여 매일 반복되는 일 단위 스냅샷 일정을 설정합니다. 또한 이 예시에서는 보관 정책을 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"
       }
     }
    }

다음을 바꿉니다.

  • PROJECT_ID: 프로젝트 이름
  • REGION: 스냅샷 일정 리소스 정책의 위치
  • SCHEDULE_DESCRIPTION: 스냅샷 일정의 설명
  • SCHEDULE_NAME: 스냅샷 일정의 이름
  • STORAGE_LOCATION: 선택사항인 스토리지 위치입니다. 이 플래그를 생략하면 기본 스토리지 위치가 사용됩니다.

이와 유사하게 주 단위나 월 단위로 일정을 만들 수 있습니다. 주 단위 또는 월 단위 일정 설정에 대한 자세한 내용은 resourcePolicies.insert 메서드를 참조하세요.

예를 들어 다음 요청은 화요일 9:00(UTC)에 실행되는 주간 일정을 만듭니다.

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를 사용하세요.

콘솔

기존 디스크에 스냅샷 일정을 연결합니다.

  1. Google Cloud 콘솔에서 디스크 페이지로 이동합니다.

    디스크로 이동

  2. 스냅샷 일정을 연결할 디스크의 이름을 선택합니다. 그러면 디스크 관리 페이지가 열립니다.

  3. 디스크 관리 페이지에서 수정을 클릭합니다. 먼저 작업 더보기 메뉴를 클릭해야 할 수 있습니다.

  4. 스냅샷 일정 드롭다운 메뉴를 사용하여 디스크에 일정을 추가하거나 새 일정을 만듭니다.

  5. 새 일정을 만드는 경우 만들기를 클릭합니다.

  6. 저장을 클릭하여 작업을 완료합니다.

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

import (
	"context"
	"fmt"
	"io"

	compute "cloud.google.com/go/compute/apiv1"
	computepb "cloud.google.com/go/compute/apiv1/computepb"
)

// attachSnapshotSchedule attaches a snapshot schedule to disk.
func attachSnapshotSchedule(w io.Writer, projectID, scheduleName, diskName, region string) error {
	// projectID := "your_project_id"
	// snapshotName := "your_snapshot_name"
	// diskName := "your_disk_name"
	// region := "europe-central2"

	ctx := context.Background()

	disksClient, err := compute.NewRegionDisksRESTClient(ctx)
	if err != nil {
		return fmt.Errorf("NewDisksRESTClient: %w", err)
	}
	defer disksClient.Close()

	req := &computepb.AddResourcePoliciesRegionDiskRequest{
		Project: projectID,
		Region:  region,
		Disk:    diskName,
		RegionDisksAddResourcePoliciesRequestResource: &computepb.RegionDisksAddResourcePoliciesRequest{
			ResourcePolicies: []string{
				fmt.Sprintf("projects/%s/regions/%s/resourcePolicies/%s", projectID, region, scheduleName),
			},
		},
	}
	op, err := disksClient.AddResourcePolicies(ctx, req)
	if err != nil {
		return fmt.Errorf("unable to attach schedule: %w", err)
	}

	if err = op.Wait(ctx); err != nil {
		return fmt.Errorf("unable to wait for the operation: %w", err)
	}

	fmt.Fprint(w, "Snapshot schedule attached\n")

	return nil
}

Java

import com.google.cloud.compute.v1.AddResourcePoliciesDiskRequest;
import com.google.cloud.compute.v1.DisksAddResourcePoliciesRequest;
import com.google.cloud.compute.v1.DisksClient;
import com.google.cloud.compute.v1.Operation;
import com.google.cloud.compute.v1.Operation.Status;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class AttachSnapshotScheduleToDisk {
  public static void main(String[] args) throws Exception {
    // TODO(developer): Replace these variables before running the sample.
    // Project ID or project number of the Cloud project you want to use.
    String projectId = "YOUR_PROJECT_ID";
    // Name of the zone where your disk is located.
    String zone = "us-central1-a";
    // Name of the disk you want to attach the snapshot schedule to.
    String diskName = "YOUR_DISK_NAME";
    // Name of the snapshot schedule you want to attach.
    String snapshotScheduleName = "YOUR_SNAPSHOT_SCHEDULE_NAME";
    // Name of the region where your snapshot schedule is located.
    String region = "us-central1";

    attachSnapshotScheduleToDisk(projectId, zone, diskName, snapshotScheduleName, region);
  }

  // Attaches a snapshot schedule to a disk.
  public static Status attachSnapshotScheduleToDisk(
        String projectId, String zone, String diskName, String snapshotScheduleName, String region)
        throws IOException, ExecutionException, InterruptedException, TimeoutException {

    String resourcePolicyLink = String.format(
            "projects/%s/regions/%s/resourcePolicies/%s", projectId, region, snapshotScheduleName);
    // Initialize client that will be used to send requests. This client only needs to be created
    // once, and can be reused for multiple requests.
    try (DisksClient disksClient = DisksClient.create()) {

      AddResourcePoliciesDiskRequest request = AddResourcePoliciesDiskRequest.newBuilder()
              .setProject(projectId)
              .setZone(zone)
              .setDisk(diskName)
              .setDisksAddResourcePoliciesRequestResource(
                      DisksAddResourcePoliciesRequest.newBuilder()
                              .addResourcePolicies(resourcePolicyLink)
                              .build())
              .build();

      Operation response = disksClient.addResourcePoliciesAsync(request).get(3, TimeUnit.MINUTES);

      if (response.hasError()) {
        throw new Error("Error attaching snapshot schedule to disk: " + response.getError());
      }
      return response.getStatus();
    }
  }
}

Python

from __future__ import annotations

import sys
from typing import Any

from google.api_core.extended_operation import ExtendedOperation
from google.cloud import compute_v1


def wait_for_extended_operation(
    operation: ExtendedOperation, verbose_name: str = "operation", timeout: int = 300
) -> Any:
    """
    Waits for the extended (long-running) operation to complete.

    If the operation is successful, it will return its result.
    If the operation ends with an error, an exception will be raised.
    If there were any warnings during the execution of the operation
    they will be printed to sys.stderr.

    Args:
        operation: a long-running operation you want to wait on.
        verbose_name: (optional) a more verbose name of the operation,
            used only during error and warning reporting.
        timeout: how long (in seconds) to wait for operation to finish.
            If None, wait indefinitely.

    Returns:
        Whatever the operation.result() returns.

    Raises:
        This method will raise the exception received from `operation.exception()`
        or RuntimeError if there is no exception set, but there is an `error_code`
        set for the `operation`.

        In case of an operation taking longer than `timeout` seconds to complete,
        a `concurrent.futures.TimeoutError` will be raised.
    """
    result = operation.result(timeout=timeout)

    if operation.error_code:
        print(
            f"Error during {verbose_name}: [Code: {operation.error_code}]: {operation.error_message}",
            file=sys.stderr,
            flush=True,
        )
        print(f"Operation ID: {operation.name}", file=sys.stderr, flush=True)
        raise operation.exception() or RuntimeError(operation.error_message)

    if operation.warnings:
        print(f"Warnings during {verbose_name}:\n", file=sys.stderr, flush=True)
        for warning in operation.warnings:
            print(f" - {warning.code}: {warning.message}", file=sys.stderr, flush=True)

    return result


def snapshot_schedule_attach(
    project_id: str, zone: str, region: str, disk_name: str, schedule_name: str
) -> None:
    """
    Attaches a snapshot schedule to a specified disk.
    Args:
        project_id (str): The ID of the Google Cloud project.
        zone (str): The zone where the disk is located.
        region (str): The region where the snapshot schedule was created
        disk_name (str): The name of the disk to which the snapshot schedule will be attached.
        schedule_name (str): The name of the snapshot schedule that you are applying to this disk
    Returns:
        None
    """
    disks_add_request = compute_v1.DisksAddResourcePoliciesRequest(
        resource_policies=[f"regions/{region}/resourcePolicies/{schedule_name}"]
    )

    client = compute_v1.DisksClient()
    operation = client.add_resource_policies(
        project=project_id,
        zone=zone,
        disk=disk_name,
        disks_add_resource_policies_request_resource=disks_add_request,
    )
    wait_for_extended_operation(operation, "Attaching snapshot schedule to disk")

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를 사용하여 디스크와 스냅샷 일정을 동시에 만들 수 있습니다.

콘솔

  1. Google Cloud Console에서 디스크 페이지로 이동합니다.

    디스크로 이동

  2. 디스크 만들기를 클릭합니다.

  3. 필수 입력란을 모두 완료하여 영역(zone) 또는 리전 디스크를 만듭니다.

  4. 스냅샷 일정과 동일한 리전에 디스크를 만듭니다.

  5. 스냅샷 일정의 필드를 채웁니다.

  6. 드롭다운 메뉴를 사용하고 필드에 모두 입력하여 일정을 만듭니다.

  7. 만들기를 클릭하여 일정을 만듭니다.

  8. 만들기를 클릭하여 디스크를 만듭니다.

gcloud

gcloud disks create 명령어를 사용하여 영역 또는 리전 Persistent Disk와 Hyperdisk를 만들어 스냅샷 일정을 연결합니다.

gcloud compute disks create DISK_NAME \
     --resource-policies SCHEDULE_NAME \
     --zone ZONE

다음을 바꿉니다.

  • DISK_NAME: 새 디스크의 이름입니다.
  • SCHEDULE_NAME: 스냅샷 일정의 이름
  • ZONE: 디스크를 만들 위치. 디스크는 스냅샷 일정과 동일한 리전에 있는 영역에 있어야 합니다.

Go

import (
	"context"
	"fmt"
	"io"

	compute "cloud.google.com/go/compute/apiv1"
	computepb "cloud.google.com/go/compute/apiv1/computepb"
	"google.golang.org/protobuf/proto"
)

// createDiskWithSnapshotSchedule creates a new empty regional disk with snapshot schedule
func createDiskWithSnapshotSchedule(
	w io.Writer,
	projectID, region, diskName, diskType, scheduleName string,
	replicaZones []string,
	diskSizeGb int64,
) error {
	// projectID := "your_project_id"
	// region := "us-west3" // should match diskType below
	// diskName := "your_disk_name"
	// diskType := "regions/us-west3/diskTypes/pd-ssd"
	// scheduleName := "your_schedule_name"
	// replicaZones := []string{"us-west3-a", "us-west3-b"}
	// diskSizeGb := 120

	// Exactly two replica zones must be specified
	replicaZoneURLs := []string{
		fmt.Sprintf("projects/%s/zones/%s", projectID, replicaZones[0]),
		fmt.Sprintf("projects/%s/zones/%s", projectID, replicaZones[1]),
	}

	ctx := context.Background()
	disksClient, err := compute.NewRegionDisksRESTClient(ctx)
	if err != nil {
		return fmt.Errorf("NewRegionDisksRESTClient: %w", err)
	}
	defer disksClient.Close()

	req := &computepb.InsertRegionDiskRequest{
		Project: projectID,
		Region:  region,
		DiskResource: &computepb.Disk{
			Name:         proto.String(diskName),
			Region:       proto.String(region),
			Type:         proto.String(diskType),
			SizeGb:       proto.Int64(diskSizeGb),
			ReplicaZones: replicaZoneURLs,
			ResourcePolicies: []string{
				fmt.Sprintf("projects/%s/regions/%s/resourcePolicies/%s", projectID, region, scheduleName),
			},
		},
	}

	op, err := disksClient.Insert(ctx, req)
	if err != nil {
		return fmt.Errorf("unable to create disk with schedule: %w", err)
	}

	if err = op.Wait(ctx); err != nil {
		return fmt.Errorf("unable to wait for the operation: %w", err)
	}

	fmt.Fprintf(w, "Disk with schedule created\n")

	return nil
}

Java

import com.google.cloud.compute.v1.Disk;
import com.google.cloud.compute.v1.DisksClient;
import com.google.cloud.compute.v1.Operation;
import com.google.cloud.compute.v1.Operation.Status;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class CreateDiskWithSnapshotSchedule {
  public static void main(String[] args)
          throws IOException, ExecutionException, InterruptedException, TimeoutException {
    // TODO(developer): Replace these variables before running the sample.
    // Project ID or project number of the Cloud project you want to use.
    String projectId = "YOUR_PROJECT_ID";
    // Name of the zone in which you want to create the disk.
    String zone = "us-central1-a";
    // Name of the disk you want to create.
    String diskName = "YOUR_DISK_NAME";
    // Name of the schedule you want to link to the disk.
    String snapshotScheduleName = "YOUR_SCHEDULE_NAME";

    createDiskWithSnapshotSchedule(projectId, zone, diskName, snapshotScheduleName);
  }

  // Creates disk with linked snapshot schedule.
  public static Status createDiskWithSnapshotSchedule(
      String projectId, String zone, String diskName, String snapshotScheduleName)
      throws IOException, ExecutionException, InterruptedException, TimeoutException {
    // Initialize client that will be used to send requests. This client only needs to be created
    // once, and can be reused for multiple requests.
    try (DisksClient disksClient = DisksClient.create()) {
      String region = zone.substring(0, zone.lastIndexOf('-'));
      // Get the resource policy to link to the disk
      String resourcePolicyLink = String.format("projects/%s/regions/%s/resourcePolicies/%s",
              projectId, region, snapshotScheduleName);

      Disk disk = Disk.newBuilder()
              .setName(diskName)
              .setZone(zone)
              .addAllResourcePolicies(List.of(resourcePolicyLink))
              .build();

      Operation response = disksClient.insertAsync(projectId, zone, disk).get(3, TimeUnit.MINUTES);

      if (response.hasError()) {
        throw new Error("Disk creation failed! " + response.getError());
      }
      return response.getStatus();
    }
  }
}

다음 단계