0

How can I create snapshot of disks of local machine or any VM disk in python.

I have read about google cloud snapshots, Here is python code

"""
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)

How Can I use this code to create a snapshot of my local disk and store it on my local machine and use later?

hard coder
  • 5,449
  • 6
  • 36
  • 61

1 Answers1

0

Can you clarify a little more on what the business need is that you are trying to fulfill?

I ask because you have mentioned in closing that you would like to use this code to create a snapshot of your local disk and store it on your local machine and use later, but in the beginning of your post you mention a VM.

It will also help to know what environment you are running as it can change the approach as well.

The code snippet you posted will take a snapshot of a disk in the zones and projects you define in the code. You can deploy this code on a cron job or scheduled task.

There is a github that covers automating this process through a cron job and I linked it below if you wanted to take a look at it.

With more details on your deployment, OS versions and the business need you are looking to fulfill, we can be a little more specific with the help we can provide.

James B
  • 139
  • 5