Skip to content

Commit

Permalink
Add script to snapshot only running instances disks
Browse files Browse the repository at this point in the history
  • Loading branch information
p1gmale0n committed Jul 3, 2018
1 parent 04e87a7 commit fcc4281
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions google-cloud-auto-snapshot-running-instances.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/bin/bash

if [ -z "${DAYS_RETENTION}" ]; then
# Default to 60 days
DAYS_RETENTION=60
fi

# loop through all disks of running instances within this project and create a snapshot
gcloud compute instances list --filter="STATUS:RUNNING" --format='value(name)'| while read -r INSTANCE_NAME; do
gcloud compute disks list --filter="NAME:${INSTANCE_NAME}" --format='value(name,zone)'| while read -r DISK_NAME ZONE; do
gcloud compute disks snapshot "${DISK_NAME}" --snapshot-names autogcs-"${DISK_NAME:0:31}"-"$(date '+%Y-%m-%d-%s')" --zone "${ZONE}"
done
done
#
# snapshots are incremental and dont need to be deleted, deleting snapshots will merge snapshots, so deleting doesn't loose anything
# having too many snapshots is unwiedly so this script deletes them after n days
#
if [[ $(uname) == "Linux" ]]; then
from_date=$(date -d "-${DAYS_RETENTION} days" "+%Y-%m-%d")
else
from_date=$(date -v -${DAYS_RETENTION}d "+%Y-%m-%d")
fi
gcloud compute snapshots list --filter="creationTimestamp<${from_date} AND name~'autogcs.*'" --uri | while read -r SNAPSHOT_URI; do
gcloud compute snapshots delete "${SNAPSHOT_URI}" --quiet
done
#

0 comments on commit fcc4281

Please sign in to comment.