Giter Club home page Giter Club logo

google-drive-upload's Introduction

Google drive upload

Stars Latest Release Codacy grade

License Buy us a tree

Google drive upload is a bash compliant script based on v3 google APIs.

It utilizes 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
    • Overwrite or skip existing files.
  • Resume Interrupted Uploads
  • Share files/folders
    • To anyone or a specific email.
  • Config file support
    • Easy to use on multiple machines.
  • Latest gdrive api used i.e v3
  • Pretty logging
  • Easy to install and update
  • An additional sync script for background synchronisation jobs. Read Synchronisation section for more info.

Table of Contents

Compatibility

As this is a bash script, there aren't many dependencies. See Native Dependencies after this section for explicitly required program list.

Linux or MacOS

For Linux or MacOS, you hopefully don't need to configure anything extra, it should work by default.

Android

Install Termux and done.

It's fully tested for all usecases of this script.

iOS

Install iSH

While it has not been officially tested, but should work given the description of the app. Report if you got it working by creating an issue.

Windows

Use Windows Subsystem

Again, it has not been officially tested on windows, there shouldn't be anything preventing it from working. Report if you got it working by creating an issue.

Installing and Updating

Native Dependencies

The script explicitly requires the following programs:

Program Role In Script
Bash Execution of script
Curl All network requests
file/mimetype Mimetype generation for extension less files
find To find files and folders for recursive folder uploads
xargs For parallel uploading
mkdir To create folders
rm To remove files and folders
grep Miscellaneous
sed Miscellaneous
diff To detect new files in a folder ( only sync.sh )
ps To manage background jobs ( only sync.sh )
tail To show indefinite logs ( only sync.sh )

Installation

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

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

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

Config Path ${HOME}/.googledrive.conf

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:

bash <(curl --compressed -s https://raw.githubusercontent.com/labbots/google-drive-upload/master/install.sh)

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:

Click to expand
  • -i | --interactive

    Install script interactively, will ask for all the variables one by one.

    Note: This will disregard all arguments given with below flags.


  • -p | --path <dir_name>

    Custom path where you want to install the script.


  • -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.


  • -s | --shell-rc <shell_file>

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


  • -z | --config

    Specify custom config file path, where credentials will be stored or loaded from.


  • -D | --debug

    Display script command trace.


  • -h | --help

    Display usage instructions.


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

E.g:

bash <(curl --compressed -s https://raw.githubusercontent.com/labbots/google-drive-upload/master/install.sh) -r username/reponame -p somepath -s shell_file -c command_name -B branch_name

Updation

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

There are two methods:

  1. 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 bash upload.sh -u then it will install the script or update if already installed.

  2. Run the installation script again.

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

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.

Usage

First, we need to obtain our Oauth credentials, here's how to do it:

Generating Oauth Credentials

  • Log into google developer console at google console.
  • Create new Project or use existing project.
  • Creating new OAuth 2.0 Credentials:
    • Select Application type "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.
  • Enable Google Drive API for the project under "Library".

Now, we have obtained our credentials, move to next section to use those credentials to 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.

Upload

For uploading files/remote gdrive files, the syntax is simple;

gupload filename/foldername/file_id/file_link gdrive_folder_name

where filename/foldername is input file/folder and gdrive_folder_name is the name of the folder on gdrive, where the input file/folder will be uploaded.

and file_id/file_link is the accessible gdrive file link or id which will be uploaded without downloading.

If gdrive_folder_name is present on gdrive, then script will upload there, else will make a folder with that name.

Apart from basic usage, this script provides many flags for custom usecases, like parallel uploading, skipping upload of existing files, overwriting, etc.

Upload Script Custom Flags

These are the custom flags that are currently implemented:

  • -z | --config

    Override default config file with custom config file.

    Default Config: ${HOME}/.googledrive.conf

    If you want to change the default value of the config path, then use this format,

    gupload --config default=your_config_file_path


  • -C | --create-dir

    Option to create directory. Will provide folder id. Can be used to specify workspace folder for uploading files/folders.


  • -r | --root-dir <google_folderid>

    Google folder id or url to which the file/directory to upload.

    If you want to change the default value of the rootdir stored in config, then use this format,

    gupload --root-dir default=root_folder_[id/url]


  • -s | --skip-subdirs

    Skip creation of sub folders and upload all files inside the INPUT folder/sub-folders in the INPUT folder, use this along with -p/--parallel option to speed up the uploads.


  • -p | --parallel <no_of_files_to_parallely_upload>

    Upload multiple files in parallel, Max value = 10, use with folders.

    Note:

    • This command is only helpful if you are uploading many files which aren't big enough to utilise your full bandwidth, using it otherwise will not speed up your upload and even error sometimes,
    • 1 - 6 value is recommended, but can use upto 10. If errors with a high value, use smaller number.
    • Beaware, this isn't magic, obviously it comes at a cost of increased cpu/ram utilisation as it forks multiple bash processes to upload ( google how xargs works with -P option ).

  • -o | --overwrite

    Overwrite the files with the same name, if present in the root folder/input folder, also works with recursive folders and single/multiple files.

    Note: If you use this flag along with -d/--skip-duplicates, the skip duplicates flag is preferred.


  • -d | --skip-duplicates

    Do not upload the files with the same name, if already present in the root folder/input folder, also works with recursive folders.


  • -f | --file/folder

    Specify files and folders explicitly in one command, use multiple times for multiple folder/files.

    For uploading multiple input into the same folder:

    • Use -C / --create-dir ( e.g ./upload.sh -f file1 -f folder1 -f file2 -C <folder_wherw_to_upload> ) option.

    • Give two initial arguments which will use the second argument as the folder you wanna upload ( e.g: ./upload.sh filename <folder_where_to_upload> -f filename -f foldername ).

      This flag can also be used for uploading files/folders which have - character in their name, normally it won't work, because of the flags, but using -f -[file|folder]namewithhyphen works. Applies for -C/--create-dir too.

      Also, as specified by longflags ( --[file|folder] ), you can simultaneously upload a folder and a file.

      Incase of multiple -f flag having duplicate arguments, it takes the last duplicate of the argument to upload, in the same order provided.


  • -cl | --clone

    Upload a gdrive file without downloading, require accessible gdrive link or id as argument.


  • -S | --share <optional_email_address>

    Share the uploaded input file/folder, grant reader permission to provided email address or to everyone with the shareable link.


  • -q | --quiet

    Supress the normal output, only show success/error upload messages for files, and one extra line at the beginning for folder showing no. of files and sub folders.


  • -v | --verbose

    Dislay detailed message (only for non-parallel uploads).


  • -V | --verbose-progress

    Display detailed message and detailed upload progress(only for non-parallel uploads).


  • --skip-internet-check

    Do not check for internet connection, recommended to use in sync jobs.


  • -i | --save-info <file_to_save_info>

    Save uploaded files info to the given filename."


  • -u | --update

    Update the installed script in your system, if not installed, then install.


  • --info

    Show detailed info, only if script is installed system wide.


  • -h | --help

    Display usage instructions.


  • -D | --debug

    Display script command trace.


Multiple Inputs

For using multiple inputs at a single time, you can use the -f/--file/--folder or -cl/--clone flag as explained above.

Now, to achieve multiple inputs without flag, it gets a little tricky. Some of them are explained below along with other usecases.

e.g:

  • gupload a b or gupload -d -o -D a b

    a is file/folder/gdrive_link_or_id and b is gdrive_folder


  • gupload -d -o a b c d -d

    a,b,c and d are file/folder/gdrive_link_or_id. Using multiple inputs with -f flag.


  • gupload a b -d c d

    a and c are file/folder/gdrive_link_or_id but b and d are gdrive folder, but since d is given at last, it will taken as gdrive folder.


  • gupload a b -d -o c d e

    a, c, d and e is file/folder/gdrive_link_or_id and b is gdrive_folder


Resuming Interrupted Uploads

Uploads interrupted either due to bad internet connection or manual interruption, can be resumed from the same position.

  • Script checks 3 things, filesize, name and workspace folder. If an upload was interrupted, then resumable upload link is saved in "$HOME/.google-drive-upload/", which later on when running the same command as before, if applicable, resumes the upload from the same position as before.
  • Small files cannot be resumed, less that 1 MB, and the amount of size uploaded should be more than 1 MB to resume.
  • No progress bars for resumable uploads as it messes up with output.
  • You can interrupt many times you want, it will resume ( hopefully ).

Additional Usage

Synchronisation

This repo also provides an additional script ( sync.sh ) to utilise upload.sh for synchronisation jobs, i.e background jobs.

Basic Usage

To create a sync job, just run

gsync folder_name -d gdrive_folder

Here, folder_name is the local folder you want to sync and gdrive_folder is google drive folder name.

In the local folder, all the contents present or added in the future will be automatically uploaded.

Note: Giving gdrive_folder is optional, if you don't specify a name with -d/--directory flags, then it will take the name of the local folder.

Also, gdrive folder creation works in the same way as gupload command.

Default wait time: 3 secs ( amount of time to wait before checking new files ).

Default gupload arguments: None ( see -a/--arguments section below ).

Sync Script Custom Flags

Read this section thoroughly to fully utilise the sync script, feel free to open an issue if any doubts regarding the usage.

Click to expand
  • -d | --directory

    Specify gdrive folder name, if not specified then local folder name is used.


  • -j | --jobs

    See all background jobs that were started and still running.

    Use -j/--jobs v/verbose to show additional information for jobs.

    Additional information includes: CPU usage & Memory usage and No. of failed & successful uploads.


  • -p | --pid

    Specify a pid number, used for --jobs or --kill or --info flags, multiple usage allowed.


  • -i | --info

    Print information for a specific job. These are the methods to do it:

    • By specifying local folder and gdrive folder of an existing job,

      e.g: gsync local_folder -d gdrive folder -i

    • By specifying pid number,

      e.g: gsync -i -p pid_number

    • To show info of multiple jobs, use this flag multiple times,

      e.g: gsync -i pid1 -p pid2 -p pid3. You can also use it with multiple inputs by adding this flag.


  • -k | --kill

    Kill background jobs, following are methods to do it:

    • By specifying local_folder and gdrive_folder,

      e.g. gsync local_folder -d gdrive_folder -k, will kill that specific job.

    • pid ( process id ) number can be used as an additional argument to kill a that specific job,

      e.g: gsync -k -p pid_number.

    • To kill multiple jobs, use this flag multiple times,

      e.g: gsync -k pid1 -p pid2 -p pid3. You can also using it with multiple inputs with this flag.

    • This flag can also be used to kill all the jobs,

      e.g: gsync -k all. This will stop all the background jobs running.


  • -t | --time time_in_seconds

    The amount of time that sync will wait before checking new files in the local folder given to sync job.

    e.g: gsync -t 4 local_folder, here 4 is the wait time.

    To set default time, use gsync local_folder -t default=4, it will stored in your default config.


  • -l | --logs

    To show the logs after starting a job or show log of existing job.

    This option can also be used to make a job sync on foreground, rather in background, thus ctrl + c or ctrl +z can exit the job.

    • By specifying local_folder and gdrive_folder,

      e.g. gsync local_folder -d gdrive_folder -l, will show logs of that specific job.

    • pid ( process id ) number can be used as an additional argument to show logs of a specific job,

      e.g: gsync -l -p pid_number.

    Note: If used with multiple inputs or pid numbers, then only first pid/input log is shown, as it goes on indefinitely.


  • -a | --arguments

    As the script uses gupload, you can specify custom flags for background job,

    e.g: gsync local_folder -a '-q -p 4 -d'

    To set some arguments by default, use gsync -a default='-q -p 4 -d'.

    In this example, will skip existing files, 4 parallel upload in case of folder.


  • -d | --debug

    Display script command trace, use before all the flags to see maximum script trace.


Note: Flags that use pid number as input should be used at last, if you are not intending to provide pid number, say in case of a folder name with positive integers.

Uninstall

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

There are two methods:

  1. 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.

  2. Run the installation script again with -U/--uninstall flag

    bash <(curl --compressed -s https://raw.githubusercontent.com/labbots/google-drive-upload/master/install.sh) --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.

Reporting Issues

Issues Status GitHub issues GitHub issues-closed

Use the GitHub issue tracker for any bugs or feature suggestions.

Before creating an issue, make sure to follow the guidelines specified in CONTRIBUTION.md

Contributing

Total Contributers GitHub contributors
Pull Requests GitHub pull-requests GitHub pull-requests closed

Submit patches to code or documentation as GitHub pull requests! Check out the contribution guide

Contributions must be licensed under the MIT. The contributor retains the copyright.

Inspired By

License

MIT

Treeware

Buy us a tree

This package is Treeware. You are free to use this package, but if you use it in production, then we would highly appreciate you buying the world a tree to thank us for our work. By contributing to the Treeware forest you’ll be creating employment for local families and restoring wildlife habitats.

google-drive-upload's People

Contributors

labbots avatar akianonymus avatar dhiva avatar jimmyppi avatar michaelgchu avatar duckydreams avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.