Cli Google Drive Client Grive2: How To Enable Automatic Synchronization

Grive 2 Google Drive auto sync

Grive2, a free and open source command line Google Drive client for Linux which supports partial sync, is quite popular among Linux users, but many probably don't know that the tool includes a built-in solution for automatic synchronization of Google Drive files with a local folder.

The Grive2 GitHub project page was recently updated to include instructions for enabling an automatic sync solution: scheduled sync (check for changes on Google Drive every 5 minutes and sync the changes locally) and sync on local file change events (automatically upload new and changed files from the local file system as they happen) with Google Drive, by using inotify with systemd units.

The systemd units as well as a sync script are directly available in the Grive2 repository and are automatically installed when installing the application. They are not new (there was a fix recently for handling directories with spaces in their names though), but this was not mentioned on the project page until recently, so many users might have missed it.

Thanks to this functionality, Grive2 can offer an experience close to that of the official Google Drive client (from the synchronization perspective only, as there is no file manager integration, etc.).

The Grive2 auto synchronization is not perfect though (but it should be enough for most users). Thanks to inotify (used via a Bash script), local file changes are instantly uploaded to Google Drive, but files changed on Google Drive are not immediately downloaded to the local file system because Grive2 can't auto detect when changes occur on Google Drive. To work around this issue, Grive2 uses a systemd timer unit to check for remote changes (set to 5 minutes by default).

The Grive2 wiki homepage mentions that a daemon mode might be implemented in the future, to avoid running Grive2 multiple times to sync files.

Grive 2 features

Grive2 is a fork of the original Grive Google Drive client for Linux. The original project was abandoned, but Vitaliy Filippov forked it under the Grive2 name, and added support for the Drive REST API, partial synchronization support, among other improvements.

Grive 2 features include:

  • Synchronize files from / to Google Drive and a folder on the local file system, from the command line
  • Partial / selective support: you can exclude specific files and folders from synchronizing by adding them to the .griveignore file. You can also only sync one subdirectory with grive -s subdir
  • --no-remote-new (download only files that are changed in Google Drive and already exist locally) and --upload-only modes (do not download anything from Google Drive, only upload local changes)
  • Create new revisions in server for updated files
  • Automatic move/rename detection
  • Sync shared files (you need to explicitly add them to your Google Drive from the web interface though)
  • Optionally limit the upload or download speed
  • Optionally display an ASCII progress bar when uploading or downloading files
  • Auth2 authentication

To see everything it has to offer, run man grive after installing it.

The application does not support Google Documents or symbolic links. It also doesn't support automatic synchronization directly (continuously waiting for changes in the file system or Google Drive and automatically upload the changes), but that's available thanks to the inotify script and systemd units that are bundled with Grive 2, which I mentioned in the beginning of the article.

Installing Grive2 and enabling automatic synchronization of Google Drive files with a local folder

For how to install Grive2, see the installation page. Make sure to download the latest Git code, and not the latest release because the latter is quite old.

The instructions include steps for creating a Debian package yourself (which you can use on Debian, Ubuntu, Linux Mint, elementary OS, and other DEB/Apt-based Linux distributions) but you can also download Grive2 DEB binaries for Ubuntu or Linux Mint from here (you can also add the PPA if you want auto updates). Despite the fork being called Grive2, the binary continues to be called grive, like the old, unmaintained project. Make sure you download the latest Grive2 build for your Ubuntu version (you can look at the date to see which is newer).

Now that Grive2 is installed, let's use it to synchronize your Google Drive files with a local directory and enable scheduled syncs and sync on file changes features.

1. Create a folder in your home directory where we'll sync Google Drive files.

The local Google Drive folder MUST be created in your user's home directory or else the automatic sync scripts will not work. I suggest you create a folder called GoogleDrive in your home directory, either by using a file manager or by typing this command:

mkdir ~/GoogleDrive

The article will assume this is what you've used from now on, so make sure you change this in the instructions below if you choose a different folder name.

2. Authorize Google Drive with Grive 2 and perform the initial sync.

The first time you run Grive2, you need to run it with the -a argument to authorize it with your Google Drive. Navigate in the newly created GoogleDrive folder from your home directory from the command line, and run grive -a in this folder:

cd ~/GoogleDrive
grive -a

A link should be displayed on the command line. You need to copy this link and paste it in a web browser. When asked, accept to give Grive2 permission to access your Google Drive files and copy the code displayed on the next page. Back in the terminal where you ran Grive2, paste the authorization code and press the Enter key.

Grive should now begin synchronizing your Google Drive files with the folder from your local file system.

3. Enable Grive 2 auto sync (scheduled syncs - check for changes on Google Drive every 5 minutes and sync to local folder, and syncs on local file change events - automatically upload new and changed files from the local file system as they happen) of Google Drive with a local folder.

To enable and start the Grive 2 systemd units, use these commands (don't run them as root):

systemctl --user enable grive-timer@GoogleDrive.timer
systemctl --user start grive-timer@GoogleDrive.timer
systemctl --user enable grive-changes@GoogleDrive.service
systemctl --user start grive-changes@GoogleDrive.service

GoogleDrive from each of these 4 commands represents the name of the Google Drive folder created in your home directory in step 1. If you've used a folder name other than GoogleDrive, replace it in these commands with the name you gave the folder. The folder NEEDS to be in your home directory or else this won't work!

In case you want to sync multiple Google Drive accounts, you can repeat the steps above using a different folder name (use a different folder for each account). Repeat the systemctl commands above for each folder you want to synchronize automatically with Google Drive.

You may also want to try Grive Indicator, a new Grive Gtk Indicator that makes use of Grive2 to synchronize with Google Drive. It doesn't seem to support the Grive2 built-in auto sync feature though, and instead it seems to use a simple timer to sync files with Google drive at a given interval.

You may also be interested in:


Powered by Blogger.