Setup Automatic Backups for Websites on Google Cloud

June 18, 2018Leron AminComments Open

Configuring automatic backups of your website means never having to worry about losing your website’s data in the case of an emergency.

In this tutorial you will learn how to configure automatic daily backups of your website on Google Cloud by configuring an automatic-backup script.

The automatic backup script creates a new backup (snapshot) of your website based on predefined specifications which you’ll define in this tutorial.

configuring disk snapshot backups google cloud
The script will automatically delete the oldest backup file when a new one is created, meaning that the total number of backups available at any given time will never exceed 7.

Getting started

Before stating this tutorial, it is essential that you reserve a static IP address for your VM instance. If you don’t reserve a static IP address before starting this tutorial, Google Cloud will automatically assign a new IP Address to your VM instance when it restarts.

If you haven’t yet launched a website on Google Cloud, check out the following beginner tutorials for instructions on how to install WordPress on Google Cloud:

  1. How to Install WordPress on Google Cloud for Beginners
  2. How to Configure a Domain Name for WordPress on Google Cloud

There are 5 steps in this tutorial:

1. Enable Compute Engine access

automatic backups google cloud
From your Google Cloud dashboard, click the hamburger menu in the upper-left corner of the screen then navigate to Compute Engine > VM Instances.
automatic backups google cloud compute engine
From your VM instances page, click on the VM instance that is running your website.
automatic backups google cloud compute engine
At the top of the VM instance settings page, click the Stop icon, and wait for your instance to shutdown. After it has successfully shutdown, click on the Edit icon.
automatic backups google cloud compute engine
Scroll to the very bottom of the page to the section titled API Access Scopes. From here, change the Compute Engine setting to Read Write, then click the Save button.
automatic backups google cloud compute engine
Lastly, scroll back to the top of the page and click the Start icon to restart your VM instance, then click the back arrow icon to return to your VM instances dashboard.

2. Clone automatic backup script

automatic backups google cloud compute engine
From your VM instance dashboard, click on the SSH button to connect to the VM instance that is running your website.
automatic backups google cloud compute engine
Now that you’re connected to your VM instance, copy and paste the entire script below into the SSH terminal in order to clone the automatic backup script.

wget https://raw.githubusercontent.com/jacksegal/google-compute-snapshot/master/gcloud-snapshot.sh

chmod +x gcloud-snapshot.sh
sudo mkdir -p /opt/google-compute-snapshot
sudo mv gcloud-snapshot.sh /opt/google-compute-snapshot/

3. Create cronjob to automate script

automatic backups google cloud compute engine
Now copy and paste the command below to create a new crontab file.
sudo crontab -e
automatic backups google cloud compute engine
In your crontab file, copy and paste the script below at the very bottom of your crontab file, then press CTRL + X, then Y, to save and exit the crontab file.
10 18 * * * /opt/google-compute-snapshot/gcloud-snapshot.sh >> /var/log/cron/snapshot.log 2>&1

4. Enable logging for cronjob

automatic backups google cloud compute engine
Copy and paste the script below to create a log file which will create a new record every time a new backup file is created.

sudo mkdir /var/log/cron 

sudo touch /var/log/cron/snapshot.log
sudo chgrp adm /var/log/cron/snapshot.log
sudo chmod 664 /var/log/cron/snapshot.log
automatic backups google cloud compute engine
Copy and paste the script below to create a logrotate file, which will ensure that our log file never gets too big.
sudo nano /etc/logrotate.d/cron
automatic backups google cloud compute engine
Finally, paste the script below into the logrotate file, then press CTRL + X then Y to save the file.

/var/log/cron/*.log {

daily
missingok
rotate 14
compress
notifempty
create 664 root adm
sharedscripts }

5. Optional configurations

Defining the total number of backups to save

In this tutorial, you configured the system to save a total of 7 backups. You can change the total number of backups to whatever you want, simply by incorporating the -d flag.

configuring backups websites on google cloud
In the example above, the -d flag is is added to the script to instruct the system to save a total of 30 backups instead of the default 7. You can replace 30 with the number of backups you want to be saved.
10 18 * * * /opt/google-compute-snapshot/gcloud-snapshot.sh -d 30 >> /var/log/cron/snapshot.log 2>&1

Testing the automatic backup script

You can test whether the automatic backup script is working by executing the backup script in your SSH terminal.

automatic backups google cloud
Execute the code below in your SSH terminal to ensure that your cronjob script is configured properly.
/opt/google-compute-snapshot/gcloud-snapshot.sh >> /var/log/cron/snapshot.log 2>&1
automatic backups wordpress google cloud
Wait a minute or so, then execute the command below to check the status of the backup in your log file.
sudo cat /var/log/cron/snapshot.log
automatic backups wordpress google cloud
If the backup file was generated successfully, you will see an output similar to the image above.

Viewing your backup files

automatic backups google cloud view backup files
To view your backup files, click on the snapshot link in the sidebar menu on Google Cloud.
automatic backups google cloud view backup files
If your configuration is working properly, you will see a new backup (snapshot) file.

Closing thoughts

If you have any questions or comments, please leave them in the comment field below.

Special thanks to Jack Segal for creating the automatic snapshots script.

For troubleshooting support, be sure to check out the WordPress Cloud Hosting Support Group on Facebook.

Leave a Reply

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

Made with by

© 2020 One Page Zen Systems, LLC  
Legal  |  Privacy Policy  |  Affiliate Disclosure  |  Contact  

Join our Mailing List!

Exclusive early access to the latest tutorials

Monthly update with a list of upcoming tutorials

Special access to members-only events and webinars