Back up Google Drive files in Linux

How to back up Google Drive files (including Google Docs documents) to the local disk using google-drive-ocamlfuse and rdiff-backup.

Install the tools

We will need:

Try google-drive-ocamlfuse

Create some temporary directory:

mkdir /home/user/google_drive_mount_test

Mount the Google Drive:

google-drive-ocamlfuse /home/user/google_drive_mount_test

Check if the files are there:

ls -la /home/user/google_drive_mount_test

Clean up:

fusermount -u /home/user/google_drive_mount_test
rmdir /home/user/google_drive_mount_test

Configure google-drive-ocamlfuse

The configuration is in ~/.gdfuse/default/config. The file should now exist, containing some default values.

I recommend setting formats for Google Docs files (otherwise, we will see only a .desktop file containing a link pointing to the file in Google Docs - it would be worthless to back up such .desktop file only):

download_docs=true
document_format=odt
drawing_format=svg
form_format=zip
presentation_format=odp
spreadsheet_format=ods
apps_script_format=json

Such settings will tell google-drive-ocamlfuse to automatically translate e.g. Google Docs documents to .odt files (in the mounted Google Drive, they will really exist as valid .odt files) which we can then back up.

This example list may not be complete - all the possible files and formats are listed in the google-drive-ocamlfuse documentation.

One more thing which can be interesting to configure:

metadata_cache_time=60

It happened that I e.g. edited a document in Google Docs and ran the backup script immediately afterwards. However, the new version of the document was not downloaded (backed up). This is because google-drive-ocamlfuse uses a caching mechanism and does not contact the server every time to check whether there are some changes. The solution is to set this time to a lower value.

Configure the exclude list file

Create a text file to be used to tell rdiff-backup which files or directories to ignore (to not copy from Google Drive). The contents can be e.g.:

**/.Trash
**/backups

What I recommend having listed there:

  • .Trash - we do not want to back up removed files
  • some other files where backing up does not have much sense (e.g. I have backups directory on Google Drive which are files backed up to Google Drive from my local disk and so it does not make much sense to create a backup of them back to my local disk)

Prepare mount directory

Create directory to be used to mount Google Drive (e.g. /mnt/google_drive_for_backup). Setup the ownership accordingly (e.g. using chown user:user /mnt/google_drive_for_backup).

Finally, the backup script

#!/bin/bash

set -e
set -u

MOUNT_POINT=/mnt/google_drive_for_backup
TARGET_DIR=/home/user/google_drive_backup
EXCLUDE_LIST_FILE=/home/user/settings/google_drive_backup-exclude_list

google-drive-ocamlfuse "$MOUNT_POINT"

rdiff-backup --exclude-globbing-filelist "$EXCLUDE_LIST_FILE" --print-statistics --terminal-verbosity 2 "$MOUNT_POINT" "$TARGET_DIR"

fusermount -u "$MOUNT_POINT"

Advantages of using rdiff-backup

  • It only copies what was changed. The first backup run will take longer, the next will finish almost immediately (this is maybe thanks to rdiff-backup changes detection mechanism combined with google-drive-ocamlfuse caching).
  • It holds all backed up versions of the file (use --list-increments and --restore-as-of to obtain older versions).
  • Files removed from the source (Google Drive) can always be obtained in the backup (can be always found e.g. using --restore-as-of). This is very useful - when we accidentally lose some file in Google Drive, we do not want the backup mechanism to delete it in the backup, too.
  • The latest versions of the files are stored as normal files and so they can be restored from the backup without any special tools (even rdiff-backup alone is not needed). Only the older versions of a file are stored as diffs - we need rdiff-backup to restore an older version.

Additional ideas

  • Now as we have the backup script, we can use cron to run it regularly.
  • What about backing up local files to Google Drive?
Written on January 5, 2020