https://cloud.google.com/compute/docs/reference/latest/snapshots
gcloud compute disks snapshot [DISK_NAME]

gcloud compute instances stop jenkins-master --zone $CPO200_ZONE
gcloud compute instances describe jenkins-master --zone $CPO200_ZONE | grep status
gcloud compute disks snapshot jenkins-master --snapshot-name jenkins-snapshot-1 --zone $CPO200_ZONE
gcloud compute snapshots describe jenkins-snapshot-1 --format yaml
gcloud compute instances start jenkins-master --zone $CPO200_ZONE
gcloud compute instances describe jenkins-master --zone $CPO200_ZONE | grep status
ALT_ZONE=<alternative-zone>
gcloud compute disks create jenkins-restore --source-snapshot jenkins-snapshot-1 --type pd-ssd --zone $ALT_ZONE
gcloud compute instances create jenkins-restore --subnet build-subnet --tags ssh,http --disk name=jenkins-restore,device-name=jenkins-restore,mode=rw,boot=yes --zone $ALT_ZONE
gcloud compute disks list
gcloud compute disks create data-01 --size 200GB --zone $CPO200_ZONE
gcloud compute instances attach-disk jenkins-master --disk data-01 --device-name disk01 --zone $CPO200_ZONE
ls -d /dev/disk/by-id/* | grep disk01
sudo mkdir -p /mnt/disk01 /mnt/disk02
sudo mkfs.ext4 -F -E lazy_itable_init=0,lazy_journal_init=0,discard /dev/disk/by-id/google-disk01
sudo mount -o discard,defaults /dev/disk/by-id/google-disk01 /mnt/disk01
ls /mnt/disk01
sudo rsync -r /home /mnt/disk01
sync
exit
gcloud compute disks snapshot data-01 --snapshot-names data-01-snap --zone $CPO200_ZONE
gcloud compute disks create data-02 --source-snapshot data-01-snap --zone $CPO200_ZONE
gcloud compute instances attach-disk jenkins-master --disk data-02 --device-name disk02 --zone $CPO200_ZONE
ls -d /dev/disk/by-id/* | grep disk02
sudo mount /dev/disk/by-id/google-disk02 /mnt/disk02
sudo diff -rq /mnt/disk01 /mnt/disk02
sudo umount /mnt/disk01 /mnt/disk02
exit
gcloud compute instances detach-disk jenkins-master --disk data-01 --zone $CPO200_ZONE
gcloud compute instances detach-disk jenkins-master --disk data-02 --zone $CPO200_ZONE
gcloud compute instances delete jenkins-restore --zone $ALT_ZONE
gcloud compute snapshots delete jenkins-snapshot-1 data-01-snap
gcloud compute disks delete data-01 data-02 --zone $CPO200_ZONE
https://github.com/jacksegal/google-compute-snapshot
Simple shell script to take a daily snapshot and delete all snapshots over 7 days:
#!/usr/bin/env bash
export PATH=$PATH:/usr/local/bin/:/usr/bin
#
# CREATE DAILY SNAPSHOT
#
# get the device name for this vm
DEVICE_NAME="$(curl -s "http://metadata.google.internal/computeMetadata/v1/instance/disks/0/device-name" -H "Metadata-Flavor: Google")"
# get the device id for this vm
DEVICE_ID="$(curl -s "http://metadata.google.internal/computeMetadata/v1/instance/id" -H "Metadata-Flavor: Google")"
# get the zone that this vm is in
INSTANCE_ZONE="$(curl -s "http://metadata.google.internal/computeMetadata/v1/instance/zone" -H "Metadata-Flavor: Google")"
# strip out the zone from the full URI that google returns
INSTANCE_ZONE="${INSTANCE_ZONE##*/}"
# create a datetime stamp for filename
DATE_TIME="$(date "+%s")"
# create the snapshot
echo "$(gcloud compute disks snapshot ${DEVICE_NAME} --snapshot-names gcs-${DEVICE_NAME}-${DEVICE_ID}-${DATE_TIME} --zone ${INSTANCE_ZONE})"
#
# DELETE OLD SNAPSHOTS (OLDER THAN 7 DAYS)
#
# get a list of existing snapshots, that were created by this process (gcs-), for this vm disk (DEVICE_ID)
SNAPSHOT_LIST="$(gcloud compute snapshots list --regexp "(.*gcs-.*)|(.*-${DEVICE_ID}-.*)" --uri)"
# loop through the snapshots
echo "${SNAPSHOT_LIST}" | while read line ; do
# get the snapshot name from full URL that google returns
SNAPSHOT_NAME="${line##*/}"
# get the date that the snapshot was created
SNAPSHOT_DATETIME="$(gcloud compute snapshots describe ${SNAPSHOT_NAME} | grep "creationTimestamp" | cut -d " " -f 2 | tr -d \')"
# format the date
SNAPSHOT_DATETIME="$(date -d ${SNAPSHOT_DATETIME} +%Y%m%d)"
# get the expiry date for snapshot deletion (currently 7 days)
SNAPSHOT_EXPIRY="$(date -d "-7 days" +"%Y%m%d")"
# check if the snapshot is older than expiry date
if [ $SNAPSHOT_EXPIRY -ge $SNAPSHOT_DATETIME ];
then
# delete the snapshot
echo "$(gcloud compute snapshots delete ${SNAPSHOT_NAME} --quiet)"
fi
done
No comments:
Post a Comment