@d0whc3r/node-gdrive 3.4.1 → 3.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2020-present d0whc3r
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,137 +1,219 @@
1
- # :floppy_disk: Node gdrive
1
+ # node-gdrive
2
2
 
3
- Utility to manipulate google drive using nodejs. It could upload content to google drive and it could be used as backup service.
3
+ CLI and Node.js library to back up, restore, encrypt, and prune files on **Google Drive**—works via **Docker**, **npx/npm**, or as a **standalone binary** (Linux, macOS, Windows).
4
4
 
5
- # :eyes: Project status
5
+ [![CI](https://img.shields.io/github/actions/workflow/status/d0whc3r/node-gdrive/ci.yml?branch=master&style=flat-square&logo=githubactions&logoColor=white&label=CI)](https://github.com/d0whc3r/node-gdrive/actions/workflows/ci.yml)
6
+ [![npm](https://img.shields.io/npm/v/@d0whc3r/node-gdrive?style=flat-square&logo=npm&logoColor=white)](https://www.npmjs.com/package/@d0whc3r/node-gdrive)
7
+ [![Docker Hub](https://img.shields.io/docker/v/d0whc3r/gdrive?style=flat-square&logo=docker&logoColor=white&label=docker)](https://hub.docker.com/r/d0whc3r/gdrive)
8
+ [![Node.js](https://img.shields.io/badge/Node.js-%3E%3D26-339933?style=flat-square&logo=nodedotjs&logoColor=white)](https://nodejs.org/)
9
+ [![License: MIT](https://img.shields.io/badge/License-MIT-blue?style=flat-square)](./LICENSE)
6
10
 
7
- [![pipeline status](https://gitlab.com/d0whc3r/node-gdrive/badges/master/pipeline.svg)](https://github.com/d0whc3r/node-gdrive)
8
- [![codecov](https://codecov.io/gh/d0whc3r/node-gdrive/branch/master/graph/badge.svg)](https://codecov.io/gh/d0whc3r/node-gdrive)
9
- [![Codacy Badge](https://api.codacy.com/project/badge/Grade/2612116bab5f493cada70bffc3cb6492)](https://www.codacy.com/app/d0whc3r/node-gdrive?utm_source=github.com&utm_medium=referral&utm_content=d0whc3r/node-gdrive&utm_campaign=Badge_Grade)
11
+ ---
10
12
 
11
- [![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=d0whc3r_node-gdrive&metric=alert_status)](https://sonarcloud.io/dashboard?id=d0whc3r_node-gdrive)
12
- [![Maintainability Rating](https://sonarcloud.io/api/project_badges/measure?project=d0whc3r_node-gdrive&metric=sqale_rating)](https://sonarcloud.io/dashboard?id=d0whc3r_node-gdrive)
13
- [![Security Rating](https://sonarcloud.io/api/project_badges/measure?project=d0whc3r_node-gdrive&metric=security_rating)](https://sonarcloud.io/dashboard?id=d0whc3r_node-gdrive)
14
- [![Bugs](https://sonarcloud.io/api/project_badges/measure?project=d0whc3r_node-gdrive&metric=bugs)](https://sonarcloud.io/dashboard?id=d0whc3r_node-gdrive)
15
- [![Vulnerabilities](https://sonarcloud.io/api/project_badges/measure?project=d0whc3r_node-gdrive&metric=vulnerabilities)](https://sonarcloud.io/dashboard?id=d0whc3r_node-gdrive)
13
+ ## Ways to Run
16
14
 
17
- ![dependencies](https://img.shields.io/david/d0whc3r/node-gdrive.svg)
18
- [![npm version](https://img.shields.io/npm/v/@d0whc3r%2Fnode-gdrive.svg)](https://www.npmjs.com/package/@d0whc3r/node-gdrive)
15
+ ### 1. Docker (No Node.js required)
19
16
 
20
- [![](https://img.shields.io/docker/cloud/build/d0whc3r/gdrive.svg)](https://hub.docker.com/r/d0whc3r/gdrive)
21
- [![](https://images.microbadger.com/badges/version/d0whc3r/gdrive.svg)](https://hub.docker.com/r/d0whc3r/gdrive)
22
- [![](https://images.microbadger.com/badges/image/d0whc3r/gdrive.svg)](https://hub.docker.com/r/d0whc3r/gdrive)
23
-
24
- [![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2Fd0whc3r%2Fnode-gdrive.svg?type=shield)](https://app.fossa.io/projects/git%2Bgithub.com%2Fd0whc3r%2Fnode-gdrive?ref=badge_shield)
17
+ ```bash
18
+ docker run --rm d0whc3r/gdrive --help
25
19
 
26
- ## :key: Create credentials and token files
20
+ # Mount your secrets folder read-only and list files
21
+ docker run --rm -v "$PWD/secrets:/app/secrets:ro" d0whc3r/gdrive -l
27
22
 
28
- To get `credentials.json` file you need to visit:
29
- - [https://developers.google.com/drive/api/v3/quickstart/nodejs](https://developers.google.com/drive/api/v3/quickstart/nodejs)
23
+ # Back up a local directory into Google Drive
24
+ docker run --rm \
25
+ -v "$PWD/secrets:/app/secrets:ro" \
26
+ -v "$PWD/data:/data:ro" \
27
+ d0whc3r/gdrive -b /data -z backup.zip -f backups/daily -c -r
28
+ ```
30
29
 
31
- Then click `ENABLE THE DRIVE API` and click `DOWNLOAD CLIENT CONFIGURATION`
30
+ ### 2. npx / npm (Node.js >= 26)
32
31
 
33
- This will download **credentials.json** file, you can save it in *secrets* folder in the project.
34
- Now you need to generate **token.json** file, to do this you need to execute any command to access your google-drive, for example:
32
+ ```bash
33
+ # Run on demand
34
+ npx @d0whc3r/node-gdrive --help
35
35
 
36
- ### :boat: Using docker
36
+ # Or install globally
37
+ npm install -g @d0whc3r/node-gdrive
38
+ node-gdrive --help
39
+ ```
37
40
 
38
- Then you could execute a simple command to generate token.json:
41
+ ### 3. Standalone Binary (No Node.js required)
39
42
 
40
- > Be careful to copy `credentials.json` into `$PWD/secrets` path
43
+ Download the prebuilt `node-gdrive` (Linux / macOS) or `node-gdrive.exe` (Windows) executable from [GitHub Releases](https://github.com/d0whc3r/node-gdrive/releases) and run it directly:
41
44
 
42
45
  ```bash
43
- docker run --rm -it -v $PWD/secrets:/app/secrets/ d0whc3r/gdrive -l
46
+ chmod +x ./node-gdrive
47
+ ./node-gdrive --help
44
48
  ```
45
49
 
46
- ### :pencil: Using downloaded project
50
+ ---
47
51
 
48
- > First you need to download project from github using:
52
+ ## Authentication Setup
49
53
 
50
- ```bash
51
- git clone https://github.com/d0whc3r/node-gdrive.git
52
- cd node-gdrive
53
- yarn install
54
- ```
54
+ ### Option A: Service Account (Recommended for Servers, Cron & Docker)
55
55
 
56
- Then you could execute a simple command to generate token.json:
56
+ 1. In [Google Cloud Console](https://console.cloud.google.com/), enable the **Google Drive API**, create a **Service Account**, and download its JSON key to `./secrets/service-account.json`.
57
+ 2. Share your target Google Drive folder or **Shared Drive** with the Service Account email address.
58
+ 3. Run with `GOOGLE_APPLICATION_CREDENTIALS` or `--service-account`:
57
59
 
58
60
  ```bash
59
- yarn cli -l
61
+ GOOGLE_APPLICATION_CREDENTIALS=./secrets/service-account.json \
62
+ node-gdrive -l --drive-id <SHARED_DRIVE_ID>
60
63
  ```
61
64
 
62
- ### :beginner: Next step
65
+ ### Option B: OAuth2 Desktop App (Personal Google Drive)
66
+
67
+ 1. In [Google Cloud Console](https://console.cloud.google.com/), enable the **Google Drive API**, create an **OAuth 2.0 Desktop App** client, and save the JSON as `./secrets/credentials.json`.
68
+ 2. Run any command (for example, `node-gdrive -l`).
69
+ 3. Open the printed authorization URL in your browser and approve access. `node-gdrive` captures the callback automatically via a temporary local loopback server (`http://127.0.0.1:<port>/oauth2callback`, PKCE + `state`) and saves `./secrets/token.json` with `0600` permissions.
63
70
 
64
- Using docker or project files the `-l` command is for listing existing files in google-drive
71
+ ---
65
72
 
66
- If *credentials.json* is present but *token.json* is missing this last file will be generated showing an url into terminal output, you need to enter that url and validate all needed permisions, then a token will be shown in your web browser, this token need to be pasted in terminal.
73
+ ## Common Usage Examples
67
74
 
68
- By default this software check for credentials (and token) file in folder `secrets` but it could be changed using environment variables:
69
- - **CREDENTIALS_FILE**: File with credentials (credentials.json)
70
- - **TOKEN_FILE**: File with token, generated using credentials (token.json)
75
+ ```bash
76
+ # List files (human-readable or JSON for scripts)
77
+ node-gdrive -l
78
+ node-gdrive -l --json
71
79
 
72
- ## :boat: Docker usage
80
+ # Back up files or globs to a nested folder (creating missing folders)
81
+ node-gdrive -b "src/*" -b package.json -f backups/app -c -r
73
82
 
74
- You could use cli app in docker
83
+ # Back up as a ZIP archive, excluding logs, with client-side AES-256-GCM encryption
84
+ BACKUP_ENCRYPTION_KEY="my-strong-passphrase" \
85
+ node-gdrive -b "./data/*" --exclude "*.log" -z backup.zip -f backups -c -r --encrypt
75
86
 
76
- ### :rowboat: Build docker image
87
+ # Download and decrypt a backup
88
+ BACKUP_ENCRYPTION_KEY="my-strong-passphrase" \
89
+ node-gdrive --download backup.zip.enc -f backups -o ./backup.zip --encrypt
77
90
 
78
- ```bash
79
- docker build -t gdrive .
91
+ # Pipe a PostgreSQL dump directly to Google Drive without writing to disk
92
+ pg_dump mydb | node-gdrive --stdin --name mydb.sql -f backups/pg -c
93
+
94
+ # Preview retention cleanup safely before deleting anything
95
+ node-gdrive -f backups/daily -d 30d --keep-last 5 --dry-run
96
+
97
+ # Nightly cron job: MySQL dump + zip + encrypt + prune older than 14 days (keep at least 5)
98
+ node-gdrive -m -z -f backups/mysql -c --encrypt && \
99
+ node-gdrive -f backups/mysql -d 14d --keep-last 5
80
100
  ```
81
101
 
82
- ### :checkered_flag: Run cli commands inside docker
102
+ ---
103
+
104
+ ## Environment Variables
105
+
106
+ | Variable | Description | Default |
107
+ | ---------------------------------------------------------------------------- | --------------------------------------------------- | ---------------------------- |
108
+ | `GOOGLE_APPLICATION_CREDENTIALS` | Path to Service Account JSON key (standard GCP var) | — |
109
+ | `SERVICE_ACCOUNT_FILE` | Alias for Service Account JSON key path | — |
110
+ | `CREDENTIALS_FILE` | Path to OAuth2 Desktop `credentials.json` | `./secrets/credentials.json` |
111
+ | `TOKEN_FILE` | Path to stored OAuth2 `token.json` (`0600`) | `./secrets/token.json` |
112
+ | `BACKUP_ENCRYPTION_KEY` | Passphrase for `--encrypt` (AES-256-GCM) | — |
113
+ | `MYSQL_HOST`, `MYSQL_PORT`, `MYSQL_USER`, `MYSQL_PASSWORD`, `MYSQL_DATABASE` | MySQL connection for `--mysql` | `localhost:3306` |
114
+
115
+ See [example.env](./example.env) for a ready-to-copy template.
116
+
117
+ ---
118
+
119
+ ## CLI Options
120
+
121
+ | Flag | Alias | Description |
122
+ | -------------------------- | ----- | ---------------------------------------------------------------------- |
123
+ | `--list` | `-l` | List files and folders |
124
+ | `--backup <file*>` | `-b` | Upload files, directories, or globs (repeatable) |
125
+ | `--download <id-or-name>` | — | Download a file by ID or name |
126
+ | `--output <path>` | `-o` | Local destination path for `--download` |
127
+ | `--folder <path>` | `-f` | Destination folder path (supports nested paths like `backups/daily`) |
128
+ | `--zip [filename.zip]` | `-z` | Compress backup files into a ZIP archive |
129
+ | `--exclude <glob>` | — | Exclude files matching glob pattern (repeatable) |
130
+ | `--replace` | `-r` | Replace existing file in target folder on upload |
131
+ | `--create` | `-c` | Create destination folder(s) if they do not exist |
132
+ | `--delete <rule>` | `-d` | Delete files older than duration (`folder=7d` or `7d`, repeatable) |
133
+ | `--keep-last <N>` | — | Always preserve the N newest matching files when deleting |
134
+ | `--dry-run` | — | Preview uploads/deletions without modifying Drive |
135
+ | `--encrypt` | — | Encrypt on upload / decrypt on download using `$BACKUP_ENCRYPTION_KEY` |
136
+ | `--stdin` | — | Read upload payload from standard input (requires `--name`) |
137
+ | `--name <filename>` | — | Filename when uploading from `--stdin` |
138
+ | `--mysql` | `-m` | Create and upload a MySQL dump using `$MYSQL_*` env vars |
139
+ | `--drive-id <id>` | — | Target a Google Workspace Shared Drive |
140
+ | `--service-account <path>` | — | Path to a Service Account JSON key |
141
+ | `--json` | — | Output machine-readable JSON (ideal for CI/scripts) |
142
+ | `--quiet` | `-q` | Suppress informational logs |
143
+ | `--version` | `-v` | Print CLI version |
144
+ | `--help` | `-h` | Print usage guide |
145
+
146
+ ### Valid Duration Units (`-d`)
147
+
148
+ | Unit | Meaning | Example |
149
+ | ---- | ------- | ------- |
150
+ | `s` | Seconds | `30s` |
151
+ | `m` | Minutes | `15m` |
152
+ | `h` | Hours | `24h` |
153
+ | `d` | Days | `7d` |
154
+ | `w` | Weeks | `2w` |
155
+ | `M` | Months | `1M` |
156
+ | `y` | Years | `1y` |
157
+
158
+ > Unrecognized units are rejected immediately to prevent accidental deletions.
159
+
160
+ ### Exit Codes
161
+
162
+ - `0`: Success (or `--help` / `--version`).
163
+ - `1`: Runtime or Google Drive API error.
164
+ - `2`: Invalid CLI arguments or missing required options.
165
+
166
+ ---
167
+
168
+ ## Using as a Node.js Library
83
169
 
84
170
  ```bash
85
- docker run --rm gdrive --help
171
+ npm install @d0whc3r/node-gdrive
86
172
  ```
87
- To make docker image work properly you need to indicate where credentials and token are located
88
173
 
89
- ```bash
90
- docker run --rm -v $PWD/secrets:/app/secrets:ro gdrive -l
174
+ ### ESM / TypeScript
175
+
176
+ ```ts
177
+ import { GDrive } from '@d0whc3r/node-gdrive';
178
+
179
+ const gdrive = new GDrive({
180
+ serviceAccountFile: './secrets/service-account.json',
181
+ sharedDriveId: '0AAxxxxxxxxUk9PVA',
182
+ concurrency: 4
183
+ });
184
+
185
+ // Upload with ZIP compression, exclusions, and AES-256-GCM encryption
186
+ await gdrive.uploadFiles(['./data/*'], 'backups/daily', {
187
+ create: true,
188
+ replace: true,
189
+ compress: 'daily.zip',
190
+ exclude: ['*.log', '*.tmp'],
191
+ encryptKey: process.env.BACKUP_ENCRYPTION_KEY
192
+ });
193
+
194
+ // Download and decrypt
195
+ await gdrive.downloadFile('daily.zip.enc', './restore/daily.zip', {
196
+ folderName: 'backups/daily',
197
+ decryptKey: process.env.BACKUP_ENCRYPTION_KEY
198
+ });
199
+
200
+ // Delete files older than 14 days while keeping at least the 5 newest
201
+ await gdrive.cleanOlder('14d', 'backups/daily', { keepLast: 5 });
91
202
  ```
92
203
 
93
- ## :checkered_flag: Cli help output
204
+ ### CommonJS
94
205
 
95
- Using docker image from [hub.docker.com](https://hub.docker.com/r/d0whc3r/gdrive)
206
+ ```js
207
+ const { GDrive } = require('@d0whc3r/node-gdrive');
96
208
 
97
- ```bash
98
- docker run --rm d0whc3r/gdrive --help
209
+ const gdrive = new GDrive();
210
+ gdrive.listFiles().then((files) => console.log(files));
99
211
  ```
100
212
 
101
- ```
102
- Help for gdrive
103
-
104
- Usage of npm gdrive in command line.
105
-
106
- Options
107
-
108
- -l, --list List all files and folders
109
- -b, --backup file* Backup files
110
- -z, --zip zipname.zip Zip backup files
111
- -r, --replace Replace files if already exists when backup upload
112
- -c, --create Create destination upload folder
113
- -f, --folder foldername Folder name to upload file/s
114
- -d, --delete foldername=duration OR duration Clean files older than duration in foldername
115
- -m, --mysql Mysql backup using environment variables to connect mysql server
116
- ($MYSQL_USER, $MYSQL_PASSWORD, $MYSQL_DATABASE, $MYSQL_HOST, $MYSQL_PORT)
117
- -h, --help Print this usage guide.
118
-
119
- Examples
120
-
121
- 1. List files. $ gdrive -l
122
- 2. Backup multiple files to "backupFolder" folder. $ gdrive -b src/index.ts -b images/logo.png -f backupFolder
123
- 3. Backup files using wildcard to "backup" folder. $ gdrive -b src/* -b images/* -f backup
124
- 4. Backup files using wildcard and zip into "zipped" folder folder will be $ gdrive -b src/* -b images/* -z -f zipped -c
125
- created if it doesn't exists.
126
- 5. Backup files using wildcard and zip using "allfiles.zip" as filename into $ gdrive -b src/* -b images/* -z allfiles.zip -f zipped -c -r
127
- "zipped" folder folder will be created if it doesn't exists and zipfile will be
128
- replaced if it exists
129
- 6. Delete files in "uploads" folder older than 2days and files in "monthly" $ gdrive -d uploads=2d -d monthly=1M
130
- folder older than 1month
131
- 7. Delete files in "uploads" folder older than 1minute $ gdrive -f uploads -d 1m
132
- 8. Generate mysql dump file zip it and upload to "mysql-backup" folder $ gdrive -f mysql-backup -m -z
133
- ```
213
+ ---
134
214
 
215
+ ## Contributing, Security & License
135
216
 
136
- ## License
137
- [![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2Fd0whc3r%2Fnode-gdrive.svg?type=large)](https://app.fossa.io/projects/git%2Bgithub.com%2Fd0whc3r%2Fnode-gdrive?ref=badge_large)
217
+ - Want to contribute or build from source? See [CONTRIBUTING.md](./CONTRIBUTING.md).
218
+ - Found a security issue? See [SECURITY.md](./SECURITY.md).
219
+ - Released under the [MIT License](./LICENSE).