如何创建和旋转自动快照?

Imr*_*hid 20 google-compute-engine

在计算引擎上我们可以做快照,基本上是备份.您是否可以尝试弄清楚我们如何创建脚本以每天执行自动快照并保留4个快照,所以基本上在我们有4个快照之后,删除最旧的快照.这是我在Google Cloud上唯一关注的问题就是没有定时备份服务器,否则我喜欢Compute Engine,它比亚马逊使用起来要便宜得多.

Jac*_*gal 31

更新:

自从我第一次给出这个答案以来,脚本发生了很大变化 - 请参阅Github repo获取最新代码:https://github.com/jacksegal/google-compute-snapshot

原始答案:

我遇到了同样的问题,所以我创建了一个简单的shell脚本来拍摄每日快照并删除7天内的所有快照:https://github.com/Forward-Action/google-compute-snapshot

#!/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
Run Code Online (Sandbox Code Playgroud)


Ala*_*ler 11

我的解决方案稍微简单一些.我想快照所有磁盘而不仅仅是主磁盘.

通过列出项目中的所有磁盘,它可以处理来自单个脚本的所有服务器 - 只要它在gcloud项目中运行(并且可以修改为在项目服务器外部运行).

整理旧快照不需要这么复杂的日期处理,因为它可以使用过滤器从gcloud命令行处理

https://gitlab.com/alan8/google-cloud-auto-snapshot

#!/bin/bash
# loop through all disks within this project  and create a snapshot
gcloud compute disks list | tail -n +2 | while read DISK_NAME ZONE c3 c4; do
  gcloud compute disks snapshot $DISK_NAME --snapshot-names auto-$DISK_NAME-$(date "+%s") --zone $ZONE 
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 unwieldy so this script deletes them after 60 days
#
gcloud compute snapshots list --filter="creationTimestamp<$(date -d "-60 days" "+%Y-%m-%d") AND (auto.*)" --uri | while read SNAPSHOT_URI; do
  gcloud compute snapshots delete --quiet $SNAPSHOT_URI
done
#
Run Code Online (Sandbox Code Playgroud)

另请注意,对于OSX用户,您必须使用类似的东西

$(date -j -v-60d "+%Y-%m-%d")
Run Code Online (Sandbox Code Playgroud)

对于creationTimestamp过滤器

  • 请注意,此脚本将不再起作用,因为快照名称的字符数 (61) 有限制```必须与正则表达式匹配 '(?:[az](?:[-a-z0- 9]{0,61}[a-z0-9])?)``` 所以你需要编辑生成快照名称的部分 (2认同)

Dmy*_*hyi 10

文档非常清楚如何做到:

gcloud compute disks snapshot DISK
Run Code Online (Sandbox Code Playgroud)

注意

始终基于上次拍摄的成功快照创建快照

在删除任何快照之前 - 请查看该图表: 在此输入图像描述

有关API的更多信息.

  • 这是我们从界面手动手动执行的方法.我想执行一个脚本,它作为cron为我做 (2认同)

Dan*_*ter 5

GCP 中现在提供了一项名为“快照计划”的功能。

它似乎仍处于 Beta 阶段,并且还没有太多关于此功能的文档。但这是一个简单的过程。首先,您创建一个快照计划,并在设置后将其分配给永久磁盘。

通过 GUI 创建快照计划

另请参阅命令行参考,以使用相应的 gcloud 命令创建计划:

gcloud beta compute resource-policies create-snapshot-schedule
Run Code Online (Sandbox Code Playgroud)

https://cloud.google.com/sdk/gcloud/reference/beta/compute/resource-policies/create-snapshot-schedule

要将计划分配给永久磁盘,您可以使用以下命令

gcloud beta compute disks add-resource-policies
Run Code Online (Sandbox Code Playgroud)

https://cloud.google.com/sdk/gcloud/reference/beta/compute/disks/add-resource-policies

2019 年 2 月 15 日更新: 从昨天开始,有一个关于计划快照功能的博客公告,并且 Compute Engine 文档中也有一个关于计划快照的页面。