Creating scheduled snapshots for persistent disk

Stay organized with collections Save and categorize content based on your preferences.

This document describes how to create a snapshot schedule to regularly and automatically back up your zonal and regional persistent disks. Use snapshot schedules as a best practice to back up your Compute Engine workloads. If you want to create a snapshot schedule that captures the state of the application data at the time of backup, also referred to as an application consistent, guest-flush, or VSS snapshot, see Creating a Linux application consistent persistent disk snapshot or Creating a Windows persistent disk snapshot.

When you create a snapshot schedule, you create a resource policy that you can apply to one or more persistent disks. You can create snapshot schedules in the following ways:

A snapshot schedule includes the following properties:

  • Schedule name
  • Schedule description
  • Snapshot frequency (hourly, daily, weekly)
  • Snapshot start time
  • Region where the snapshot schedule is available
  • Source disk deletion policy for handling auto-generated snapshots if the source disk is deleted
  • Retention policy to define how long to keep snapshots that are generated from the snapshot schedule

Before you begin

  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. Install the Google Cloud CLI.
  3. To initialize the gcloud CLI, run the following command:

    gcloud init
  4. Create or select a Google Cloud project.

    • Create a Cloud project:

      gcloud projects create PROJECT_ID
    • Select the Cloud project that you created:

      gcloud config set project PROJECT_ID
  5. Make sure that billing is enabled for your Cloud project. Learn how to check if billing is enabled on a project.

  6. Enable the Compute Engine API:

    gcloud services enable compute.googleapis.com
  7. Install the Google Cloud CLI.
  8. To initialize the gcloud CLI, run the following command:

    gcloud init
  9. Create or select a Google Cloud project.

    • Create a Cloud project:

      gcloud projects create PROJECT_ID
    • Select the Cloud project that you created:

      gcloud config set project PROJECT_ID
  10. Make sure that billing is enabled for your Cloud project. Learn how to check if billing is enabled on a project.

  11. Enable the Compute Engine API:

    gcloud services enable compute.googleapis.com

Restrictions

  • A persistent disk can have at most 10 snapshot schedules attached to it at a time.
  • You cannot create archive snapshots using a snapshot schedule.
  • You can create a maximum of 1,000 in-use snapshot schedules per region.
  • Snapshot schedules apply only in the project that they were created in. Snapshot schedules cannot be used in other projects or organizations.
  • You might need to request an increase in resource quota through the console if you require additional resources in your region.
  • You cannot delete a snapshot schedule if it is attached to a disk. You must detach the schedule from all disks, then delete the schedule.
  • After you create a snapshot schedule, making changes to the description, schedule, and labels is supported in (Preview). To update other values for a snapshot schedule, you must delete the snapshot schedule and create a new one.
  • For persistent disks that use a customer-supplied encryption key (CSEK), you can't create snapshot schedules.
  • For persistent disks that use a customer-managed encryption key (CMEK), all snapshots created with a snapshot schedule are automatically encrypted with the same key.

Snapshot schedule frequency

Before creating a snapshot schedule, choose a start time and a snapshot frequency. The start time is when you want the snapshot schedule to begin. The frequency is defined as hourly, daily, or weekly.

A snapshot schedule begins within the hour of the specified start time. For example, if the scheduled start time is at 12:00, snapshotting starts anytime between 12:00 and 12:59. The schedule uses the UTC time standard.

Snapshot schedules are impacted by daylight savings time (DST) shifts. At the beginning and end of DST in the US, you might see the start times of your snapshot schedule jobs move forward or backward by an hour, respectively, regardless of whether your local timezone uses DST.

  • Setting an hourly schedule: To set up an hourly schedule, choose the start time and choose a snapshot interval using an integer between 1 and 23. A snapshot interval defines how often the snapshot is taken. When setting the interval, choose a number that evenly divides 24 (1, 2, 3, 4, 6, 8, or 12) so that the snapshot starts at the same time interval every day. For example, setting a start time of 9:00 with an interval of 6 indicates the snapshot schedule starts between 9:00 and 10:00 with a snapshot interval of every 6 hours.

  • Setting a daily schedule: Set the frequency to daily for the snapshot to occur once a day. For example, you can set a snapshot schedule so that a snapshot is generated every day at 9:00.

  • Setting a weekly schedule: Set the frequency to weekly for snapshotting multiple days of the week. For example, a start time of 9:00 with a weekly schedule set to Monday, Thursday, Sunday indicates the snapshot schedule occurs at 9:00 on Monday, Thursday, and Sunday every week. You can also set a weekly schedule for different days of the week. For example, you might set a weekly schedule for every Monday at 14:00 and Thursday at 10:00.

Snapshot retention policy

A snapshot retention policy defines how long you want to keep your snapshots.

If you choose to set up a snapshot retention policy, you must do so as part of your snapshot schedule. If you use the gcloud CLI or the Google Cloud console, you must always set a retention policy when creating a snapshot schedule. If you make a request to the API directly, you can omit this field and your snapshots will be retained indefinitely.

If you make a request to the API and choose the default option to retain all your auto-generated snapshots permanently until you delete them manually, keep in mind that you will incur storage costs for retaining these snapshots until you delete them manually.

As a best practice, consider setting up a retention policy to minimize long-term snapshot storage costs.

  • You must specify a snapshot retention policy in number of days with a minimum retention of 1 day.
  • The snapshot retention policy applies only to automatic snapshots, not to those you created manually.
  • You cannot apply a retention policy retroactively. If you create a snapshot schedule without a retention policy and then switch to a snapshot schedule with a retention policy, the retention policy will only apply to the new snapshots.
  • Regardless of the retention policy, you can always manually delete automatically generated and manual snapshots.

Source disk deletion rule

When you create a snapshot schedule, you can also set a source disk deletion rule. The source disk deletion rule controls what happens to your snapshots if the source disk is deleted. By default, if your source disk is deleted, snapshots automatically generated from your snapshot schedule are retained permanently unless you delete them manually, regardless of the original retention policy you set, unless you specify otherwise.

Alternatively, you can set the rule so that your snapshot retention policy is applied to your snapshots if the source disk is deleted. For example, you might apply a retention policy that keeps your snapshots for one week after the source disk is deleted.

For example, assume you created a snapshot schedule with a retention policy of 3 days but choose not to apply the retention policy on source disk deletion. After you apply the policy to your persistent disk, Compute Engine automatically generates snapshots but will also automatically delete snapshots after 3 days, per the retention policy. If your source disk ever gets deleted however, the retention policy no longer applies and all existing snapshots created by this scheduled are retained indefinitely.

Scheduling snapshots for Windows instances

If you are applying a snapshot schedule to disks that are attached to Windows VM instances, use the guest-flush option to enable VSS to generate shadow copies of your snapshots. For more information about snapshots for Windows, see Creating a Windows persistent disk snapshot.

Snapshot locality rules and snapshot labels

Similar to manually generated snapshots, you can also apply resource labels and select the storage location for auto-generated snapshots. If you set these in your snapshot schedule, any auto-generated snapshots from this schedule will have these properties applied to it. For example, if you set a snapshot locality as US, all snapshots generated from this schedule will be stored in the US. Review the examples below for details on setting these options.

Names for scheduled snapshots

Snapshots captured as a part of your snapshot schedule have automatically generated names. The snapshot names are generated according to the following pattern:

[disk-name-prefix]-[scope-name]-[timestamp]-[random-chars]
  • disk-name-prefix: The name of the source disk. This field can be up to 20 characters.
  • scope-name: The name of the zone or region of the source disk.
  • timestamp: The UTC creation timestamp in YYYYMMDDHHMMSS format.
  • random-chars: Randomly generated alphanumeric characters to avoid naming conflicts with other snapshots.

Create a snapshot schedule

Create a snapshot schedule for your persistent disks using the Google Cloud console, Google Cloud CLI, or the Compute Engine API. You must create your snapshot schedule in the same region where your persistent disk resides. For example, if your persistent disk resides in zone us-west1-a, your snapshot schedule must reside in the us-west1 region. For more information, see Selecting the storage location for a snapshot.

Console

  1. In the Google Cloud console, go to the VM instances page.

    Go to VM instances
    The remaining steps will appear automatically in the Google Cloud console.

  2. Select the project that contains your VM instances.
  3. In the Name column, click the name of the VM that has the persistent disk to create a snapshot schedule for.
  4. In Storage, click the name of the Boot disk or the Additional disk to create a snapshot schedule for.
  5. Click Edit.
  6. In Snapshot schedule, choose Create a schedule.
  7. In Name, enter one of the following names for the snapshot schedule:
    • boot-disk-snapshot-schedule
    • attached-persistent-disk-snapshot-schedule
  8. In Location, the default is Multi-regional, which provides higher availability at a higher cost.

    Choose Regional snapshots for more control over the physical location of your data at a lower cost.

  9. To finish creating the snapshot schedule, click Create.
  10. To attach this snapshot schedule to the persistent disk, click Save.

gcloud

To create a snapshot schedule for persistent disks, use the compute resource-policies create snapshot-schedule gcloud command. Set your schedule frequency to hourly, daily, or weekly.

  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] \
      --weekly-schedule-from-file [FILE_NAME] \
      --on-source-disk-delete [DELETION_OPTION]

where:

  • [SCHEDULE_NAME] is the name of the new snapshot schedule.
  • "[SCHEDULE_DESCRIPTION]" is a description of the snapshot schedule. Use quotes around your description.
  • [MAX_RETENTION_DAYS] is the number of days to retain the snapshot. For example, setting 3 would mean that snapshots are retained for 3 days before they are deleted. You must set a retention policy of at least 1 day.
  • [START_TIME] is the UTC start time. The time must start on the hour. For example:
    • 2:00 PM PST is 22:00.
    • If you set a start time of 22:13, you will receive an error.
  • [SNAPSHOT_INTERVAL] defines the interval at which you want snapshotting to occur. Set the hourly schedule using an integer between 1 and 23. Choose an hourly number that is evenly divided into 24. For example, setting --hourly-schedule to 12, means the snapshot is generated every 12 hours. For a weekly schedule define the days you want the snapshotting to occur. You must spell out the week days, they are not case-sensitive. The snapshot frequency flags hourly-schedule, daily-schedule, and weekly-schedule are mutually-exclusive. You must pick one for your snapshot schedule.

  • [FILE_NAME] is the file name that contains the weekly snapshot schedule, if you choose to provide the schedule in this format. Note that you can specify weekly schedules on different days of the week and at different times using a file (but you cannot specify multiple weekly schedules directly on the command-line). For example, your file might specify a snapshot schedule on Monday and Wednesday: [{"day": "MONDAY", "startTime": "04:00"}, {"day": "WEDNESDAY", "startTime": "02:00"}] If you include a start time in your file, you do not need to set the --start-time flag. The schedule uses the UTC time standard.

  • [DELETION_OPTION] determines what happens to your snapshots if the source disk is deleted. Choose either the default keep-auto-snapshots by omitting this flag, or use apply-retention-policy to apply a retention policy.

These are additional examples for setting up a snapshot schedule. In all the following examples:

  • The disk deletion rule is included; the --on-source-disk-delete flag is set to the default of keep-auto-snapshots to permanently keep all auto-generated snapshots. The alternative is to set this flag to apply-retention-policy to use your snapshot retention policy.
  • The storage location is set the US so all generated snapshots will be stored in the US multi-region.
  • The labels env=dev and media=images are applied to all generated snapshots.
  • The retention policy is set to 10 days.

Hourly schedule: In this example, the snapshot schedule starts at 22:00 UTC and occurs every 4 hours.

  gcloud compute resource-policies create snapshot-schedule SCHEDULE_NAME \
      --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

Daily schedule: In this example, the snapshot schedule starts at 22:00 UTC and occurs every day at the same time. The --daily-schedule flag must be present, but not set to anything.

gcloud compute resource-policies create snapshot-schedule SCHEDULE_NAME \
    --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

Weekly schedule: In this example, the snapshot schedule starts at 22:00 UTC and occurs every week on Tuesday and Thursday.

gcloud compute resource-policies create snapshot-schedule SCHEDULE_NAME \
    --description "MY WEEKLY SNAPSHOT SCHEDULE" \
    --max-retention-days 10 \
    --start-time 22:00 \
    --weekly-schedule tuesday,thursday \
    --region us-west1 \
    --on-source-disk-delete keep-auto-snapshots \
    --snapshot-labels env=dev,media=images \
    --storage-location US

API

In the API, construct a POST request to resourcePolicies.insert to create a snapshot schedule. At the minimum, you must include the snapshot schedule name, snapshot storage regional location, and snapshot frequency.

By default, the onSourceDiskDelete parameter is set to keepAutoSnapshots. This means that if the source disk is deleted, the auto-generated snapshot for that disk is retained indefinitely. Alternatively, you can set the flag to applyRetentionPolicy to apply your retention policy.

The following example sets a daily snapshot schedule that starts at 12:00 UTC and repeats every day. The example also sets a retention policy of 5 days; after 5 days, snapshots are automatically removed.

You can also include snapshot locality options and snapshot labels in your request to ensure your snapshots are stored in the location of your choice.

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": ["US"]
   }
 }
}

where:

  • [PROJECT_ID] is the project name.
  • [REGION] is the location of the snapshot schedule resource policy.
  • [SCHEDULE_DESCRIPTION] is the description of the snapshot schedule.
  • [SCHEDULE_NAME] is the name of the snapshot schedule.

Similarly, you can create a weekly or monthly schedule. Review the API reference for details specific to setting a weekly or monthly schedule.

For example, the following request creates a weekly schedule that runs on Tuesday and Thursday, at 9:00 and 2:00 respectively.

POST https://compute.googleapis.com/compute/v1/projects/[PROJECT_ID]/regions/[REGION]/resourcePolicies

{
 "name": "[SCHEDULE_NAME]",
 "description": "[SCHEDULE_DESCRIPTION]",
 "snapshotSchedulePolicy": {
   "schedule": {
     "weeklySchedule": {
       "dayOfWeeks": [
       {
         "day": "Monday",
         "startTime": "9:00"
       },
       {
         "day": "Thursday",
         "startTime": "2:00"
       }
       ]
     }
   },
  "retentionPolicy": {
    "maxRetentionDays": "5"
  },
  "snapshotProperties": {
    "guestFlush": "False",
    "labels": {
      "production": "webserver"
    },
    "storageLocations": ["US"]
  }
 }
}

Attach a snapshot schedule to a disk

Once you have a schedule, attach it to an existing disk. Use the console, gcloud command, or the Compute Engine API method.

Console

Attach a snapshot schedule to an existing disk.

  1. In the Google Cloud console, go to the Disks page.

    Go to the Disks page

  2. Click the name of the disk to which you want to attach a snapshot schedule. This opens the disk details page.
  3. At the top of the disk details page, click Edit.
  4. Use the Snapshot schedule drop-down menu to add the schedule to the disk. Or create a new schedule.
  5. If you created a new schedule, click Create.
  6. Click Save to complete the task.

gcloud

To attach a snapshot schedule to a disk, use the disks add-resource-policies gcloud command.

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

where:

  • [DISK_NAME] is the name of the existing disk.
  • [SCHEDULE_NAME] is the name of the snapshot schedule.
  • [ZONE] is the location of your disk.

API

In the API, construct a POST request to disks.addResourcePolicies to attach a snapshot schedule to an existing disk.

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

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

where:

  • [PROJECT_ID] is the project name.
  • [ZONE] is the location of the disk.
  • [REGION] is the location of the snapshot schedule.
  • [DISK_NAME] is the name of the disk.
  • [SCHEDULE_NAME] is the name of the snapshot schedule in that region you are applying to this disk.

Create a disk with a snapshot schedule

In the Google Cloud console and gcloud only, you can create a disk and a snapshot schedule at the same time.

Console

  1. In the Google Cloud console, go to the Disks page.

    Go to the Disks page

  2. At the top of the page, click Create Disk.
  3. Complete the required fields to create a zonal or regional disk.
  4. Create your disk in the same region as your snapshot schedule.
  5. Populate the fields for your Snapshot schedule.
  6. Use the drop-down menu and complete the fields to create the schedule.
  7. Click Create to create the schedule.
  8. Click Create to create the disk.

gcloud

Use the disks create gcloud command to create a zonal or regional persistent disk and attach a snapshot schedule to it.

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

where:

  • [DISK_NAME] is the name of the new disk.
  • [SCHEDULE_NAME] is the name of the snapshot schedule.
  • [ZONE] is the location where you are creating the disk. The disk must be in a zone within the snapshot schedule's region.

View snapshot schedules

To get a list of snapshot schedules, use the console, gcloud command, or the Compute Engine API method. This request displays the name, description, and region of all snapshot schedules within a project.

Console

  1. In the Google Cloud console, go to the Snapshots page.

    Go to the Snapshots page

  2. Click on Snapshot schedules at the top of page.
  3. Use the Filter snapshot schedules search box to review a specified snapshot schedule.
  4. Click on a snapshot schedule to see its details.

gcloud

To see a list of your snapshot schedules, use the resource-policies list gcloud command.

gcloud compute resource-policies list

To see the description of a specific snapshot schedule, use the resource-policies describe gcloud command.

gcloud compute resource-policies describe [SCHEDULE_NAME]

where: [SCHEDULE_NAME] is the name of the snapshot schedule.

API

Make a GET a request to resourcepolicies.aggregatedList to return a list of the snapshot schedules in a project.

GET https://compute.googleapis.com/compute/v1/projects/[PROJECT_ID]/aggregated/resourcePolicies

where [PROJECT_ID] is the project name.

View a snapshot schedule by region

To view a snapshot schedule within a particular region, use the console, gcloud command, or the Compute Engine API method.

Console

  1. In the Google Cloud console, go to the Snapshots page.

    Go to the Snapshots page

  2. Click on Snapshot schedules at the top of page.
  3. Use the Filter snapshot schedules search box to review a specified snapshot schedule by region.

gcloud

To view the snapshot schedules in a specific region, use the resource-policies list gcloud command.

gcloud compute resource-policies list [PROJECT_ID] \
    --filter [REGION]

where:

  • [PROJECT_ID] is the project name.
  • [REGION] is the location of the resource policy.

API

Make a request to resourcePolicies.get to retrieve the snapshot schedule established in a region.

GET https://compute.googleapis.com/compute/v1/projects/[PROJECT_ID]/regions/[REGION]/resourcePolicies/[SCHEDULE_NAME]

where:

  • [PROJECT_ID] is the project name.
  • [REGION] is the location of the snapshot schedule.
  • [SCHEDULE_NAME] is the name of the snapshot schedule you want to view.

Change a snapshot schedule

After you create a snapshot schedule, changing the description, schedule frequency, and labels is supported in Preview. The snapshot schedule updates take effect with the next scheduled run of the snapshot schedule. Any currently running snapshots will finish before the new updates take effect.

To update other values for a snapshot schedule, you must delete the snapshot schedule and create a new one.

Update a snapshot schedule

You can use Google Cloud CLI or the Compute Engine API to change the following properties of your snapshot schedule:

  • Description
  • Snapshot schedule
  • Label added to the snapshots

If you want to change other properties of your snapshot schedule, use the method described in Change a snapshot schedule by replacement.

gcloud

Use the gcloud beta compute resource-policies update snapshot-schedule command to update a snapshot schedule's description, schedule, or labels.

gcloud beta compute resource-policies update snapshot-schedule SCHEDULE_NAME \
   --region=REGION \
   --description="DESCRIPTION" \
   --start-time=START_TIME \
   SCHEDULE_FLAG \
   --snapshot-labels="KEY=VALUE"

Replace the following:

  • SCHEDULE_NAME: the name of the snapshot schedule.
  • REGION: the region where your snapshot schedule resides.
  • DESCRIPTION: a description of the snapshot schedule. Use quotes around your description.
  • START_TIME: the UTC start time. The time must start on the hour. For example:
    • 2:00 PM PST is 22:00.
    • If you set a start time of 22:13, you will receive an error.
  • SCHEDULE_FLAG: one of the following flags:

    • --hourly-schedule=HOURLY_INTERVAL: the number of hours between every snapshot. The HOURLY_INTERVAL must be an integer between 1 and 23. For example, setting --hourly-schedule to 12, means the snapshot is generated every 12 hours.
    • --daily-schedule: performs a snapshot daily, at the START_TIME
    • --weekly-schedule=WEEKLY_INTERVAL: defines the days you want the snapshotting to occur. You must spell out the week days; they are not case-sensitive.

    • --weekly-schedule-from-file=FILE_NAME: specifies a file that contains the weekly snapshot schedule. You can specify weekly schedules on different days of the week and at different times using a file. For example, your file might specify a snapshot schedule on Monday and Wednesday: [{"day": "MONDAY", "startTime": "04:00"}, {"day": "WEDNESDAY", "startTime": "02:00"}] If you include a start time in your file, you do not need to set the --start-time flag. The schedule uses the UTC time standard. The time must start on the hour. For example:

      • 2:00 PM PST is 22:00.
      • If you set a start time of 22:13, you will receive an error.

    The snapshot frequency flags hourly-schedule, daily-schedule, weekly-schedule, and weekly-schedule-from-file are mutually-exclusive. You can use only one for your snapshot schedule.

  • KEY and VALUE: a key-value pair that can be used to group related or associated resources.

Examples:

To change a snapshot schedule to a daily schedule:

gcloud beta compute resource-policies update snapshot-schedule SCHEDULE_NAME \
 --region=REGION --daily-schedule --start-time=START_TIME

To change a snapshot to an hourly schedule, and also update the description and snapshot label:

gcloud beta compute resource-policies update snapshot-schedule SCHEDULE_NAME \
 --region=REGION --description="DESCRIPTION" \
 --hourly-schedule=HOURLY_INTERVAL --start-time=START_TIME \
 --snapshot-labels="KEY=VALUE"

API

Update a snapshot schedule's description, schedule, or labels using the resourcePolicies method.

  • Change the description and label:

    PATCH https://compute.googleapis.com/compute/beta/projects/PROJECT_ID/regions/REGION/resourcePolicies/SCHEDULE_NAME
    {
        "name": "SCHEDULE_NAME",
        "description": "DESCRIPTION",
        "snapshotProperties": {
              "labels": {"KEY": "VALUE"}
        }
    }
    
  • Change the snapshot schedule to hourly:

    PATCH https://compute.googleapis.com/compute/beta/projects/PROJECT_ID/regions/REGION/resourcePolicies/SCHEDULE_NAME
    {
        "name": "SCHEDULE_NAME",
        "snapshotSchedulePolicy": {
            "schedule": {
              "hourlySchedule": {
                  "hoursInCycle": HOURLY_INTERVAL,
                  "startTime": START_TIME
               }
            }
        }
    }
    
  • Change the snapshot schedule to daily:

    PATCH https://compute.googleapis.com/compute/beta/projects/PROJECT_ID/regions/REGION/resourcePolicies/SCHEDULE_NAME
    {
        "name": "SCHEDULE_NAME",
        "snapshotSchedulePolicy": {
            "schedule": {
              "dailySchedule": {
                  "daysInCycle": DAILY_INTERVAL,
                  "startTime": START_TIME
               }
            }
        }
    }
    
  • Change the snapshot schedule to weekly:

    PATCH https://compute.googleapis.com/compute/beta/projects/PROJECT_ID/regions/REGION/resourcePolicies/SCHEDULE_NAME
    {
        "name": "SCHEDULE_NAME",
        "snapshotSchedulePolicy": {
            "schedule": {
               "weeklySchedule": {
                  "dayOfWeeks": [
                   {
                      "day": WEEKLY_INTERVAL,
                      "startTime": START_TIME
                   }
                  ]
               }
            }
        }
    }
    

Replace the following:

  • PROJECT_ID: the project name.
  • REGION: the region where the snapshot schedule is located.
  • SCHEDULE_NAME: the name of the snapshot schedule.
  • DESCRIPTION: a description of the snapshot schedule. Use quotes around your description.
  • KEY and VALUE: a key- value pair that can be used to group related or associated resources.
  • HOURLY_INTERVAL: defines the interval at which you want snapshotting to occur. Set the hourly schedule using an integer between 1 and 23. To have the snapshots created at the same time every day, choose a number that divides evenly into 24 (1, 2, 3, 4, 6, 8, or 12). For example, setting --hourly-schedule to 12, means the snapshot is generated every 12 hours.
  • DAILY_INTERVAL: defines the number of days between each snapshot.
  • WEEKLY_INTERVAL: defines a schedule that runs on specific days of the week. Specify one or more days. The following options are available: MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, and SUNDAY. You must spell out the week days; they are not case-sensitive. You can define up to 7 intervals for dayOfWeeks, one for each day of the week.
  • START_TIME is the UTC start time. The time must start on the hour. For example:
    • 2:00 PM PST is 22:00.
    • If you set a start time of 22:13, you will receive an error.

Change a snapshot schedule by replacement

After you create a snapshot schedule, you can modify the description, schedule frequency, and labels, as described in Update a snapshot schedule (Preview). To update other values for a snapshot schedule, you must delete the snapshot schedule and create a new one.

If you are replacing a snapshot schedule that is already attached to a disk, you must first detach the schedule from the disk and delete it. Then you can create a new schedule, and attach it to the disk. Snapshots that are generated from the detached snapshot schedule will not be managed by the new policy. Those snapshots will be retained indefinitely until you delete them.

Use the Google Cloud console, Google Cloud CLI, or the Compute Engine API to remove and replace your snapshot schedule.

Console

  1. In the Google Cloud console, go to the Disks page.

    Go to the Disks page

  2. Select the disk that has the schedule you want to detach.
  3. Click Edit at the top of the page.
  4. Open the Snapshot schedule drop-down menu.
  5. Click No schedule to detach the schedule from the disk.
  6. You can create a new schedule, or swap the schedule while you are editing the disk options.
  7. Click Save to complete the task.

gcloud

Use the disks remove-resource-policies command to detach the snapshot schedule from the disk with the schedule you want to change.

gcloud compute disks remove-resource-policies DISK_NAME \
    --resource-policies SCHEDULE_NAME \
    --region REGION \
    --zone ZONE

where:

  • DISK_NAME is the name of the disk with the snapshot schedule attached to it.
  • SCHEDULE_NAME is the name of the snapshot schedule you want to detach from this disk.
  • REGION is where your snapshot schedule resides.
  • ZONE is the zone where your zonal disk resides.

Then, use the disks add-resource-policies command to add the updated snapshot schedule to the disk.

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

where:

  • DISK_NAME is the name of the disk with the snapshot schedule resource policy.
  • SCHEDULE_NAME is the name of the snapshot schedule you want to add to this disk.
  • ZONE is the zone where your disk resides.

API

To detach a schedule from a disk, construct a POST request to disks.removeResourcePolicies.

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

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

where:

  • PROJECT_ID is the project name.
  • ZONE is the zone where the disk resides.
  • DISK_NAME is the name of the disk with the associated snapshot schedule.
  • REGION is the location of the snapshot schedule.
  • SCHEDULE_NAME is the name of the snapshot schedule you are removing from this disk.

In the API, construct a POST request to disks.addResourcePolicies to attach a snapshot schedule to an existing disk.

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

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

where:

  • PROJECT_ID is the project name.
  • ZONE is the location of the disk.
  • DISK_NAME is the name of the disk.
  • REGION is the location of the snapshot schedule.
  • SCHEDULE_NAME is the name of the snapshot schedule in that region you are applying to this disk.

Deleting a snapshot schedule

If you delete a snapshot schedule, all auto-generated snapshots associated with the snapshot schedule are kept permanently. However, after the schedule is deleted, it can no longer generate snapshots.

Your retention policy is part of your snapshot schedule. After the schedule is deleted, your retention policy no longer applies. Snapshots that have already been generated are kept permanently until you manually delete them.

To delete an existing snapshot schedule, use the Google Cloud console, Google Cloud CLI, or the Compute Engine API method. If the schedule is already attached to a disk, detach the schedule from the disk first, then delete the schedule. You cannot delete a snapshot schedule that is attached to a disk.

Console

  1. In the Google Cloud console, go to the Snapshots page.

    Go to the Snapshots page

  2. Select Snapshot schedules to see a list of your schedules.
  3. Select any schedule not associated with a disk.
  4. Click Delete at the top of the page.

gcloud

To delete a specified snapshot schedule, use the resource-policies delete gcloud command.

gcloud compute resource-policies delete [SCHEDULE_NAME] \
    --region [REGION]

where:

  • [SCHEDULE_NAME] is the name of the snapshot schedule.
  • [REGION] is the location of the snapshot schedule.

API

Make a request to resourcepolicies.delete to delete a snapshot schedule. If the snapshot schedule is already attached to a specific disk, you will receive an error.

DELETE https://compute.googleapis.com/compute/v1/projects/[PROJECT_ID]/regions/[REGION]/resourcePolicies/[SCHEDULE_NAME]

where:

  • [PROJECT_ID] is the project name.
  • [REGION] is the location of the snapshot schedule.
  • [SCHEDULE_NAME] is the name of the snapshot schedule.

Logging and monitoring

Every scheduled snapshot associated with a disk is continuously creating a system event, which is monitored and logged at all times. The system event audit logs are always enabled.

These logs provide behavioral information about your scheduled snapshots for each associated disk. You can view your logs from the Cloud Logging menu within the console.

  1. In the Google Cloud console, go to the Logs Explorer page.

    Go to the Logs Explorer page

  2. In the Audited resource drop-down list, point to Disk and select All disk_id.

  3. In the All logs drop-down list, select cloudaudit.googleapis.com/system_event and click OK.

  4. In the Any log level drop-down list, select the log type.

Set up alerts for scheduled snapshots

You can create a custom metric to raise alerts or provide information to troubleshoot problems with scheduled snapshots.

For example, to set up an alert for scheduled snapshot failures, use the following procedure:

  1. Create a log filter to capture scheduled snapshot events.
  2. Create a metric based off of the log filter that counts scheduled snapshot failures.
  3. Create an alert policy to send an alert when there is a scheduled snapshot failure.

Create a log filter

  1. In the Google Cloud console, go to the Cloud Logging > Logs Explorer page.

    Go to the Logs Explorer page

  2. In the Filter by label or text search list, select Convert to advanced filter.

    Convert to advanced filter.

  3. Replace the filter text box with the following text, replacing project-id with your project ID:

    resource.type="gce_disk"
    logName="projects/project-id/logs/cloudaudit.googleapis.com%2Fsystem_event"
    protoPayload.methodName="ScheduledSnapshots"
    severity>"INFO"
    
  4. Click Submit Filter.

Create a metric

  1. In the Logs Explorer, at the top of the page, click Create metric.

  2. In the Metric Editor, enter the following:

    • Name: scheduled_snapshot_failure_count.
    • Description: count of scheduled snapshot failures.
    • Type: Counter
  3. Under Labels, click Add item and enter the following:

    • Name: status
    • Description: status of scheduled snapshot request
    • Label type: String
    • Field name: protoPayload.response.status
  4. Click Done.

  5. Click Create Metric.

Create an alert policy

  1. In the Google Cloud console, go to the Cloud Logging > Logs-based metrics page.

    Go to the Logs-based metrics page

  2. Under User-defined Metrics, find your new metric named user/scheduled_snapshot_failure_count.

  3. Click the More menu button in this row and select Create alert from metric. The alert policy condition creation page opens.

    User-defined metric.

  4. In the Target panel, under Aggregator, select none.

  5. Under Filter:

    1. Click Add a filter.
    2. Select status from the list.
    3. In the Value field, type DONE.
    4. Click Apply.

      Alert filter status.

  6. Click Show advanced options.

  7. In the Advanced aggregation pane, click the Aligner list and select sum.

  8. In the Configuration panel, select the following values:

    • Condition triggers if: Any time series violates
    • Condition: is above
    • Threshold: 0
    • For: most recent value

      Configuration panel.

  9. Click Save.

  10. On the Create new alerting policy page, enter a policy name. Optionally, you can add notification channels and documentation for this policy.

  11. Click Save.

What's next