Sync Google Drive from terminal using gdrive and crontab

First of all

  • Install crontab [NethServer Wiki] crontab module!
  • Have a Google Account (Any will do it)
  • Have terminal (root) access to your NethServer, i tested it on NethServer 7.9.2009, it may work fine in others too or other administrator credentials!
  • Have nano installed yum install nano or your fav terminal text editor.

References:

GitHub - prasmussen/gdrive: Google Drive CLI Client
https://shouts.dev/mount-google-drive-using-gdrive-on-linux-server-with-own-oauth-credentials#step1
https://shouts.dev/install-latest-go-on-centos8-rhel8

Install Go

Download it:

wget https://dl.google.com/go/go1.13.5.linux-amd64.tar.gz

Extract it:

sudo tar -C /usr/local -xf go1.13.5.linux-amd64.tar.gz

Set Go Path

sudo nano ~/.bash_profile

Add:

export GOPATH=$HOME/golang
export PATH=$PATH:/usr/local/go/bin:$GOPATH/bin

Save:

source ~/.bash_profile

Verify:

go version

Create Google Credentials
Go to:

Create a new Project:


Give any name you want.

Click here and enable GDrive API:
image
image

Configure Conset Screen:
Click:
image
Click:


Check “External” and click “Create”

Fill this:

You should leave the rest blank. Click in “Save and Continue”. DO NOT ADD LOGO it will give you some trouble later on…

Add one Developer contact information.

No need to add Scopes or Test Users, just click “save and continue” and “back to dasboard”.

After that click on “Credentials” and “+Create Credentials”.:

image
Select “Desktop App” or “Others” and click “Create”:

Save this data:

Install GDrive:

go get github.com/prasmussen/gdrive

After that type:

cd /$GOPATH/src/github.com/prasmussen/gdrive

and then:

nano handlers_drive.go

Go to line 17& 18 and add your google Credentials (use your credentials):

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

Control+O to save changes
image
Hit Enter and then Control+X

Lets Build our project

Type:

go build

Move Gdrive to sbin:

sudo mv gdrive /usr/sbin/gdrive

Set Permissions:

sudo chmod 700 /usr/sbin/gdrive

Publish your APP click “OAuth conset Screen” then “PUBLISH APP”:

Run:

gdrive list

One URL should appear, follow it and authorize it. With the correct Google Account
Something like this should show in Browser, click in “advanced”:


The click in the “unsafe” option.

Click in “Allow”
image

And Again, click “allow”

Copy:
image
And paste it to your terminal.

Should look something like this:

Now to start “Sync” Folders you need to create folders using Gdrive:
Do as many folders you need to sync and keep organized. I did 3 folders!

gdrive mkdir foldername

It will look something like this:
image

Keep those gdrive Directory names, only with those you will be able to sync a folder within your linux.
If for any reason you lost them just type:

gdrive list

Something like this should:

In my case i want to sync 3 “file shares” therefore i created a file named backup.sh inside /usr/local/bin/

that means:

nano /usr/local/bin/backup.sh

Inside that file.

#!/bin/sh
cd /
gdrive --config root/.gdrive sync upload /var/lib/nethserver/ibay/operacional/ 1xLTs8N7L8BBWAUKdoQCO0zt2mbYd2sUN
gdrive --config root/.gdrive sync upload /var/lib/nethserver/ibay/patrimonio/ 1omMEYSMmqxqm1waXVqEREZT-7zxfjEl1
gdrive --config root/.gdrive sync upload /var/lib/nethserver/ibay/vendas/ 1k1t3ZZY_qqJLryPdIesZoNObnUqwoHCU

that --config root/.gdrive indicates where the token auth is.
The wierd letters and numbers after the ibay folder is where gdrive should sync, in my case i just need to upload, but here you can see your options.

give your backup.sh execute permissions.

chmod +x /usr/local/bin/backup.sh

Test it out.

:heart:
Now just add it to your crotab schedule like this:

4 Likes

Btw, this no longer works, use rclone instead! :slight_smile:

Hi @ssabbath , would you be able to share your experiences with clone please?

TIA!

Just give me sometime and i will, but its easy.