How to create and rotate Automatic Snapshot?

前端 未结 10 2078
梦如初夏
梦如初夏 2020-12-14 08:33

On Compute Engine we can do Snapshots, which are basically backups. Could you try to figure out how we could create a script to do automated snapshots every day and keep lik

10条回答
  •  醉话见心
    2020-12-14 09:04

    Script assumes $HOSTNAME is the same as disk-name (my primary system disk assumes the same name as the the VM instance or $HOSTNAME -- (change to your liking) ultimately, wherever it says $HOSTNAME, it needs to point to the system disk on your VM.

    gcloud creates incremental diff snapshots. The oldest will contain the most information. You do not need to worry about creating a full snapshot. Deleting the oldest will make the new oldest snapshot the primary that future incrementals will base from. This is all done on Google side logic -- so it is automagic to gcloud.

    We have this script set to run on a cron job every hour. It creates a incremental snapshot (abt 1 to 2GB) and deletes any that are older than retention days. Google magically resizes the oldest snapshot (which was previously an incremental) to be the base snapshot. You can test this by deleting the base snapshot and refreshing the snapshot list (console.cloud.google.com) -- the "magic" occurs in the background and you may need to give it a minute or so to rebase itself. Afterwards, you'll notice the oldest snapshot is the base and it's size will reflect the used portion of the disk that you are performing the snapshot on.

    $> snapshot

    #!/bin/bash
    
        . ~/.bash_profile > /dev/null 2>&1  # source environment for cron jobs
        
        retention=7 #days
        
        zone=`gcloud info|grep zone:|awk -F\[ '{print $2}'|awk -F\] '{print $1}'`
        date=`date +"%Y%m%d%H%M"`
        expire=`date -d "-${retention} days" +"%Y%m%d%H%M"`
        
        snapshots=`gcloud compute snapshots list --regexp "(${HOSTNAME}-.*)" --uri`
        
        # Delete snapshots older than $expire
        for line in "${snapshots[@]}"
        do
          snapshot=`echo ${line}|awk -F\/  '{print $10}'|awk -F\  '{print $1}'`
          snapdate=`echo $snapshot|awk -F\- '{print $3}'`
          if (( $snapdate <= $expire )); then
            gcloud compute snapshots delete $snapshot --quiet
          fi
        done
        
        # Create New Snapshot
        gcloud compute disks snapshot $HOSTNAME --snapshot-name ${HOSTNAME}-${date} --zone $zone --description "$HOSTNAME Disk snapshot ${date}"
    

提交回复
热议问题