Backup script on Google Drive for Linux

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.

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
“`

  • Muito bom…

    • I’m not sure I follow your sugoestign, Duncan. The part of the script you are referring to is just part of the original cygwin bat shell written by whoever wrote cygwin. My modification is around getting cygwin to launch my shell script (the part after the -c switch), and then getting that shell script to figure out what directory it is in and change the working directory there.

  • Thanks a lot. It’s work and works great.
    Ubuntu 13.10
    I’ve installed by run
    $> apt-get install grive

    • 4 years agoHello Ray,I used ORCA, but this doesn’t work as expected. Also, most of the time Universal extcrator cannot extract anything from the MSi it doesn’t do a very good job.But I’m glad you posted something concrete. I am going to try this one.Have any idea if the author is going to update the Less MSIe9rables? By the name, I guess it’s a french author Thanks for this tip man. I hoped Microsoft would have given a tool of its own, but Thanks again.Reply

  • Thx.
    What about the bandwidth? If my backups are about 400 GB in size, it will also work with the upload and download of the Google Drive?

  • I ԛuite like reading ann articlе that can make men and women think.
    Also,thank yoou for permitting mme to comment!

    my webpage … [Shop Evo Design](http://colinfitzgibbon.pen.io “Shop Evo Design”)

  • Great manual

    But I recommend better to change in the script a copying procedure to rsync.
    ————————————————-
    # Coping new content
    echo Copying from ${BACKUPDIR}/* to ${GDRIVEDIR}/${TARGETDIR}/
    cp -R ${BACKUPDIR}/* ${GDRIVEDIR}/${TARGETDIR}/
    ————————————————-
    to
    ————————————————-
    # Coping new content
    echo Copying from ${BACKUPDIR}/* to ${GDRIVEDIR}/${TARGETDIR}/
    rsync -auz -r –delete ${BACKUPDIR}/ ${GDRIVEDIR}/${TARGETDIR}/
    ————————————————-

  • Finally i quit my regular job, now i earn a lot of money online you should try too, just search in google – blackhand roulette
    system

  • sgitkene

    Hello, this doesn’t work as easily anymore. Google deprecated the api used here. Maybe you can get it to work with https://github.com/Grive/grive and supply a .deb package for it to easily work? The package in jessie uses the deprecated api and doesn’t work anymore.

  • Daniel Sobrinho

    nice script

  • LeoRS