Running Google Drive on Linux

Grive – a Linux client for Google Drive

Having recently I bought myself a brand new new android, within hours of unboxing it I found myself downloading dropbox and trying to link it up with my free, 2GB dropbox account. I was a bit put off when dropbox told me I already had three devices linked to that account and that I would need to delink one or more of them before I could activate dropbox on that brand new android. Up until that point I had been vaguely aware of a number of providers out there offering much more than the measly 2GB that I had with my free dropbox account and who also allow many more than just 3 devices to hook up. More out of anger than a real need to have more than 3 simultaneously syncing devices, I decided to explore other providers as a sad beginning of the end of my years of association with dropbox.

My biggest problem with cloud storage providers is that not many of them provide reliable client software for Linux, which happens to be the OS that runs on most of my laptops and PCs. Pcloud appeared to be a good option with a great UI-driven client working like a charm on my linux mint 19 systems but I eventually ran into an issue with its app on android which would not allow me to save edited files for want of a storage permission that I couldn’t ultimately make any sense of. Moreover, pcloud offers just 5GB of free space which is only slightly better than that offered to a dropbox free member.  

Eventually I decided to settle down on google drive which has a massive 15GB to offer for free, with no limit on the number of syncing devices. Gdrive is fairly standard, has wide acceptance and has rock-solid clients for windows, iOS and android. But not Linux. Absence of a reliable, maintenance-free gdrive client running seamlessly on a linux box was the only thing that stood between me and my little space in the cloud that I could access from anywhere and from all the gadgets that surround me. And that precisely made me embark on an interesting journey to work out a solution that allows a linux device to stay synced with the google drive server. The journey, I must admit, was interesting enough to coax me into spending some time to write this piece out. I sincerely hope it will prove to be a goodie for avid linux enthusiasts like me. 

Googling the prospect of having a gdrive client for linux produces a number of promising results. Several ambitious programmers have come up with a range of clients of which the one that stands out is insync. I gave insync a good try and can confirm it appears to be quite fit for purpose, reliably doing what it is supposed to. Unfortunately, insync comes with a hefty $29.99 licence fee per google account, thereby making you pay not for owning 15GB of space on the cloud but simply for connecting to it, which in itself is a bit ridiculous. There are other free clients out there like odrive, gosync, rclone etc but I quickly discovered show-stopping flaws in most of them. For example odrive installs well, fires off the synchronization process without frills and seems to start working but in the middle of the syncing process it decides it has had enough and simply gives up. There are some others that sync well for a starter but then develop moods of their own to remain oblivious to changes to files on the cloud or to local files, leading to a local file system that goes seriously out of sync with the copy on the cloud, therefore defeating the basic purpose. 

The one I finally found to work quite efficiently is called grive2 which has evolved from an older version simply called grive and which does seem to solve most common requirements an average user has around a cloud storage client. Grive2 is a command-line tool with no GUI or system tray icons to play around with. As a CLI tool, it makes use of google’s RESTful APIs to transfer files back and forth between a local location and your google storage on the cloud. Another point worth noting about grive2 is that it does not keep a constant watch over either the local or the cloud storage for changes. It needs to be wired into some kind of a service or script that does the job of invoking grive at regular intervals. Once invoked, grive does a very efficient job in terms of completely synchronizing the remote and local folders. With my programmer’s hat on, I found it very easy to overcome these shortcomings of grive and in the end I found the core grive tool to be surprisingly powerful, efficient and reliable. The initial effort required to install, configure and deamonize it is well worth the reliable service this tool offers. The following sections offer a detailed explanation around how grive can be installed, configured and set up as a daemon to run in the background and keep your local folder in perpetual sync with your google cloud storage. Commands shown below were run on a Linux Mint 19 (Tara) 64 bit system. 

Installing Grive

To install grive2, run the following commands to install grive2 on a ubuntu-based system 

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

If you are wary of adding a ppa, you can also download the binary installer file for your linux distribution from http://ppa.launchpad.net/nilarimogard/webupd8/ubuntu/pool/main/g/grive2/ and install it from the binary (refer to package installation instructions for your linux flavor) 

Now create a folder for your google drive contents e.g 

mkdir /opt/gdrive

This is your local google drive folder. Give it all the necessary permissions

chmod -R 775 /opt/gdrive
chown -R <user>:<group> /opt/gdrive 

Configuring Grive

To configure grive2, run the following commands 

/usr/bin/grive -p /opt/gdrive -a

The -a flag makes grive request google for an authorization token. This command will make grive print a URL on the command line. Copy-paste the URL into a browser and sign in with your google credentials. Upon successful authentication, google should give you an auth token which needs to be copy-pasted back into the command line window.  

Press enter and allow the command to complete. Successful completion should result the creation of a hidden file called .grive in your local google drive folder (/opt/gdrive in this case). This file contains your auth token for all all future runs. 

Running Grive

Once configuration is out of the way, you can manually synchronize your cloud and local folders by running the following command

/usr/bin/grive -p /opt/gdrive

Grive As A Linux Service

 Manually running grive to achieve syncing is not a very convenient option at all. What you ideally want is a background service (daemon) that keeps doing this for you at regular intervals (say every 30 secs) so that you don’t have to worry about synchronization every time you make changes to files in the cloud or on you local storage. To run grive as a service, thereby making it automatically sync at regular intervals, you need to write a script that runs grive and wrap the script into a linux service that automatically starts up when the system boots up and which also allows you to manually interfere if you so desire. Here are the steps to creating such a service for grive

In a CLI window, run the following commands

sudo mkdir -p /opt/grive
sudo touch /opt/grive/runGrive
sudo chmod +x /opt/grive/runGrive

Copy-paste the following content into /opt/grive/runGrive (using any text editor of your choice)

#!/bin/bash
# Grive home folder (for the purpose of this script)
GRIVE_HOME=/opt/grive
## Path to the grive executable
GRIVE=/usr/bin/grive
## The google drive root folder
GDRIVE_ROOT_FOLDER=/opt/gdrive
# Log file for this script
LOG_FILE=$GRIVE_HOME/grive.log
# Temp log file
TEMP_LOG_FILE=$GRIVE_HOME/temp.grive.log
# Seconds to sleep between successive runs
SECONDS_BETWEEN_RUNS=30
# Maximum lines in log file
MAX_LOG_LINES=5000
# set an infinite loop
while :
 do
  TIMESTAMP=`date`
  echo “*********************************** $TIMESTAMP *********************************” >> $LOG_FILE
  $GRIVE -p $GDRIVE_ROOT_FOLDER 2>&1 | tee -a $LOG_FILE
  tail -$MAX_LOG_LINES $LOG_FILE > $TEMP_LOG_FILE
  mv $TEMP_LOG_FILE $LOG_FILE
  echo “Sleeping $SECONDS_BETWEEN_RUNS seconds” >> $LOG_FILE
  sleep $SECONDS_BETWEEN_RUNS
 done

This script ensures grive is invoked at regular intervals with no more than 30 seconds between 2 successive grive runs. It also creates a log file (/opt/grive/grive.log) giving you an insight into what grive has been doing in the background and makes sure the log file remains limited to 5000 lines so as not to grow and fill up your file system over a few days. Of course you can always change the delay between successive runs and the line limit on your log file by tweaking values of the variables SECONDS_BETWEEN_RUNS and MAX_LOG_LINES respectively.

To wrap this script into a linux service, create a file called grive.service at the location /etc/systemd/system/

This file should have the following content

Unit]
Description=Grive service
After=network.target

[Service]
Type=simple
ExecStart=/opt/grive/runGrive

[Install]
WantedBy=multi-user.target

This service needs to be “enabled” which can be done by running the following command

sudo systemctl enable grive

Finally, the service should be started by running the following command

sudo systemctl start grive

And that’s pretty much it. The script you wrote above should now be running in the background. Linux will also ensure the service is started every time the system is rebooted. You can check the status of this service at any time by running the following command 

sudo systemctl status grive

And finally, should you ever need to stop the service for any reason, you can do so by running the following command:

sudo systemctl stop grive

Grive Runtime Logs

If you need to take a peek at what grive has been doing in the background, just view the grive logs available at Grive logs are available at /opt/grive/grive.log 

About Dipak Jha

Dipak Jha is is a hands-on Solutions and Integration Architect. He is based at London, UK and works as a SME on Cloud Technologies, Enterprise Architecture, Middleware, Systems Integration, Transformations, Migrations, and general Internet technologies.

Leave a Comment

Your email address will not be published. Required fields are marked *