How to Back Up Files to Google Drive on Linux

Gdfuse Featured Image

Google Drive is an excellent medium for people who need to store online copies of their files. Linux users can leverage its free 15 GB plan for backing up essential configuration files. Moreover, you can back up a large volume of data, including media files, if you have an active Google Workspace (formerly G-Suite) plan. This guide will show how you can automatically back up files to Google Drive in Linux using the open-source package google-drive-ocamlfuse and CRON.

What is google-drive-ocamlfuse?

google-drive-ocamlfuse is a FUSE filesystem that lets users mount their Google Drive storage on the local machine. It is written using OCaml and is freely available via GitHub. Some of its main features include full read/write access, multiple account support, Unix permissions, and Team Drive support.

How to Use It for Automatic Backups?

The first step is to mount the remote Google Drive to the local filesystem. This is where google-drive-ocamlfuse comes into play. Then we can use an automatic scheduler for periodically transferring files to the drive. To keep things simple, we’re using the Linux CRON utility to take care of the scheduling process. (You can use Zeit to schedule cron jobs, too.)

Install and Configure google-drive-ocamlfuse

First, we need to install google-drive-ocamlfuse on our Linux machine. Luckily, it is very straightforward. Fire up your terminal and enter the following command at the prompt.

sudo add-apt-repository ppa:alessandro-strada/ppa
adding gdfuse ppa

It will add the required package to your package manager’s repository list. You can go ahead and install the package by using the following commands.

# Debian/Ubuntu
sudo apt update && sudo apt install google-drive-ocamlfuse
 
# Fedora
sudo dnf copr enable sergiomb/google-drive-ocamlfuse
sudo dnf install google-drive-ocamlfuse
 
# Arch
yay -S google-drive-ocamlfuse
installing gdfuse

Wait until the installation finishes. Once it completes, you need to configure it for mounting. The first step for this is to create a mount point for Google Drive in your local filesystem. You can do this by typing the below command in your terminal.

mkdir -p ~/mount/google-drive
creating mount point to store backup files to google drive

Next, mount the drive storage with the following command:

google-drive-ocamlfuse ~/mount/google-drive
mounting google drive

google-drive-ocamlfuse will open your default browser and redirect to the account authentication page for Google Drive. Select the account you want to mount locally.

selecting drive account

Enter the password for the selected account to authenticate yourself.

authenticating google account

Next, Google will require permission to grant access to google-drive-ocamlfuse. Click on “Allow” and proceed to the next page.

allowing gdfuse access to store backup files to google drive

You will be asked to allow access to your user account one more time.

allowing account access

Once you allow permission, it will redirect you to the webpage of google-drive-ocamlfuse. Congratulations, you’ve successfully mounted your Google Drive account to the local filesystem.

completed mount

Now is a good time to verify whether the mounting process for Google Drive went smoothly or not. If this stage gives an error, you can’t use it to back up files to Google Drive. Use the below command to verify this.

ls -l ~/mount/google-drive/
checking mount status

The output should display a list of files and directories present in your Google Drive storage. If everything goes as expected, we can proceed to configuring the backup scheduler.

Back Up Files to Google Drive Using CRON

A backup is nothing but a copy operation. Since our Google Drive is already mounted, we can copy files to it as part of the local filesystem. You can verify this by running the following command:

touch test-file
cp test-file ~/mount/google-drive/

It should copy the test file to your drive storage. Use the ls command to verify whether it was successfully copied to Google Drive.

ls ~/mount/google-drive/
testing backup files to google drive using gdfuse

Now that everything is in place, configure cron for automating the backup process. If you don’t know already, cron is a scheduler that allows users to run certain commands periodically. We can specify which commands to run and when to run them by using the crontab. You can learn more about it here.

We’re using the following crontab entry to copy our “/tmp” folder’s contents to Google Drive at 00:00 daily.

0 0 * * * cp /tmp/ ~/mount/google-drive/

If you want to back up your files once every week, use the following crontab entry.

0 0 * * 0 cp /tmp/ ~/mount/google-drive/

It will copy the contents of “/tmp” to your drive storage at 00:00 of every Sunday. Replace “/tmp” with the directory containing your personal files. However, there is one last step. None of the above crontab entries mount Google Drive automatically. One quick solution to this is to mount the drive storage using another crontab entry, then follow it up with the entry for backup.

55 23 * * 0 google-drive-ocamlfuse ~/mount/google-drive
0 0 * * 0 cp /tmp/ ~/mount/google-drive/

As you can see, we are mounting the drive five minutes earlier. It gives cron enough time to make sure it doesn’t miss the backup process due to initialization issues. Use the below command to edit the crontab and add the above lines.

crontab entries for storing backup files to google drive

Wrapping Up

We have presented a simple way to back up files to Google Drive using google-drive-ocamlfuse and cron. Alternatively, if you are using Gnome, you can also easily connect Google to your desktop and mount Google Drive on your system. You can also check out some other Cron alternatives or use the systemd timers as a cron replacement.

Tip: Use these Google Drive keyboard shortcuts to supercharge your routine.

Rubaiat Hossain
Rubaiat Hossain

Rubaiat is a CS graduate who possesses hands-on experience with Unix Administration, Web Programming, DevOps, and Virtualization. He has a strong passion for enlightening people in open-source technologies.

Subscribe to our newsletter!

Our latest tutorials delivered straight to your inbox