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:
- 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
makepkg
- rdiff-backup - e.g. for Arch Linux it is available in the Community repository
Try google-drive-ocamlfuse
Create some temporary directory:
Mount the Google Drive:
Check if the files are there:
Clean up:
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):
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
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
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 withgoogle-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 needrdiff-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?