Membuat jadwal untuk snapshot disk


Anda membuat jadwal snapshot untuk mencadangkan Persistent Disk zona dan regional serta Hyperdisk Google Cloud secara rutin dan otomatis. Gunakan jadwal snapshot sebagai praktik terbaik untuk mencadangkan workload Compute Engine Anda.

Jika Anda ingin membuat jadwal snapshot yang merekam status data aplikasi pada saat pencadangan, yang juga disebut sebagai konsisten aplikasi, pengosongan tamu, atau snapshot VSS, lihat Membuat snapshot disk yang konsisten dengan aplikasi Linux atau Membuat snapshot disk yang konsisten dengan aplikasi Windows.

Untuk mengetahui informasi selengkapnya tentang properti jadwal snapshot, lihat Properti jadwal snapshot.

Sebelum memulai

  • Tinjau Batasan untuk jadwal snapshot.
  • Jika Anda belum melakukannya, siapkan autentikasi. Autentikasi adalah proses yang digunakan untuk memverifikasi identitas Anda agar dapat mengakses Google Cloud layanan dan API. Untuk menjalankan kode atau contoh dari lingkungan pengembangan lokal, Anda dapat mengautentikasi ke Compute Engine dengan memilih salah satu opsi berikut:

    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

      Untuk menggunakan contoh Go di halaman ini dalam lingkungan pengembangan lokal, instal dan lakukan inisialisasi gcloud CLI, lalu siapkan Kredensial Default Aplikasi dengan kredensial pengguna Anda.

      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.

      Untuk informasi selengkapnya, lihat Set up authentication for a local development environment.

      Java

      Untuk menggunakan contoh Java di halaman ini dalam lingkungan pengembangan lokal, instal dan lakukan inisialisasi gcloud CLI, lalu siapkan Kredensial Default Aplikasi dengan kredensial pengguna Anda.

      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.

      Untuk informasi selengkapnya, lihat Set up authentication for a local development environment.

      Node.js

      Untuk menggunakan contoh Node.js di halaman ini dalam lingkungan pengembangan lokal, instal dan lakukan inisialisasi gcloud CLI, lalu siapkan Kredensial Default Aplikasi dengan kredensial pengguna Anda.

      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.

      Untuk informasi selengkapnya, lihat Set up authentication for a local development environment.

      Python

      Untuk menggunakan contoh Python di halaman ini dalam lingkungan pengembangan lokal, instal dan lakukan inisialisasi gcloud CLI, lalu siapkan Kredensial Default Aplikasi dengan kredensial pengguna Anda.

      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.

      Untuk informasi selengkapnya, lihat Set up authentication for a local development environment.

      REST

      Untuk menggunakan contoh REST API di halaman ini dalam lingkungan pengembangan lokal, gunakan kredensial yang Anda berikan ke gcloud CLI.

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

        gcloud init

      Untuk informasi selengkapnya, lihat Mengautentikasi untuk menggunakan REST dalam Google Cloud dokumentasi autentikasi.

Peran dan izin yang diperlukan

Untuk mendapatkan izin yang Anda perlukan untuk membuat jadwal snapshot, minta administrator untuk memberi Anda peran IAM berikut pada project:

Untuk mengetahui informasi selengkapnya tentang cara memberikan peran, lihat Mengelola akses ke project, folder, dan organisasi.

Peran bawaan ini berisi izin yang diperlukan untuk membuat jadwal snapshot. Untuk melihat izin yang benar-benar diperlukan, luaskan bagian Izin yang diperlukan:

Izin yang diperlukan

Izin berikut diperlukan untuk membuat jadwal snapshot:

  • Untuk membuat jadwal snapshot: compute.resourcePolicies.create di project atau organisasi
  • Untuk melampirkan jadwal snapshot ke disk:
    • compute.disks.addResourcePolicies pada disk
    • compute.resourcePolicies.use pada kebijakan resource
  • Untuk membuat disk dengan jadwal snapshot:
    • compute.disks.create dalam project
    • compute.resourcePolicies.create dalam project
    • compute.disks.addResourcePolicies pada disk

Anda mungkin juga bisa mendapatkan izin ini dengan peran khusus atau peran bawaan lainnya.

Ringkasan pembuatan jadwal snapshot

Saat membuat jadwal snapshot, buat kebijakan resource yang dapat Anda terapkan ke satu atau beberapa volume Hyperdisk atau Persistent Disk.

Anda dapat membuat jadwal snapshot dengan cara berikut:

Menggunakan enkripsi dengan jadwal snapshot

Jika disk menggunakan kunci enkripsi yang dikelola pelanggan (CMEK), saat Anda menggunakan jadwal snapshot untuk membuat snapshot disk tersebut, semua snapshot yang dibuat akan otomatis dienkripsi dengan kunci yang sama.

Anda tidak dapat menggunakan jadwal snapshot dengan disk yang menggunakan kunci enkripsi yang disediakan pelanggan (CSEK).

Membuat jadwal snapshot

Anda dapat membuat jadwal snapshot untuk disk menggunakan konsol Google Cloud, Google Cloud CLI, atau REST. Anda harus membuat jadwal snapshot di region yang sama dengan tempat disk berada. Misalnya, jika disk Anda berada di zona us-west1-a, Anda harus membuat jadwal snapshot di region us-west1. Namun, Anda dapat memilih untuk menyimpan snapshot yang dihasilkan oleh jadwal snapshot di lokasi yang berbeda.

Konsol

  1. Di Konsol Google Cloud, buka halaman Instance VM.

    Buka instance VM
    Langkah-langkah yang tersisa akan muncul secara otomatis di Konsol Google Cloud.

  2. Pilih project yang berisi instance VM Anda.
  3. Di kolom Nama, klik nama VM yang memiliki persistent disk untuk membuat jadwal snapshot.
  4. Di beberapa Penyimpanan, klik nama Boot disk atau Disk tambahan untuk membuat jadwal snapshot.
  5. Klik Edit. Anda mungkin perlu mengklik menu Tindakan lainnya, lalu Edit.
  6. Di Jadwal snapshot, pilih Buat jadwal.
  7. Di bagian Name, masukkan salah satu nama berikut untuk jadwal snapshot:
    • boot-disk-snapshot-schedule
    • attached-persistent-disk-snapshot-schedule
  8. Di bagian Location, pilih lokasi penyimpanan snapshot Anda. Lokasi default bawaan atau yang telah disesuaikan dalam setelan snapshot Anda akan otomatis dipilih. Secara opsional, Anda dapat mengganti setelan snapshot dan menyimpan snapshot Anda di lokasi penyimpanan kustom dengan melakukan hal berikut:

    1. Pilih jenis lokasi penyimpanan yang diinginkan untuk snapshot Anda.

      • Pilih Multi-regional untuk ketersediaan yang lebih tinggi dengan biaya lebih tinggi.
      • Pilih Snapshot regional untuk mendapatkan kontrol lebih besar atas lokasi fisik data Anda dengan biaya yang lebih rendah.
    2. Di kolom Pilih lokasi, pilih region atau multi-region spesifik yang ingin Anda gunakan. Untuk menggunakan region atau multi-region yang paling dekat dengan disk sumber, pilih Berdasarkan lokasi disk.

  9. Untuk menyelesaikan pembuatan jadwal snapshot, klik Buat.
  10. Untuk melampirkan jadwal snapshot ini ke persistent disk, klik Simpan.

gcloud

Untuk membuat jadwal snapshot untuk disk, gunakan perintah gcloud compute resource-policies create snapshot-schedule. Tetapkan frekuensi jadwal Anda ke per jam, harian, atau mingguan.

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

Ganti kode berikut:

  • SCHEDULE_NAME: nama jadwal snapshot.
  • SCHEDULE_DESCRIPTION: deskripsi jadwal snapshot. Gunakan kutipan untuk deskripsi Anda.
  • MAX_RETENTION_DAYS: jumlah hari untuk mempertahankan snapshot.

    Misalnya, nilai 3 berarti snapshot dipertahankan selama 3 hari sebelum dihapus. Anda harus menggunakan nilai 1 atau lebih tinggi.

  • START_TIME: waktu mulai dalam zona waktu UTC. Waktu harus dimulai di awal jam.

    Contoh:

    • Pukul 14.00 PST harus ditentukan sebagai 22:00.
    • Jika Anda menetapkan waktu mulai 22:13, Anda akan menerima error.

    Jika menggunakan flag --weekly-schedule-from-file dan menentukan waktu mulai dalam file, Anda tidak perlu menyertakan flag ini.

  • SNAPSHOT_INTERVAL: interval antara pembuatan snapshot berturut-turut. Flag frekuensi snapshot hourly-schedule, daily-schedule, weekly-schedule, dan weekly-schedule-from-file bersifat eksklusif satu sama lain. Anda hanya dapat menggunakan satu opsi untuk jadwal snapshot Anda.

    • Tetapkan jadwal harian dengan menyertakan flag --daily-schedule tanpa nilai apa pun.
    • Tetapkan jadwal per jam dengan flag --hourly-schedule yang ditetapkan ke nilai bilangan bulat antara 1 dan 23. Untuk membuat snapshot pada waktu yang sama setiap hari, pilih angka per jam yang dibagi merata menjadi 24. Misalnya, menetapkan --hourly-schedule ke 12, berarti snapshot dibuat setiap 12 jam.
    • Tetapkan jadwal mingguan dengan flag --weekly-schedule yang ditetapkan ke hari dalam seminggu yang Anda inginkan untuk membuat snapshot. Anda harus menguraikan hari kerja; nilai-nilainya tidak peka huruf besar/kecil. Misalnya, untuk mencadangkan disk setiap hari Jumat, perintah Anda akan menyertakan --weekly-schedule=friday.
    • Tetapkan jadwal mingguan lanjutan, yang menentukan hari yang berbeda dalam minggu dan dengan waktu mulai yang berbeda dengan menyertakan tanda --weekly-schedule-from-file. Ganti FILE_NAME dengan nama file yang berisi jadwal snapshot mingguan. Meskipun Anda dapat menentukan hari dalam seminggu dan waktu mulai yang berbeda menggunakan file, Anda tidak dapat menentukan beberapa jadwal mingguan langsung di command line. Misalnya, file Anda mungkin menentukan dua jadwal mingguan, pada hari Senin dan Rabu, tetapi Anda tidak dapat menduplikasi setelan ini di command line:

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

      Jika menyertakan waktu mulai dalam file, Anda tidak perlu menetapkan tanda --start-time di command line. Jadwal menggunakan zona waktu UTC.

  • DELETION_OPTION: menentukan apa yang terjadi pada snapshot jika disk sumber dihapus. Jika ingin menyimpan semua snapshot yang dihasilkan, Anda dapat menghilangkan flag ini. Jika tidak, tentukan apply-retention-policy untuk menggunakan setelan dalam kebijakan retensi.

  • STORAGE_LOCATION: Opsional: lokasi penyimpanan. Jika Anda menghapus flag ini, lokasi penyimpanan default akan digunakan.

Contoh

Dalam semua contoh berikut:

  • Aturan penghapusan disk disertakan; flag --on-source-disk-delete disetel ke default keep-auto-snapshots untuk menyimpan secara permanen semua snapshot yang dihasilkan secara otomatis. Alternatifnya adalah menetapkan flag ini ke apply-retention-policy untuk menggunakan kebijakan retensi snapshot Anda.
  • Lokasi penyimpanan ditetapkan secara manual ke US, sehingga semua snapshot yang dihasilkan disimpan di multi-region AS.
  • Label env=dev dan media=images diterapkan ke semua snapshot yang dihasilkan.
  • Kebijakan retensi disetel ke 10 hari.

Jadwal per jam: Dalam contoh ini, jadwal snapshot dimulai pada pukul 22.00 UTC (14.00 PST) dan terjadi setiap 4 jam.

  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

Jadwal harian: Dalam contoh ini, jadwal snapshot dimulai pada pukul 22.00 UTC (14.00 PST) dan terjadi setiap hari pada waktu yang sama. Flag --daily-schedule harus ada, tetapi tanpa nilai terkait.

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

Jadwal mingguan: Dalam contoh ini, jadwal snapshot dimulai pada pukul 22.00 UTC (14.00 PST) dan terjadi setiap minggu pada hari Selasa.

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

Untuk membuat jadwal snapshot, buat permintaan POST ke resourcePolicies.insert. Anda harus menyertakan nama jadwal snapshot dan frekuensi snapshot.

Anda juga dapat menentukan lokasi penyimpanan snapshot secara manual dan menambahkan label resource dalam permintaan.

Secara default, parameter onSourceDiskDelete ditetapkan ke keepAutoSnapshots. Artinya, jika disk sumber dihapus, snapshot yang dihasilkan secara otomatis untuk disk tersebut akan dipertahankan tanpa batas waktu. Atau, Anda dapat menetapkan tanda ke applyRetentionPolicy untuk menerapkan kebijakan retensi.

Contoh berikut menetapkan jadwal snapshot harian yang dimulai pada pukul 12.00 UTC (04.00 PST) dan berulang setiap hari. Contoh ini juga menetapkan kebijakan retensi 5 hari; setelah 5 hari, snapshot akan otomatis dihapus.

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"
       }
     }
    }

Ganti kode berikut:

  • PROJECT_ID: nama project
  • REGION: lokasi kebijakan resource jadwal snapshot
  • SCHEDULE_DESCRIPTION: deskripsi jadwal snapshot
  • SCHEDULE_NAME: nama jadwal snapshot
  • STORAGE_LOCATION: Opsional: lokasi penyimpanan. Jika Anda menghapus flag ini, lokasi penyimpanan default akan digunakan.

Demikian pula, Anda dapat membuat jadwal mingguan atau bulanan. Tinjau metode resourcePolicies.insert untuk mengetahui detail khusus terkait menyetel jadwal mingguan atau bulanan.

Misalnya, permintaan berikut membuat jadwal mingguan yang berjalan pada hari Selasa pukul 09.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"
      }
  }
}

Melampirkan jadwal snapshot ke disk

Setelah membuat jadwal, lampirkan ke disk yang ada. Gunakan konsol, gcloud CLI, atau Compute Engine API.

Konsol

Lampirkan jadwal snapshot ke disk yang ada.

  1. Di konsol Google Cloud, buka halaman Disks.

    Buka Disk

  2. Pilih nama disk tempat Anda ingin melampirkan jadwal snapshot. Tindakan ini akan membuka halaman Kelola disk.

  3. Di halaman Manage disk, klik Edit. Anda mungkin perlu mengklik menu More actions terlebih dahulu.

  4. Gunakan menu drop-down Snapshot schedule untuk menambahkan jadwal ke disk. Atau buat jadwal baru.

  5. Jika Anda membuat jadwal baru, klik Create.

  6. Klik Save untuk menyelesaikan tugas.

gcloud

Untuk menyertakan jadwal snapshot ke disk, gunakan perintah gcloud disks add-resource-policies.

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

Ganti kode berikut:

  • DISK_NAME: nama disk yang ada
  • SCHEDULE_NAME: nama jadwal snapshot
  • ZONE: lokasi disk Anda

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

Buat permintaan POST ke disks.addResourcePolicies untuk melampirkan jadwal snapshot ke disk yang ada.

POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/disks/DISK_NAME/addResourcePolicies

{
  "resourcePolicies": [
    "regions/REGION/resourcePolicies/SCHEDULE_NAME"
  ]
}

Ganti kode berikut:

  • PROJECT_ID: nama project
  • ZONE: zona tempat disk berada
  • REGION: region tempat jadwal snapshot dibuat
  • DISK_NAME: nama disk
  • SCHEDULE_NAME: nama jadwal snapshot yang Anda terapkan ke disk ini

Membuat disk dengan jadwal snapshot

Anda dapat menggunakan Konsol Google Cloud atau gcloud CLI untuk membuat disk dan jadwal snapshot secara bersamaan.

Konsol

  1. Di konsol Google Cloud, buka halaman Disks.

    Buka Disk

  2. Klik Buat Disk.

  3. Lengkapi kolom wajib diisi untuk membuat disk zona atau regional.

  4. Buat disk di region yang sama dengan jadwal snapshot Anda.

  5. Isi kolom untuk Jadwal snapshot Anda.

  6. Gunakan menu drop-down dan lengkapi kolom untuk membuat jadwal.

  7. Klik Buat untuk membuat jadwal.

  8. Klik Create untuk membuat disk.

gcloud

Gunakan perintah gcloud disks create untuk membuat Persistent Disk atau Hyperdisk zona atau regional dan sertakan jadwal snapshot.

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

Ganti kode berikut:

  • DISK_NAME: nama disk baru
  • SCHEDULE_NAME: nama jadwal snapshot
  • ZONE: lokasi Anda membuat disk. Disk harus berada di zona yang berada di region yang sama dengan jadwal snapshot.

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();
    }
  }
}

Langkah berikutnya