Mount Google Drive using GDrive on Linux Server with Own OAuth Credentials

Published on Jan 22, 2020

In this article, I’m going to share how to mount Google Drive using GDrive with your own OAuth credentials. So, let’s start:

Table of Contents

  1. Install Go on Your Machine
  2. Create Own Google Credentials
  3. Download GDrive & Set Credentials
  4. Build the Project
  5. Upload & Test GDrive on Server
  6. File Upload Test
  7. Make Schedule Backup

Step 1 : Install Go on Your Machine

At first, we have to install Go Language on our machine (Server, macOS, Windows etc.). I’ve written an article about Go installation for RHEL/CentOS server.

Step 2 : Create Own Google Credentials

To create your own credentials, follow these steps:

  • Go to Google API Console
  • Create a new project
  • Enable Google Drive API
  • Configure Consent Screen
  • Create credentials by choosing OAuth client ID

This link may help you to create your own Google credentials.

Step 3 : Download GDrive & Set Credentials

We need to import gdrive package using this command:

go get github.com/prasmussen/gdrive

The gdrive binary should now be available at $GOPATH/src/github.com/prasmussen/gdrive

From the binary folder, open handlers_drive.go file from the root of the project directory and set your own credentials at 17 & 18 lines.

const ClientId = "367116221053-7n0v**.apps.googleusercontent.com"
const ClientSecret = "1qsNodXN*****jUjmvhoO"

Step 4 : Build the Project

We’re about to finish. Let’s build the project. Run this command to build the project for Linux:

go build

After building the project, you’ll find a new file called “gdrive” on the root of the project folder. We’ll upload this file on our server.

Note for Windows users: For Windows users, you need to change go GOOS environment variable. To check current GOOS value, run this command:

go env

If you see the value like GOOS=windows, you have to change to set GOOS=linux. To do this, open PowerShell and type this command:

$env:GOOS = "linux"

After that, you can check go env again and then run go build to build the project. I recommend to use PowerShell for Windows users.

Note For macOS Users: At the time of building the project if you face error, you can try by changing GOOS env. To change GOOS env from macOS run this command:

export GOOS=linux

Now you can try to build the project using go build command.

Credit: Thanks Yuom Theara for sharing the macOS issue and solution. ?

Step 5 : Upload & Test GDrive on Server

We’ve already built GDrive with our own credentials. It’s time to test. I’m testing on CentOS 8. Let’s upload the generated gdrive file on our server.

Move GDrive to your server’s bin folder. The bin path of CentOS is: /usr/sbin.

sudo mv gdrive /usr/sbin/gdrive

We need to set file permission. Run this command to set permission:

# CentOS
sudo chmod 700 /usr/sbin/gdrive
# OtherOS
sudo chmod 700 /server-bin-path/gdrive

Run gdrive list command to start the authentication process. You will see a link like this:

https://accounts.google.com/o/oauth2/auth?client_id=123456789123-7n0vf****

Copy and paste the link in the browser. Accept the permission and you will get a verification code. It may show unverified, but just click on advanced and click goto <your app name>(unsafe).

Copy the verification code and paste:

Enter verification code: 4/9gKYAFAJ326XIP6J42t35****

We have installed & setup GDrive and connected it to our Google Drive. Now we are able to use GDrive commands. ?

Step 6 : File Upload Test

Let’s try to upload a file from server to Google Drive.

 # upload to google drive root folder
gdrive upload test.zip

# upload to a specefic folder
gdrive upload --parent 1dYSp4NcaRTykSebFt-UoAadszRhMQciX test.zip

File successfully saved to drive:

Step 7 : Make Schedule Backup

To make schedule backup, just add a cron job. Open cronjob file by typing this:

sudo crontab -e

Now add this line to take backup daily at 12 AM:

0 0 * * * /var/www/domain-path/public_html/backup.sh >/dev/null 2>&1
Done…! I hope this article will help you. ?

Comments (0)