Creating scheduled snapshots for persistent disk

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. After creating a snapshot schedule, you can apply it to one or more persistent disks.

A snapshot schedule includes the following properties:

  • schedule name
  • schedule description
  • snapshot frequency (hourly, daily, weekly)
  • snapshot retention policy to determine the number of days to keep your snapshots
  • 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

A snapshot schedule also includes an optional retention policy which defines how long to keep snapshots that are generated from the snapshot schedule. If you do not specify a retention policy, snapshots are retained indefinitely.

Before you begin

Restrictions

  • A persistent disk can have only one snapshot schedule attached to it at a time.
  • There is a maximum of 1000 disks per schedule.
  • 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.
  • Once created, a snapshot schedule is immutable. To update a snapshot schedule, you must delete it and create a new one.
  • You cannot create snapshot schedules for persistent disks that use Customer Supplied Encryption Key (CSEK) at this time.

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:00PM, snapshotting starts anytime between 12:00PM and 12:59PM. If you are using the console or the gcloud tool, you can supply either a UTC or a local time. If you are using the Compute Engine API, you must supply the time in UTC.

  • 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, one option is to choose a number that is evenly divided into 24. For example, setting a start time of 9:00AM with an interval of 6 indicates the snapshot schedule runs at 9:00AM 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:00AM.

  • Setting a weekly schedule: Set the frequency to weekly for snapshotting multiple days of the week. For example, a start time of 9:00AM with a weekly schedule set to Monday, Thursday, Sunday indicates the snapshot schedule occurs at 9:00AM 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 2:00PM and Thursday at 10:00AM.

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 do not set a retention policy, the default behavior is to retain all your auto-generated snapshots permanently until you delete them manually. If you choose the default option to keep snapshots indefinitely, keep in mind that you might incur storage costs for retaining these snapshots. 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 generate 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 snaphots 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 additional information about snapshots for Windows, read 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.

Creating a snapshot schedule

Create a snapshot schedule for your persistent disks using the console, gcloud command, or the Compute Engine API method. 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, read Selecting the storage location for a snapshot.

Console

To create a schedule for your snapshot:

  1. Go to the Snapshots page in the Google Cloud Platform Console.

    Go to the Snapshots page

  2. At the top of the page, click Create Snapshot Schedule.
  3. Enter a schedule Name.
  4. Select a Region for the snapshot schedule. Your persistent disks must reside in the zone(s) within the region that contains the snapshot schedule.
  5. Optionally, select your Snapshot location or accept the default selection.
  6. Select your Schedule frequency.
  7. Enter the number of days to Autodelete snapshots after.
  8. Select your Deletion rule to determine what happens to snapshots if the schedule's source disk is deleted.
  9. Choose the number of days to retain snapshots generated from this schedule.
  10. If you are using Windows, you can Enable VSS.
  11. Optionally, specify any relevant labels you want to apply to these snapshots.
  12. Click Create to create the snapshot schedule.

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 start time in local time or UTC. For example: 2:00PM is 14:00. The time must start on the hour. For example, if you set a start time of 14: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 time can be set in UTC or your local time zone.

  • [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 2:00PM GMT 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 14: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 2:00PM 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 14: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 2:00PM 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 14: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 time, 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://www.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 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://www.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"]
  }
 }
}

Attaching 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. Go to the Disks page in the Google Cloud Platform Console.

    Go to the Disks page

  2. Select the disk you want to add the schedule to.
  3. At the top of the 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://www.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.

Creating a disk with a snapshot schedule

In the GCP Console and gcloud only, you can create a disk and a snapshot schedule at the same time.

Console

  1. Go to the Disks page in the Google Cloud Platform Console.

    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.

Viewing 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. Go to the Snapshots page in the Google Cloud Platform Console.

    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://www.googleapis.com/compute/v1/projects/[PROJECT_ID]/aggregated/resourcePolicies

where [PROJECT_ID] is the project name.

Viewing 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. This request displays the name, description, and region of all snapshot schedules within a project.

Console

  1. Go to the Snapshots page in the Google Cloud Platform Console.

    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 resource policy 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 policy.

API

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

GET https://www.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.

Changing a snapshot schedule

You cannot edit a snapshot schedule. To change a 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. Use the console, gcloud command, or the Compute Engine API method to make changes to your snapshot schedule.

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.

Console

  1. Go to the Disks page in the Google Cloud Platform Console.

    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 gcloud 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 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://www.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://www.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, once the schedule is deleted, it can no longer generate snapshots.

Your retention policy is part of your snapshot schedule. Once 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 console, gcloud command, 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. Go to the Snapshots page in the Google Cloud Platform Console.

    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://www.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 Stackdriver menu within the console. Additionally, you can create a custom metric to raise alerts or provide information to troubleshoot a snapshot schedule problem.

Compute Engine provides offline alert monitoring via the console. Check the Logging section under Stackdriver to view your scheduled snapshot alerts:

  1. Go to the Stackdriver Logging > Logs page in the GCP Console:
    Go to the Logs Viewer page
  2. Click on Logs Viewer in the left navigation.
  3. Select Disk from the first drop-down menu.
  4. Select cloudaudit.googleapis.com/system_event from the All logs drop-down menu and click OK.
  5. Select the log type from the Any log level drop-down menu.

Logs viewer UI

To set up and monitor alerts, read the Stackdriver Error Reporting documentation.

What's next

Was deze pagina nuttig? Laat ons weten hoe goed we u hebben geholpen:

Feedback verzenden over...

Compute Engine Documentation