【问题标题】:How Can I create Snapshot of Disk of Local Machine/ VM如何创建本地机器/虚拟机磁盘的快照
【发布时间】:2019-01-09 11:28:16
【问题描述】:

如何在 python 中创建本地机器磁盘或任何 VM 磁盘的快照。

我读过谷歌云快照,这里是python代码

"""
BEFORE RUNNING:
---------------
1. If not already done, enable the Compute Engine API
   and check the quota for your project at
   https://console.developers.google.com/apis/api/compute
2. This sample uses Application Default Credentials for authentication.
   If not already done, install the gcloud CLI from
   https://cloud.google.com/sdk and run
   `gcloud beta auth application-default login`.
   For more information, see
   https://developers.google.com/identity/protocols/application-default-credentials
3. Install the Python client library for Google APIs by running
   `pip install --upgrade google-api-python-client`
"""
from pprint import pprint

from googleapiclient import discovery
from oauth2client.client import GoogleCredentials

credentials = GoogleCredentials.get_application_default()

service = discovery.build('compute', 'v1', credentials=credentials)

# Project ID for this request.
project = 'my-project'  # TODO: Update placeholder value.

# The name of the zone for this request.
zone = 'my-zone'  # TODO: Update placeholder value.

# Name of the persistent disk to snapshot.
disk = 'my-disk'  # TODO: Update placeholder value.

snapshot_body = {
    # TODO: Add desired entries to the request body.
}

request = service.disks().createSnapshot(project=project, zone=zone, disk=disk, body=snapshot_body)
response = request.execute()

# TODO: Change code below to process the `response` dict:
pprint(response)

如何使用此代码创建本地磁盘的快照并将其存储在本地计算机上供以后使用?

【问题讨论】:

    标签: python google-cloud-platform diskimage


    【解决方案1】:

    您能否进一步说明您要满足的业务需求是什么?

    我问是因为您在结束时提到您想使用此代码创建本地磁盘的快照并将其存储在本地计算机上并稍后使用,但在您的帖子开头您提到了一个 VM。

    这也将有助于了解您正在运行什么环境,因为它也可以改变方法。

    您发布的代码 sn-p 将对您在代码中定义的区域和项目中的磁盘进行快照。您可以将此代码部署在 cron 作业或计划任务上。

    有一个 github 涵盖了通过 cron 作业自动执行此过程,如果您想查看它,我将其链接在下面。

    有了更多关于您的部署、操作系统版本和您希望满足的业务需求的详细信息,我们可以提供的帮助更具体一些。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-02-23
      • 2017-07-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-30
      相关资源
      最近更新 更多