Automatic Backup of Compute Engine Persistent Disks in Google Cloud Platform

There are two ways to create a backup snapshot of compute engine persistent disks. You can either take a snapshot from the Google cloud console or run gcloud snapshot command from the command line.  The following command creates a snapshot (backup) of the disk named "mydisk1" created in "asia-east1-a" availability zone,

gcloud compute disks snapshot mydisk1 --zone=asia-east1-a

If you want to enable automatic daily or weekly backups, you need to add the above command in a cron job. The cron job for automatic backups can be run in a VM in the compute engine or in a Docker container in Google Container Engine. Configuring the above command in cron requires the following steps,

  1. Install Google Cloud SDK (required for the gcloud command). If you are configuring cron in a Linux VM, follow these instructions to install Google Cloud SDK. If you are using Google Container Engine, you can create a custom container with Google Cloud SDK from this base image. Such a container can be used for all maintenance activities of your production environment.
  2. Create a file named cronfile with the following content,

    0 23 * * * /myfolder/

    The above cronfile will run the every day at 11pm. Note the use of PATH variable. Please change the first path element to point to the folder where gcloud command is located.

  3. Create the script with the following content (don't forget to change zone for your disk),

    gcloud compute disks snapshot mydisk1 --zone=asia-east1-a

    Ensure that the is executable by running the following command,

    chmod +x
  4. Finally enable the cronfile by configuring it as a cron job for a user account.

    crontab -u root cronfile
    cron reload