-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add script to snapshot only running instances disks
- Loading branch information
Showing
1 changed file
with
26 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
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 | ||
# |