Back up Google Drive files in Linux
Install the tools
We will need:
- google-drive-ocamlfuse - e.g. for Arch Linux it is available in AUR; as it has many dependencies residing in AUR, I recommend using some AUR helper instead of manually building the package and every single dependency using
- rdiff-backup - e.g. for Arch Linux it is available in the Community repository
Create some temporary directory:
Mount the Google Drive:
Check if the files are there:
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):
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:
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.:
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
backupsdirectory 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
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-backupchanges detection mechanism combined with
- It holds all backed up versions of the file (use
--restore-as-ofto 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-backupalone is not needed). Only the older versions of a file are stored as diffs - we need
rdiff-backupto restore an older version.
- Now as we have the backup script, we can use
cronto run it regularly.
- What about backing up local files to Google Drive?