Additional menu

How to Backup Google Cloud VM Automatically?

How to Backup Google Cloud VM Automatically?

Geek Flare Blog post is sponsored by Netsparker Web Application Security Scanner.

You don’t have to bother about backup if you are hosting your website on shared hosting or VPS. Most of the hosting provider take care the backup for you.

However, when you migrate hosting to cloud like Google Cloud or AWS, then there are few things which you got to take care of yourself.

Backup is one of them!

Lately, I moved Geek Flare to Google Cloud Platform and was looking for a one-click backup option, but unfortunately, it doesn’t exist.

After some research, I learned backup procedure is slightly different, and I loved it. The good thing is, you have full control on your backup.

There are multiple levels (application, configuration, logs, etc.) of backup and the following I will talk about taking complete VM backup manually and automatically.

The term used to take backup of Google Cloud is called “snapshot.”

Some of the advantages of a snapshot.

  • You can take a snapshot while disk is attached to the instance – no downtime during backup
  • It’s differential instead of creating full disk backup every time – it reduces the backup storage cost and fast operation

How to take manual Google Cloud VM backup?

  • Login to Google Cloud and go to Disks under Compute Engine
  • Click on the disk name which is attached to the instance, and you want to take a snapshot
  • Click on “CREATE SNAPSHOT.”

Give the snapshot name and click Create

  • It will take some time (depends on the disk size) and once done, you see them in snapshots section.

This indicates VM disk backup is done. Whenever needed, you can use this snapshot to restore/create a new VM with the same state at the time of VM backup.

How to Automate VM backup?

If your application is having frequent changes and would like to automate taking a snapshot on a regular basis then following will help you.

I found this solution from https://github.com/jacksegal/google-compute-snapshot

  • Login to the instance which you want to take auto backup
  • Create a folder where you want to store the script file
  • Download the script file
wget https://github.com/jacksegal/google-compute-snapshot/blob/master/gcloud-snapshot.sh
  • Change the file permission to be executable
chmod 755 gcloud-snapshot.sh
  • Test the script to ensure it works by manually executing script
./gcloud-snapshot.sh

Once confirmed, it works manually then configure crontab to run periodically

  • Following example to run daily at 05:00
0 5 * * * /opt/google-compute-snapshot/gcloud-snapshot.sh >> /var/log/cron/snapshot.log 2>&1

By default, a script will keep the snapshot for last 7 days. If you need to change this schedule then edit the script file and change the value of OLDER_THAN=7

Now, you don’t have to worry about taking backup manually.

Interested in learning about GCP? Check out this tutorial.

Reader Interactions

Chandan Kumar
About Chandan
Chandan Kumar is the founder of Geek Flare. Learn more here and connect with him on Twitter.

Leave a Reply

Your email address will not be published. Required fields are marked *

11 Shares
Share
Tweet
Stumble
Share