In this post you will learn how to configure setup automatic backups daily of your website on Google Cloud by configuring an automatic-backup script.The automatic backup script creates a new backups (snapshot) of your website based on predefined specifications which you’ll define in this tutorial.
There are 5 steps in this tutorial:
- 1. Enable Compute Engine access
- 2. Clone automatic backups script
- 3. Create cronjob to automate script
- 4. Enable logging for cronjob
- 5. Optional Configurations
1. Enable Compute Engine access
From your Google Cloud dashboard, click the hamburger menu in the upper-left corner of the screen then navigate to Compute Engine > VM Instances.
From your VM instances page, click on the VM instance that is running your website.
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.
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.
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
From your VM instance dashboard, click on the SSH button to connect to the VM instance that is running your website.
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.
3. Create cronjob to automate script
Now copy and paste the command below to create a new crontab file.
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.
4. Enable logging for cronjob
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.
Copy and paste the script below to create a logrotate file, which will ensure that our log file never gets too big.
Finally, paste the script below into the logrotate file, then press CTRL + X then Y to save the file.
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.
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.
Testing the automatic backup script
You can test whether the automatic backup script is working by executing the backup script in your SSH terminal.
Execute the code below in your SSH terminal to ensure that your cronjob script is configured properly.
Wait a minute or so, then execute the command below to check the status of the backup in your log file.
If the setup automatic backups file was generated successfully, you will see an output similar to the image above.
Viewing your backup files
To view your backup files, click on the snapshot link in the sidebar menu on Google Cloud.
If your configuration is working properly, you will see a new backup (snapshot) file.