@d0whc3r/node-gdrive 3.4.1 → 3.5.0
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 +21 -0
- package/README.md +174 -92
- package/bin/cli.js +2 -634
- package/dist/auth.d.ts +25 -0
- package/dist/config.d.ts +16 -0
- package/dist/crypto.d.ts +4 -0
- package/dist/gdrive.cjs.js +1 -1
- package/dist/gdrive.esm.mjs +802 -0
- package/dist/index.d.ts +65 -48
- package/dist/retry.d.ts +3 -0
- package/dist/types.d.ts +96 -0
- package/package.json +83 -139
- package/CHANGELOG.md +0 -10
- package/dist/gdrive.esm.js +0 -1
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
|
-
#
|
|
1
|
+
# node-gdrive
|
|
2
2
|
|
|
3
|
-
|
|
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
|
-
|
|
5
|
+
[](https://github.com/d0whc3r/node-gdrive/actions/workflows/ci.yml)
|
|
6
|
+
[](https://www.npmjs.com/package/@d0whc3r/node-gdrive)
|
|
7
|
+
[](https://hub.docker.com/r/d0whc3r/gdrive)
|
|
8
|
+
[](https://nodejs.org/)
|
|
9
|
+
[](./LICENSE)
|
|
6
10
|
|
|
7
|
-
|
|
8
|
-
[](https://codecov.io/gh/d0whc3r/node-gdrive)
|
|
9
|
-
[](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
|
-
|
|
12
|
-
[](https://sonarcloud.io/dashboard?id=d0whc3r_node-gdrive)
|
|
13
|
-
[](https://sonarcloud.io/dashboard?id=d0whc3r_node-gdrive)
|
|
14
|
-
[](https://sonarcloud.io/dashboard?id=d0whc3r_node-gdrive)
|
|
15
|
-
[](https://sonarcloud.io/dashboard?id=d0whc3r_node-gdrive)
|
|
13
|
+
## Ways to Run
|
|
16
14
|
|
|
17
|
-
|
|
18
|
-
[](https://www.npmjs.com/package/@d0whc3r/node-gdrive)
|
|
15
|
+
### 1. Docker (No Node.js required)
|
|
19
16
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
[](https://hub.docker.com/r/d0whc3r/gdrive)
|
|
23
|
-
|
|
24
|
-
[](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
|
-
|
|
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
|
-
|
|
29
|
-
|
|
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
|
-
|
|
30
|
+
### 2. npx / npm (Node.js >= 26)
|
|
32
31
|
|
|
33
|
-
|
|
34
|
-
|
|
32
|
+
```bash
|
|
33
|
+
# Run on demand
|
|
34
|
+
npx @d0whc3r/node-gdrive --help
|
|
35
35
|
|
|
36
|
-
|
|
36
|
+
# Or install globally
|
|
37
|
+
npm install -g @d0whc3r/node-gdrive
|
|
38
|
+
node-gdrive --help
|
|
39
|
+
```
|
|
37
40
|
|
|
38
|
-
|
|
41
|
+
### 3. Standalone Binary (No Node.js required)
|
|
39
42
|
|
|
40
|
-
|
|
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
|
-
|
|
46
|
+
chmod +x ./node-gdrive
|
|
47
|
+
./node-gdrive --help
|
|
44
48
|
```
|
|
45
49
|
|
|
46
|
-
|
|
50
|
+
---
|
|
47
51
|
|
|
48
|
-
|
|
52
|
+
## Authentication Setup
|
|
49
53
|
|
|
50
|
-
|
|
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
|
-
|
|
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
|
-
|
|
61
|
+
GOOGLE_APPLICATION_CREDENTIALS=./secrets/service-account.json \
|
|
62
|
+
node-gdrive -l --drive-id <SHARED_DRIVE_ID>
|
|
60
63
|
```
|
|
61
64
|
|
|
62
|
-
### :
|
|
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
|
-
|
|
71
|
+
---
|
|
65
72
|
|
|
66
|
-
|
|
73
|
+
## Common Usage Examples
|
|
67
74
|
|
|
68
|
-
|
|
69
|
-
-
|
|
70
|
-
-
|
|
75
|
+
```bash
|
|
76
|
+
# List files (human-readable or JSON for scripts)
|
|
77
|
+
node-gdrive -l
|
|
78
|
+
node-gdrive -l --json
|
|
71
79
|
|
|
72
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
79
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
90
|
-
|
|
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
|
-
|
|
204
|
+
### CommonJS
|
|
94
205
|
|
|
95
|
-
|
|
206
|
+
```js
|
|
207
|
+
const { GDrive } = require('@d0whc3r/node-gdrive');
|
|
96
208
|
|
|
97
|
-
|
|
98
|
-
|
|
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
|
-
|
|
137
|
-
[
|
|
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).
|