Backup script on Google Drive for Linux

The problem

With 5Gb of free space, Google Drive could be a good option for simple backups of small servers and desktops.

We'll use Grive for Google Drive support and a small script that you can adjust for your needs.

This is an old article (2013). Maybe the content is outdated.

Installing Grive

On Debian:

apt-get install grive

On Ubuntu:

We'll be using PPA Repositories.

sudo add-apt-repository ppa:nilarimogard/webupd8
sudo apt-get update
sudo apt-get install grive

Preparing

Create a directory and enter it:

mkdir /mnt/GoogleDrive
cd /mnt/GoogleDrive

Run the grive command with the option -a to prepare and get authorization:

# grive -a
-----------------------
Please go to this URL and get an authentication code:

https://accounts.google.com/o/oauth2/auth?scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.profile+https%3A%2F%2Fdocs.google.com%2Ffeeds%2F+https%3A%2F%2Fdocs.googleusercontent.com%2F+https%3A%2F%2Fspreadsheets.google.com%2Ffeeds%2F&redirect_uri=urn:ietf:wg:oauth:2.0:oob&response_type=code&client_id=22314510474.apps.googleusercontent.com

-----------------------
Please input the authentication code here:

Copy and paste the link into your browser, being sure to be logged on the right Google Account to access the Drive you need. Copy and paste back the authentication code, and is done - first sync will be done.

Warning: only "real" files will be synced - grive doesn't support download of Google Apps editable docs.

Script

This script reads a directory to sync with GoogleDrive.

Create the following script in /SOMEPATH/googledrive-sync.sh and give it permission to execute (chmod 755 /SOMEPATH/googledrive-sync.sh)

#!/bin/bash
# Google Drive Sync
#
# Author: Rafael Goulart <rafaelgou@gmail.com>
#
# Config BEGIN
# =====================================================================

# Directory to backup
BACKUPDIR=/var/backups

# Google Drive directory
GDRIVEDIR=/mnt/GoogleDrive

# Directory target in remote
TARGETDIR=/backups

# =====================================================================
# Config END

# Create backup dir if not exists
echo Creating ${GDRIVEDIR}/${TARGETDIR} if needed
if [ ! -d "${GDRIVEDIR}/${TARGETDIR}" ]; then mkdir ${GDRIVEDIR}/${TARGETDIR}; fi

# Moving to Gdrive Dir
echo Entering ${GDRIVEDIR}
cd ${GDRIVEDIR}

# Initial sync
echo Initial Google Drive Sync
grive

# Coping new content
echo Copying from ${BACKUPDIR}/* to ${GDRIVEDIR}/${TARGETDIR}/
cp -R ${BACKUPDIR}/* ${GDRIVEDIR}/${TARGETDIR}/

# Showing files copied
echo Files to sync
find ${GDRIVEDIR}/${TARGETDIR}/

# Final sync
echo Final Google Drive Sync
grive

You have 3 parameters:

  • BACKUPDIR: Directory to backup - put your stuff here, make a script to compact your data befoe - default /var/backups
  • GDRIVEDIR: Google Drive directory you'd created before - default /mnt/GoogleDrive
  • TARGETDIR: Directory target in remote, it will be crated to not mess up your Drive - default /backups

Put this file in your crontab:

ln -sf /SOMEPATH/googledrive-sync.sh /etc/cron.dayly/

or run it manually:

/SOMEPATH/googledrive-sync.sh

Warnings and Tips

  • Be aware of the limitations of Grive
  • You can save some disk space doing your backup directly on /mng/GoogleDrive/backups and only running grive after doing it. I prefered the 2 steps as suggested, but it's up to you

Update

If the following command does not exist (Ubuntu 12.04)

sudo add-apt-repository ppa:nilarimogard/webupd8

install with:

sudo apt-get install python-software-properties