Skip to content

Instantly share code, notes, and snippets.

@brews
Last active December 2, 2020 21:22
Show Gist options
  • Select an option

  • Save brews/357f427d37e1716c2eff249f1a9f1c15 to your computer and use it in GitHub Desktop.

Select an option

Save brews/357f427d37e1716c2eff249f1a9f1c15 to your computer and use it in GitHub Desktop.
Example bash script to create a basic Google Compute Engine (GCE) disk backup policy (schedule snapshot) and apply the policy to GCE disks in a zone.
#!/usr/bin/env bash
# 2020-01-02
# Create a basic GCE disk backup policy (schedule snapshot) and
# apply the policy to GCE disks in a zone.
set -e
TARGET_POLICY="backup-schedule"
ZONE="us-west1-a"
REGION="us-west1"
PROJECT="compute-impactlab"
# Creating basic backup snapshot schedule. Don't need this if schedule already exists.
gcloud compute resource-policies create snapshot-schedule $TARGET_POLICY \
--max-retention-days 90 \
--start-time 12:00 \
--weekly-schedule sunday \
--on-source-disk-delete apply-retention-policy \
--snapshot-labels type=backup,managed-by=backup-schedule \
--region $REGION \
--storage-location $REGION
# Apply snapshot schedule to all disks in query.
for DISK in $(gcloud compute disks list --project compute-impactlab --filter="zone:( $ZONE )" --uri)
do
gcloud compute disks add-resource-policies $DISK \
--project $PROJECT \
--zone $ZONE \
--resource-policies=$TARGET_POLICY
done
@brews

brews commented Dec 2, 2020

Copy link
Copy Markdown
Author

Ug. I'm terrible about properly quoting and escaping my variables.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment