Last active
December 2, 2020 21:22
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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 |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Ug. I'm terrible about properly quoting and escaping my variables.