Backup your Docker files to Google Drive
By: James Tenniswood
Tags: Ubuntu, Docker, Backup, Cron, Automate

So after setting up my docker containers, and mapping the useful configuration files to a central location, I thought it would be handy to automatically back those up.

Sure you can do this manually, but as we all know, manual processes are a sure fire way to make sure it doesn’t get done. So lets take the humans out of this and make this automated.

Whilst I’m sure you could move this process to a docker container, for now, I’m kept it simple and setup the client on the host machine.

Install the client

To get started, download the Gdrive client using this command (this is for the Linux x64 version, others can be found here)…

Then mark the download as executable…

chmod +x gdrive

Next move it somewhere useful…

sudo mv gdrive /bin/

Finally switch to running as root and configure the client…

sudo su

gdrive about

Setup your backup folders on Google Drive

The next part is about setting up your destination folders on google drive, I used the web client and created some new folders to store these backups in.

The reason I created ended up creating folders for each host under weekly but not monthly was due to the way I wanted the backups to be scheduled.

For the monthly backups, I wanted it to create a compressed file with the month & year appended to the file, so you can go back further than the last backup, but for weekly, I just wanted it to overwrite the existing backup each time it ran.

/Backup

/Backup/Monthly

/Backup/Weekly

/Backup/Weekly/Hostname

The gdrive client will need the folder reference for each destination, so you’ll need to look them up and take note of the ID.

gdrive list --order folder -m 200

Setup the backup scripts

Next setup is to setup a simple script to run the backups when you like. Start by creating a home for your scripts, I put mine in the same folder as my container files, therefore it will also be backed up in this process.

sudo mkdir /data/backup

cd /data/backup

Next step is to create the script for the weekly backups, start by creating a black text file in your new backup directory.

sudo nano weekly.sh

The add this content to that file. What this does is pretty simple, it first copies and compresses all the contents of our /data folder into a .tar file stored within the /backup/weekly folder. Then the gdrive client moves that file up to the folder you found earlier from the gdrive list command.

Its moving and then deleting the original because we don’t want to end up backup up the existing backups.

#!/bin/bash

#START

tar -cvjf /backup/weekly/HOSTNAME.tar.bz2 /data/

gdrive sync upload /backup/weekly/ INSERT FOLDER ID HERE

#END

Then do something similar for the monthly script.

sudo nano monthly.sh

But this time we are going to create a file with the year and month in the file name and then upload that so it doesn’t replace the previous monthly backups.

#!/bin/bash

#START

tar -cvjf /backup/monthly/HOSTNAME\ $(date +%Y-%m).tar.bz2 /data/

gdrive upload --parent 0B6yil4SCOYSaMUttbWczcURaRGc /backup/monthly/HOSTNAME\ $(date +%Y-%m).tar.bz2

#END

Set the automation

We are going to enter the world of the cronjob, its a pretty simple procedure to do and opens up a whole world of scheduling that you could use for other tasks in the future. Start by setting execute permissions on those new scripts.

sudo chmod +x /data/backup/weekly.sh

sudo chmod +x /data/backup/monthly.sh

Next switch to run as root, and then open the cronjob setup environment.

sudo su

crontab -e

At the bottom of this text document, add these lines. The first few numbers and stars looks like nonsense, but its actually the timings, running from minutes to years in order (read a much better description here).

I have mine set to run the weekly backup at 4am on the first day of the week, and the monthly backup to run at 6am on the first day of each month.

00 4 * * 1 /bin/bash /data/backup/weekly.sh

00 6 1 * * /bin/bash /data/backup/monthly.sh

If you want to check that its running correctly, you can add these lines that will make the scripts run every minute, you obviously don’t want to keep those in there too long.