• Get application security done the right way! Detect, Protect, Monitor, Accelerate, and more…
  • You don’t have to bother about backup if you are hosting your website on shared hosting as most of the hosting provider takes care of the backup for you.

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

    Backup is one of them!

    Lately, I moved Geekflare 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 over 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 a backup of Google Cloud is called “snapshot.”

    Some of the advantages of a snapshot.

    • You can take a snapshot while a 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

    Manually using Google Cloud Console

    Google cloud console has the option to take a snapshot of the disk, and it is easy.

    • Log in 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 the 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.

    Automatically using Scripts

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

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

    • Log in 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 a script

    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 the last seven days. If you need to change this schedule, then edit the script file and change the value of OLDER_THAN=7

    Automatically using Google Cloud Console

    Google recently introduced these features, and I love it. Now, GCP has the option for you to schedule a disk snapshot from the console itself.

    • Go to Compute Engine >> Snapshots
    • Click on the Snapshot schedules tab and the following popup will appear. Click Create snapshot schedules

    • Enter the necessary information and click Create

    Let’s explore some of the available options.

    • Region – select where you want to store your VM snapshot (backup)
    • Schedule frequency – choose how often you want to take a backup from daily, weekly, hourly
    • Start time – at what time backup should be made. Choose low peak hours.
    • Autodelete snapshots after – how many last snapshots you want to keep at any time.
    • Deletion rule – what should happen if you delete the source disk (VM)

    Once created, you should see them listed.

    This indicates the snapshot schedule is ready to be attached to the disk.

    • Go to Compute Engine >> Disks
    • Select the disk where you want to enable the snapshot
    • Click edit and select the newly created schedule as shown below.

    • Click save

    That’s all!

    Google Cloud will take the snapshot based on the schedule.

    You see, just spending a few minutes to set up a backup can be a lifesaver. On top of the snapshot, you should also consider enabling screenshot which can help to know the VM state.

    If you are interested in learning more then check out this GCP certification course.