Virtualmin: Backups to Google Cloud Storage
Table of Contents
Introduction #
For a while I have been running a few websites using Virtualmin under CentOS. It’s a great free Web Hosting Control Panel application that helps you to manage your server without having to do it manually. It contains Apache, MySQL, proFTPD and much much more.
As I am a backup freak I have been backing up my server to Amazon S3 for a while. Amazon backups cost around $0.03 per GB, not that bad, but a few months ago I switched to Google Cloud Storage which has a technology called Nearline costing only $0.01 per GB (yes, 1/3 of the price!). The only disadvantage compared to S3 is that the files retrieval takes a few seconds instead of milliseconds, as I am not serving this file on websites, and I really don’t mind waiting a couple of seconds to have the download link ready.
Virtualmin has a built-in script to automatically backup all servers to Amazon, but if you want your backups sent to Google, you need to buy the Pro version of Virtualmin. To bypass this restriction I’ve created a script to have it done.
Installing gsutil #
To start, we need to have Google’s terminal application gsutil installed on the server. To do so, follow the steps from Google’s gsutil installation guide.
To test if gsutil is installed and configured correctly, you can type
gsutil ls
, and it should list all your buckets (where your backup files will be stored).[user@server]# gsutil ls gs://bucket-name
Creating a Temporary Backup Folder #
Now we create the folder where our temporary backup files will be stored. Remember that for this to work you need to have enough free space to store all the backup files before sending them to our bucket. In my case, the temporary files will be in /home/backups/files, so type in the console mkdir /home/backups/files
.
Creating the Upload Script #
The next step is to create a bash script to automatically upload all files to our bucket. I want to upload each backup to its own dated folder, and when done, it should delete the files from the server. Copy the following script and save it to /home/backups/upload.sh
.
#!/bin/bash
#today variable as YYYY-MM-DD
NOW=$(date +"%Y-%m-%d")
#Upload all files to our dated folder in the selected bucket
gsutil cp /home/backups/files/* gs://bucket-name/$NOW
#Delete all the files after uploading them
rm /home/backups/files/*
Configuring Scheduled Backups in Virtualmin #
Everything is ready on the console part, it’s time to configure our scheduled backup in Virtualmin. Go to Backups and Restore » Scheduled Backups » Add a new backup schedule and fill it as shown in the picture below:
All set, you should have your backups ready to go!
I suggest you run it at least once to confirm everything is running smoothly.
Cheers!