Md Obydullah Follow I'm Md Obydullah. I build open-source projects and write about Laravel, Linux server, modern JavaScript and web development.

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

Last modified on February 14, 2020 2 min read

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

After installation of Go, download GDrive project from GitHub and extract the project in a folder. Now open handlers_drive.go file from the root of the project directory and set your own credentials at lines 17 & 18.

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

Step 4 : Build the Project

Before building the project, we need to import gdrive package using this command:

go get github.com/prasmussen/gdrive

We’re about to finish. Let’s build the project. Using command line, go to the root folder of the project. Then 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 folder of CentOS is: /usr/sbin.

sudo mv gdrive /usr/sbin/gdrive

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

sudo chmod 700 /server-bin-folder/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. ๐Ÿ™‚

You're welcome to suggest any article to write!

Md Obydullah Follow I'm Md Obydullah. I build open-source projects and write about Laravel, Linux server, modern JavaScript and web development.

10 Replies to “Mount Google Drive using GDrive on Linux Server with…”

  1. Great article, well written and timely help!
    Amazed to see how it helps

    Great job and keep it up!
    Such articles are really useful when we are stuck

  2. Great post, been looking for a good way to attached google drive to my centos box running CWP7, for the means to backup, because CWP7 has a terrible backup service…

  3. Great post, thank you very much! I’m not very experienced with this kind of procedure (compiling sources, etc), but, the way you described it, I feel confident to try.
    In most of the computers that I use, I still have acess to GoogleDrive API for gdrive, since I set gdrive on them a long time ago.
    I’m having troubles with Gdrive only in a new Windows 10 computer that I’ve started to use last January. Isn’t there a way of copy the access credentials from the other computers to the new one?

    1. Hi Manoel Guimaraes, thank you so much for trying this method. If you face any issue, let me know. I’ll try to sort.

      Yeah, It’s possible to find access credentials from the old device. You’ll find the credentials in the root path of your device. The gdrive config file on CentOS is: /root/.gdrive/config.json or /username/.gdrive/config.json. I didn’t test on Windows. But I think you may find the config file at C:\Users\USERNAME\.gdrive.

  4. Great work! thank you so much for your post! I have stacked for about a month to transfer large files to google drive. This post is LIFE-SAVER!!!!!
    One thing I would like to ask? I’m new to crontab. I don’t understand what this part means: /var/www/domain-path/public_html/backup.sh. How can I sync only one folder in ubuntu server to one folder in google drive? BTW, I’m not the root user but have the root password.

    1. Hi Yu An. Thank you so much for trying this method.

      Crontab is a time-based job scheduler. You can take schedule backup of your websites using crontab. You can search for crontab on Google. I take my website’s backup using crontab. If you need help with crontab, you can inform me. I’ll try to help.

      I didn’t test GDrive sync option. You can check the sync option and try.

      Thanks. ๐Ÿ™‚

Leave a Reply

Your email address will not be published. Required fields are marked *