Skip to content

Instantly share code, notes, and snippets.

@mikejk8s
Created January 20, 2017 20:22
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikejk8s/81e662f4c1f794bf2682aec4e32c0570 to your computer and use it in GitHub Desktop.
Save mikejk8s/81e662f4c1f794bf2682aec4e32c0570 to your computer and use it in GitHub Desktop.
gcp autosnapshot
#!/bin/bash
# loop through all disks within this project and create a snapshot
gcloud compute disks list --format='value(name,zone)'| while read DISK_NAME ZONE; do
gcloud compute disks snapshot $DISK_NAME --snapshot-names gcs-$DISK_NAME-$(date "+%Y-%m-%d-%s") --zone $ZONE
done
#
# snapshots are incremental and dont need to be deleted, deleting snapshots will merge snapshots, so deleting doesn't lose anything
# having too many snapshots is unwieldly so this script deletes them after 60 days
#
gcloud compute snapshots list --filter="creationTimestamp<$(date -d "-2 days" "+%Y-%m-%d")" --regexp "(gcs.*)" --uri | while read SNAPSHOT_URI; do
gcloud compute snapshots delete $SNAPSHOT_URI
done
#
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment