Automating Machine Image Backups in GCP: A Comprehensive Guide

ยท

4 min read

Automating Machine Image Backups in GCP: A Comprehensive Guide

Hello Hashnode Community,

I'm excited to share my recent experience automating machine image (MI) backups in Google Cloud Platform (GCP). This journey was both enlightening and rewarding, and I'm thrilled to walk you through the steps I took to implement this robust disaster recovery solution.

Why Choose Machine Image Backups Over Snapshots?

Machine Images are way more comprehensive than snapshots. They encapsulate all the information about a VM into a single entity, including disk configurations, network settings, startup scripts, SSH keys, and instance metadata. This means that the VM can be restored to the exact state it was in at the time of backup, saving us from the overhead of provisioning a new VM and attaching snapshots as disks. Essentially, we're saving significant time and effort.

On the other hand, Snapshots typically capture only the VM data (disk state). While useful, the process of recovering a VM from a snapshot is quite time-consuming and requires additional steps like creating a new VM and attaching the snapshot as a disk.

Advantages of Machine Image Backups:

  1. Comprehensive Backup: Machine images capture the entire VM state, including disk configurations, network settings, startup scripts, SSH keys, and instance metadata. This ensures the VM can be restored to its exact state at the time of backup.

  2. Faster Recovery: Machine images allow for quick restoration of the entire VM without manual reconfiguration. You can recreate the VM from an image without needing to manually configure network settings or attach additional disks.

  3. Simplified Management: Machine images provide a single entity for the entire VM, simplifying backup management and reducing the chances of errors during restoration.

  4. Enhanced Reliability: Machine images reduce the risk of missing critical components during backup, ensuring a more reliable disaster recovery process.

Automating Machine Image Backups in GCP

To automate machine image backups in GCP, I'll guide you through creating a Cloud Function and setting up Cloud Scheduler to run it on a fixed schedule.

Step 1: Create a Cloud Function

First, let's write the Cloud Function. This function will create a machine image of your VM instance.

Cloud Function Code:

import googleapiclient.discovery
from google.oauth2 import service_account
import datetime

def create_machine_image(event, context):
    credentials = service_account.Credentials.from_service_account_file('path_to_your_service_account_key.json')
    compute = googleapiclient.discovery.build('compute', 'v1', credentials=credentials)
    project = 'your-project-id'
    zone = 'your-zone'
    instance = 'your-instance-name'
    timestamp = datetime.datetime.now().strftime('%Y%m%d-%H%M%S')
    machine_image_name = f'{instance}-backup-{timestamp}'

    request_body = {
        "name": machine_image_name,
        "sourceInstance": f"projects/{project}/zones/{zone}/instances/{instance}"
    }

    request = compute.machineImages().insert(project=project, body=request_body)
    response = request.execute()
    print(f'Machine Image {machine_image_name} created: {response}')

To deploy this function, use the following command:

gcloud functions deploy createMachineImage \
  --runtime python39 \
  --trigger-http \
  --allow-unauthenticated \
  --entry-point create_machine_image \
  --set-env-vars GOOGLE_APPLICATION_CREDENTIALS=path_to_your_service_account_key.json

Step 2: Set Up Cloud Scheduler

Next, we'll set up a Cloud Scheduler job to trigger this function on a regular basis.

  1. Navigate to Cloud Scheduler in the Google Cloud Console.

  2. Click "Create Job".

  3. Configure the job settings:

    • Name: Provide a name for the job.

    • Frequency: Set the desired frequency (e.g., daily at midnight).

    • Time Zone: Set the appropriate time zone.

  4. Configure the Job Target:

    • Target: HTTP

    • URL: Use the URL of your deployed Cloud Function.

    • HTTP Method: POST

    • Auth Header: Use an OIDC token with the service account that has permissions to invoke the Cloud Function.

Example Cloud Scheduler Job Setup:

gcloud scheduler jobs create http createMachineImageBackup \
    --schedule "0 0 * * *" \
    --uri "https://REGION-PROJECT_ID.cloudfunctions.net/createMachineImage" \
    --time-zone "TIME_ZONE" \
    --oidc-service-account-email "YOUR_SERVICE_ACCOUNT_EMAIL" \
    --oidc-token-audience "https://REGION-PROJECT_ID.cloudfunctions.net/createMachineImage"

Conclusion

By automating machine image backups in GCP, you're ensuring that your VM instances can be quickly and efficiently restored to their exact state at the time of backup. This provides a robust disaster recovery solution that simplifies management, reduces overhead, and enhances reliability.

I hope this guide was helpful! If you have any questions or need further assistance, feel free to reach out. I'm always here to help.

Happy coding! ๐Ÿš€


If you enjoyed this article, please give it a like and share it with your network. Follow me for more insights on cloud infrastructure and DevOps practices.

Feel free to leave comments or questions below. I'd love to hear your thoughts and experiences!


ย