Google drive upload is a collection of shell scripts runnable on all POSIX compatible shells ( sh / ksh / dash / bash / zsh / etc ).

It utilizes google drive api v3 and google OAuth2.0 to generate access tokens and to authorize application for uploading files/folders to your google drive.

  • Minimal
  • Upload or Update files/folders
  • Recursive folder uploading
  • Sync your folders
  • Sync with overwrite or skip existing files support.
  • Resume Interrupted Uploads
  • Share files/folders to anyone or a specific email.
  • Config file support
  • Easy to use on multiple operating system.
  • Support for multiple accounts in a single config.
  • Latest gdrive api used i.e v3
  • Pretty logging
  • Easy to install and update
    • Self update
    • Auto update
    • Can be per-user and invoked per-shell, hence no root access required or global install with root access.
  • An additional sync script for background synchronisation jobs. Read Synchronisation section for more info.

Install

You can install the script by automatic installation script provided in the repository.

This will also install the synchronisation script provided in the repo.

Installation script also checks for the native dependencies.

Default values set by automatic installation script, which are changeable:

Repo: labbots/google-drive-upload

Command name: gupload

Sync command name: gsync

Installation path: $HOME/.google-drive-upload

Source: release { can be branch }

Source value: latest { can be branchname }

Shell file: .bashrc or .zshrc or .profile

For custom command names, repo, shell file, etc, see advanced installation method.

Now, for automatic install script, there are two ways:

Basic Method

To install google-drive-upload in your system, you can run the below command:

curl --compressed -Ls https://github.com/labbots/google-drive-upload/raw/master/install.sh | sh -s

and done.

Advanced Method

This section provides information on how to utilise the install.sh script for custom usescases.

These are the flags that are available in the install.sh script:

-p | –path <dir_name>

Custom path where you want to install the script.

Note: For global installs, give path outside of the home dir like /usr/bin and it must be in the executable path already.

-c | –cmd <command_name>

Custom command name, after installation, script will be available as the input argument.

To change sync command name, use install sh -c gupload sync=‘gsync’

-r | –repo <Username/reponame>

Install script from your custom repo, e.g –repo labbots/google-drive-upload, make sure your repo file structure is same as official repo.

-B | –branch <branch_name>

Specify branch name for the github repo, applies to custom and default repo both.

-R | –release <tag/release_tag>

Specify tag name for the github repo, applies to custom and default repo both.

-t | –time ‘no of days’

Specify custom auto update time ( given input will taken as number of days ) after which script will try to automatically update itself.

Default: 5 ( 5 days )

-s | –shell-rc <shell_file>

Specify custom rc file, where PATH is appended, by default script detects .zshrc, .bashrc. and .profile.

–sh | –posix

Force install posix scripts even if system has compatible bash binary present.

-q | –quiet

Only show critical error/sucess logs.

-U | –uninstall

Uninstall the script and remove related files.\n

-D | –debug

Display script command trace.

-h | –help

Display usage instructions.

Now, run the script and use flags according to your usecase.

E.g:

curl --compressed -Ls https://github.com/labbots/google-drive-upload/raw/master/install.sh | sh -s -- -r username/reponame -p somepath -s shell_file -c command_name -B branch_name

INITIAL SETUP

First, we need to obtain our oauth credentials, here’s how to do it:

Generating Oauth Credentials

  • Follow Enable Drive API section.
  • Open google console.
  • Click on “Credentials”.
  • Click “Create credentials” and select oauth client id.
  • Select Application type “Desktop app” or “other”.
  • Provide name for the new credentials. ( anything )
  • This would provide a new Client ID and Client Secret.
  • Download your credentials.json by clicking on the download button.

Now, we have obtained our credentials, move to the First run section to use those credentials:

Enable Drive API

  • Log into google developer console at google console.
  • Click select project at the right side of “Google Cloud Platform” of upper left of window.

If you cannot see the project, please try to access to https://console.cloud.google.com/cloud-resource-manager.

You can also create new project at there. When you create a new project there, please click the left of “Google Cloud Platform”. You can see it like 3 horizontal lines.

By this, a side bar is opened. At there, select “API & Services” -> “Library”. After this, follow the below steps:

  • Click “NEW PROJECT” and input the “Project Name”.
  • Click “CREATE” and open the created project.
  • Click “Enable APIs and get credentials like keys”.
  • Go to “Library”
  • Input “Drive API” in “Search for APIs & Services”.
  • Click “Google Drive API” and click “ENABLE”.

Go back to oauth credentials setup

First Run

On first run, the script asks for all the required credentials, which we have obtained in the previous section.

Execute the script: gupload filename

Now, it will ask for following credentials:

Client ID: Copy and paste from credentials.json

Client Secret: Copy and paste from credentials.json

Refresh Token: If you have previously generated a refresh token authenticated to your account, then enter it, otherwise leave blank. If you don’t have refresh token, script outputs a URL on the terminal script, open that url in a web browser and tap on allow. Copy the code and paste in the terminal.

Root Folder: Gdrive folder url/id from your account which you want to set as root folder. You can leave it blank and it takes root folder as default.

If everything went fine, all the required credentials have been set, read the next section on how to upload a file/folder.

CONFIGURATION

After first run, the credentials are saved in config file. By default, the config file is ${HOME}/.googledrive.conf.

To change the default config file or use a different one temporarily, see -z / --config custom in Upload Script Custom Flags.

This is the format of a config file:

ACCOUNT_default_CLIENT_ID="client id"
ACCOUNT_default_CLIENT_SECRET="client secret"
ACCOUNT_default_REFRESH_TOKEN="refresh token"
SYNC_DEFAULT_ARGS="default args of gupload command for gsync"
ACCOUNT_default_ROOT_FOLDER_NAME="root folder name"
ACCOUNT_default_ROOT_FOLDER="root folder id"
ACCOUNT_default_ACCESS_TOKEN="access token"
ACCOUNT_default_ACCESS_TOKEN_EXPIRY="access token expiry"

where default is the name of the account.

You can use a config file in multiple machines, the values that are explicitly required are CLIENT_ID, CLIENT_SECRET and REFRESH_TOKEN.

If ROOT_FOLDER is not set, then it is asked if running in an interactive terminal, otherwise root is used.

ROOT_FOLDER_NAME, ACCESS_TOKEN and ACCESS_TOKEN_EXPIRY are automatically generated using REFRESH_TOKEN.

SYNC_DEFAULT_ARGS is optional.

A pre-generated config file can be also used where interactive terminal access is not possible, like Continuous Integration, docker, jenkins, etc

Just have to print values to "${HOME}/.googledrive.conf", e.g:

printf "%s\n" '
ACCOUNT_default_CLIENT_ID="client id"
ACCOUNT_default_CLIENT_SECRET="client secret"
ACCOUNT_default_REFRESH_TOKEN="refresh token"
' >| "${HOME}/.googledrive.conf"

Note: If you have an old config, then nothing extra is needed, just need to run the script once and the default config will be automatically converted to the new format.

UPDATE

If you have followed the automatic method to install the script, then you can automatically update the script.

There are two methods:

Use the script itself to update the script.

gupload -u or gupload --update

This will update the script where it is installed.

If you use the this flag without actually installing the script,

e.g just by sh upload.sh -u then it will install the script or update if already installed.

Run the installation script again.

Yes, just run the installation script again as we did in install section, and voila, it’s done.

Automatic updates

By default, script checks for update after 5 days. Use -t / –time flag of install.sh to modify the interval.

Note: Above methods always obey the values set by user in advanced installation, e.g if you have installed the script with different repo, say myrepo/gdrive-upload, then the update will be also fetched from the same repo.

UNINSTALLATION

If you have followed the automatic method to install the script, then you can automatically uninstall the script.

There are two methods:

Use the script itself to uninstall the script.

gupload -U or gupload --uninstall

This will remove the script related files and remove path change from shell file.

Run the installation script again with -U/–uninstall flag

curl --compressed -Ls https://github.com/labbots/google-drive-upload/raw/master/install.sh | sh -s -- --uninstall

Yes, just run the installation script again with the flag and voila, it’s done.

Note: Above methods always obey the values set by user in advanced installation.

Read more: https://labbots.github.io/google-drive-upload/ 

Bash shell script upload file to Google Drive on Ubuntu and Linux
11.75 GEEK