Last active
January 14, 2018 14:35
-
-
Save z1nkum/8d1d45c2fd79409b7b0c0e23c758e653 to your computer and use it in GitHub Desktop.
cronjob backup all disks for Google Cloud Instances
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
#!/bin/sh | |
# 1) crontab -l: 15 1 * * * /root/make_my_disks_snapshots.sh | |
# 2) Add service account snapshots-rw@[project-id].iam.gserviceaccount.com with Compute Storage Admin role attached | |
# 3) Set this account to instance | |
S_DATE=`date +"%Y%m%d-%H%M%S"` | |
S_NAME=`hostname` | |
ZONE=`curl -s -m 10 "http://metadata.google.internal/computeMetadata/v1/instance/zone" -H "Metadata-Flavor: Google" | grep -Po '[^/]+$'` | |
for OUTPUT in $(curl -s -m 10 "http://metadata.google.internal/computeMetadata/v1/instance/disks/" -H "Metadata-Flavor: Google") | |
do | |
S_DISK_NAME=`curl -s -m 10 "http://metadata.google.internal/computeMetadata/v1/instance/disks/${OUTPUT}device-name" -H "Metadata-Flavor: Google"` | |
S_FULL_NAME="${S_DISK_NAME}-${S_DATE}" | |
echo "Snapshoting: ${S_FULL_NAME}" | |
gcloud compute disks snapshot $S_DISK_NAME --snapshot-names $S_FULL_NAME --zone $ZONE | |
done | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment