GitHub - labbots/google-drive-upload: Bash scripts to upload files to google dri...
source link: https://github.com/labbots/google-drive-upload
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Google drive upload
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
- 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
- Auto update
- An additional sync script for background synchronisation jobs. Read Synchronisation section for more info.
Table of Contents
Compatibility
As this is a collection of shell scripts, 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.
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
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
This repo contains two types of scripts, posix compatible and bash compatible.
These programs are required in both bash and posix scripts.
Program | Role In Script |
---|---|
curl | All network requests |
file or 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 |
mktemp | To generate temporary files ( optional ) |
sleep | Self explanatory |
If BASH is not available or BASH is available but version is less tham 4.x, then below programs are also required:
Program | Role In Script |
---|---|
awk | For url encoding in doing api requests |
date | For installation, update and Miscellaneous |
cat | Miscellaneous |
stty or zsh or tput | To determine column size ( optional ) |
These programs are needed for synchronisation script:
Program | Role In Script |
---|---|
ps | To manage background jobs |
tail | To show indefinite logs |
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.
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
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:
curl --compressed -s https://raw.githubusercontent.com/labbots/google-drive-upload/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:
Click to expand
Updation
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.
An update log is saved in
"${HOME}/.gdrive-downloader/update.log"
.
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 -c 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.
Note: It's not mandatory to use -c / -C / --create-dir flag.
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 | -C | --create-dir
Option to create directory. Will provide folder id. Can be used to specify workspace folder for uploading files/folders.
If this option is used, then input file is optional.
-
-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 shell 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.
-
--speed 'speed'
Limit the download speed, supported formats: 1K, 1M and 1G.
-
-R | --retry 'num of retries'
Retry the file upload if it fails, postive integer as argument. Currently only for file uploads.
-
-in | --include 'pattern'
Only include the files with the given pattern to upload - Applicable for folder uploads.
e.g: gupload local_folder --include "1", will only include the files with pattern '1' in the name.
Note: Only provide patterns which are supported by find -name option.
-
-ex | --exclude 'pattern'
e.g: gupload local_folder --exclude "1", will exclude all the files with pattern '1' in the name.
Note: Only provide patterns which are supported by find -name option.
-
--hide
This flag will prevent the script to print sensitive information like root folder id or drivelink
-
-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, you can just use glob or just give them as arguments.
-
gupload a b c d
a/b/c/d will be treated as file/folder/gdrive_link_or_id.
-
gupload
*mp4 *mkv
This will upload all the mp4 and mkv files in the folder, if any.
To upload all files, just use *. For more info, google how globs work in shell.
-
gupload a b -d c d -c e
a/b/c/d will be treated as file/folder/gdrive_link_or_id and e as
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 upload in the root folder set by gupload command.
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
Background Sync Jobs
There are basically two ways to start a background job, first one we already covered in the above section.
It will indefinitely run until and unless the host machine is rebooted.
Now, a systemd service service can also be created which will start sync job after boot.
-
To generate a systemd unit file, run the sync command with
--service service_name
at the end.e.g: If
gsync foldername -d drive_folder --service myservice
, where, myservice can be any name desired.This will generate a script and print the next required commands to start/stop/enable/disable the service.
The commands that will be printed is explained below:
-
Start the service
sh gsync-test.service.sh start
, where gsync-test is the service nameThis is same as starting a sync job with command itself as mentioned in previous section.
To stop:
sh gsync-test.service.sh stop
-
If you want the job to automatically start on boot, run
sh gsync-test.service.sh enable
To disable:
sh gsync-test.service.sh disable
-
To see logs after a job has been started.
sh gsync-test.service.sh logs
-
To remove a job from system,
sh gsync-test.service.sh remove
You can use multiple commands at once, e.g: sh gsync-test.service.sh start logs
, will start and show the logs.
Note: The script is merely a wrapper, it uses systemctl
to start/stop/enable/disable the service and journalctl
is used to show the logs.
Extras: A sample service file has been provided in the repo just for reference, it is recommended to use gsync
to generate the service file.
Uninstall
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 -s https://raw.githubusercontent.com/labbots/google-drive-upload/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.
Reporting Issues
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
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
- github-bashutils - soulseekah/bash-utils
- deanet-gist - Uploading File into Google Drive
- Bash Bible - A collection of pure bash alternatives to external processes
- sh bible - A collection of posix alternatives to external processes
License
Treeware
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.
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK