@softistx/mongo-backup 0.0.0-stage → 0.4.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 +21 -0
- package/README.md +484 -2
- package/dist/backups/key-file.d.ts +30 -0
- package/dist/backups/key-file.d.ts.map +1 -0
- package/dist/backups/mongo-backups.d.ts +33 -0
- package/dist/backups/mongo-backups.d.ts.map +1 -0
- package/dist/backups/operations.d.ts +55 -0
- package/dist/backups/operations.d.ts.map +1 -0
- package/dist/backups/options.d.ts +60 -0
- package/dist/backups/options.d.ts.map +1 -0
- package/dist/backups/plan.d.ts +20 -0
- package/dist/backups/plan.d.ts.map +1 -0
- package/dist/chunks/index-j0tptz7w.js +97 -0
- package/dist/chunks/index-j0tptz7w.js.map +11 -0
- package/dist/cli.d.ts +3 -0
- package/dist/cli.d.ts.map +1 -0
- package/dist/cli.js +31 -0
- package/dist/cli.js.map +10 -0
- package/dist/errors.d.ts +43 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/format/bson-stream.d.ts +13 -0
- package/dist/format/bson-stream.d.ts.map +1 -0
- package/dist/format/change.d.ts +61 -0
- package/dist/format/change.d.ts.map +1 -0
- package/dist/format/document.d.ts +7 -0
- package/dist/format/document.d.ts.map +1 -0
- package/dist/format/metadata.d.ts +14 -0
- package/dist/format/metadata.d.ts.map +1 -0
- package/dist/format/names.d.ts +22 -0
- package/dist/format/names.d.ts.map +1 -0
- package/dist/format/position.d.ts +29 -0
- package/dist/format/position.d.ts.map +1 -0
- package/dist/index.d.ts +17 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +1340 -0
- package/dist/index.js.map +32 -0
- package/dist/restore/landing.d.ts +26 -0
- package/dist/restore/landing.d.ts.map +1 -0
- package/dist/restore/options.d.ts +59 -0
- package/dist/restore/options.d.ts.map +1 -0
- package/dist/restore/restore-collections.d.ts +12 -0
- package/dist/restore/restore-collections.d.ts.map +1 -0
- package/dist/source/catalog.d.ts +28 -0
- package/dist/source/catalog.d.ts.map +1 -0
- package/dist/source/changes.d.ts +12 -0
- package/dist/source/changes.d.ts.map +1 -0
- package/dist/source/collections.d.ts +20 -0
- package/dist/source/collections.d.ts.map +1 -0
- package/dist/source/events.d.ts +46 -0
- package/dist/source/events.d.ts.map +1 -0
- package/dist/source/followed.d.ts +13 -0
- package/dist/source/followed.d.ts.map +1 -0
- package/dist/source/mongo-source.d.ts +24 -0
- package/dist/source/mongo-source.d.ts.map +1 -0
- package/dist/source/snapshot.d.ts +17 -0
- package/dist/source/snapshot.d.ts.map +1 -0
- package/dist/target/changes.d.ts +8 -0
- package/dist/target/changes.d.ts.map +1 -0
- package/dist/target/collection.d.ts +20 -0
- package/dist/target/collection.d.ts.map +1 -0
- package/dist/target/mongo-target.d.ts +22 -0
- package/dist/target/mongo-target.d.ts.map +1 -0
- package/docs/README.md +28 -0
- package/docs/guide/errors.md +411 -0
- package/docs/guide/format.md +179 -0
- package/docs/guide/getting-started.md +716 -0
- package/docs/guide/incremental.md +323 -0
- package/docs/guide/operations.md +237 -0
- package/docs/guide/restore.md +684 -0
- package/docs/roadmap.md +61 -0
- package/docs/troubleshooting.md +1257 -0
- package/package.json +70 -5
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Steve Tsala
|
|
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,3 +1,485 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @softistx/mongo-backup
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Encrypted, signed, verifiable backups of a MongoDB database, on **Bun**:
|
|
4
|
+
`mongoBackups` makes a full backup a week and incrementals in between, reads
|
|
5
|
+
each one back, rotates them, and restores a whole database or part of it.
|
|
6
|
+
It is built on `@softistx/backup`,
|
|
7
|
+
and its lower-level source and target stay there for what it does not do.
|
|
8
|
+
|
|
9
|
+
- a **full** backup reads every collection in **one snapshot**, at one
|
|
10
|
+
cluster time, so the collections agree with each other however long the
|
|
11
|
+
read takes — their options and indexes included;
|
|
12
|
+
- an **incremental** backup reads the **change stream** from where the
|
|
13
|
+
backup it builds on stopped: documents written, updated and deleted,
|
|
14
|
+
collections created, modified, renamed and dropped, indexes built and
|
|
15
|
+
dropped;
|
|
16
|
+
- a **restore** lands each collection **whole or not at all**, into the same
|
|
17
|
+
database or another one — or only some collections, under other names, or
|
|
18
|
+
only the documents a query takes;
|
|
19
|
+
- documents are kept as `mongodump` writes them, concatenated BSON, so no
|
|
20
|
+
number changes kind: an `Int32` stays an `Int32`, a `Decimal128` a
|
|
21
|
+
`Decimal128`.
|
|
22
|
+
|
|
23
|
+
```sh
|
|
24
|
+
bunx nxgt-mongo-backup keygen /etc/backup/shop.key # once; keep a copy away from the backups
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
```ts
|
|
28
|
+
import { mongoBackups } from '@softistx/mongo-backup';
|
|
29
|
+
import { MongoClient } from 'mongodb';
|
|
30
|
+
|
|
31
|
+
const client = await MongoClient.connect(process.env.MONGO_URL as string); // a replica set
|
|
32
|
+
const backups = mongoBackups({
|
|
33
|
+
db: client.db('shop'),
|
|
34
|
+
repository: '/mnt/backups', // an absolute folder, or @softistx/backup repositories
|
|
35
|
+
keyFile: '/etc/backup/shop.key',
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
await backups.run(); // hourly: a full backup or an incremental, read back, rotated
|
|
39
|
+
await backups.drill(); // daily: the newest restored apart, counted, dropped
|
|
40
|
+
await backups.restore({ into: client.db('shop-restored') }); // the newest, whole
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
> **0.x.** Full and incremental backups of one database on a schedule, and
|
|
44
|
+
> restores into it or another — whole, or some collections or documents —
|
|
45
|
+
> are here. What comes next — and what was ruled out — is in the
|
|
46
|
+
> [roadmap](docs/roadmap.md).
|
|
47
|
+
|
|
48
|
+
## Install
|
|
49
|
+
|
|
50
|
+
`@softistx/mongo-backup` is public on npm, with no registry setting and no token. It moved from
|
|
51
|
+
`@nxgt/mongo-backup` (npm), which gets no new version: replace the name in your imports
|
|
52
|
+
and `package.json`.
|
|
53
|
+
|
|
54
|
+
```sh
|
|
55
|
+
bun add @softistx/mongo-backup @softistx/backup mongodb
|
|
56
|
+
bun add -d @types/bun typescript
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
- **Bun only**, as `@softistx/backup` is: zstd, hashing, the files a restore
|
|
60
|
+
stages and the `nxgt-mongo-backup` bin are Bun's own.
|
|
61
|
+
- **Required peers:** `@softistx/backup` 0.6, `mongodb` `>=7.0.0 <8` (the
|
|
62
|
+
Node.js driver), `typescript` `^6.0.3 || ^7.0.0`.
|
|
63
|
+
- **`age-encryption` is a dependency**, installed with it: the key file
|
|
64
|
+
holds an age identity.
|
|
65
|
+
- **A replica set.** A snapshot read and a change stream need one; a
|
|
66
|
+
standalone `mongod` cannot serve either. A single-node replica set is
|
|
67
|
+
enough. A sharded cluster serves both too, but is **untested**: the specs
|
|
68
|
+
run against a replica set, and a restore does not shard a collection.
|
|
69
|
+
- **MongoDB 6.1 or later** for incremental backups: they read the change
|
|
70
|
+
stream's expanded events (collections and indexes created, modified,
|
|
71
|
+
dropped) and its `disambiguatedPaths`, which 6.0 and 6.1 added. The specs
|
|
72
|
+
run against MongoDB 8.2.
|
|
73
|
+
- It does not depend on `@softistx/mongo`: it takes the driver's `Db`, so an
|
|
74
|
+
application on the plain driver uses it as is.
|
|
75
|
+
|
|
76
|
+
## Setup
|
|
77
|
+
|
|
78
|
+
```sh
|
|
79
|
+
bunx nxgt-mongo-backup keygen /etc/backup/shop.key
|
|
80
|
+
# key file written: /etc/backup/shop.key
|
|
81
|
+
# recipient: age1…
|
|
82
|
+
# keep a copy of it away from the backups
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
The key file holds the age identity every backup is encrypted for and the
|
|
86
|
+
Ed25519 key every manifest is signed with. It is written with mode `0600`
|
|
87
|
+
and never overwritten; the bin prints only the public recipient.
|
|
88
|
+
**Without it the backups are noise**: keep a copy where the backups are
|
|
89
|
+
not. `generateKeyFile(path)` writes the same file from code —
|
|
90
|
+
[the key file](docs/guide/getting-started.md#the-key-file).
|
|
91
|
+
|
|
92
|
+
## Back up: `run`
|
|
93
|
+
|
|
94
|
+
```ts
|
|
95
|
+
import { mongoBackups } from '@softistx/mongo-backup';
|
|
96
|
+
import { MongoClient } from 'mongodb';
|
|
97
|
+
|
|
98
|
+
const client = await MongoClient.connect(process.env.MONGO_URL as string);
|
|
99
|
+
try {
|
|
100
|
+
const report = await mongoBackups({
|
|
101
|
+
db: client.db('shop'),
|
|
102
|
+
repository: '/mnt/backups',
|
|
103
|
+
keyFile: '/etc/backup/shop.key',
|
|
104
|
+
collections: (name) => name !== 'metrics', // or a list of names; all by default
|
|
105
|
+
}).run();
|
|
106
|
+
console.log(JSON.stringify(report));
|
|
107
|
+
// {"id":"…","kind":"incremental","fellBack":false,"entries":1,"storedSize":2048,"chain":3,"removed":[]}
|
|
108
|
+
} finally {
|
|
109
|
+
await client.close();
|
|
110
|
+
}
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
- **A full backup when none is younger than `fullEvery`** (a week by
|
|
114
|
+
default), an incremental otherwise.
|
|
115
|
+
- **`HISTORY_LOST` falls back to a full backup** at once, with
|
|
116
|
+
`fellBack: true`: the oplog no longer reaches the last backup. Any other
|
|
117
|
+
error is thrown.
|
|
118
|
+
- **The new backup is read back** with the key — decrypted, every digest
|
|
119
|
+
and the signature checked — then the rotation runs, `DEFAULT_KEEP` by
|
|
120
|
+
default, `keep: false` to keep everything.
|
|
121
|
+
- The report holds ids, kinds and sizes: never a key nor a document.
|
|
122
|
+
|
|
123
|
+
## Restore: `restore`
|
|
124
|
+
|
|
125
|
+
```ts
|
|
126
|
+
import { mongoBackups } from '@softistx/mongo-backup';
|
|
127
|
+
import { MongoClient } from 'mongodb';
|
|
128
|
+
|
|
129
|
+
const client = await MongoClient.connect(process.env.MONGO_URL as string);
|
|
130
|
+
const backups = mongoBackups({ db: client.db('shop'), repository: '/mnt/backups', keyFile: '/etc/backup/shop.key' });
|
|
131
|
+
|
|
132
|
+
// the whole database as it was at 9:00, into another one
|
|
133
|
+
await backups.restore({ into: client.db('shop-restored'), at: new Date('2026-10-04T09:00:00Z') });
|
|
134
|
+
|
|
135
|
+
// orders as it was in the newest backup, beside today's
|
|
136
|
+
await backups.restore({ into: client.db('shop'), collections: ['orders'], as: { orders: 'orders-before' } });
|
|
137
|
+
|
|
138
|
+
// one deleted customer, merged back into the live collection
|
|
139
|
+
await backups.restore({
|
|
140
|
+
into: client.db('shop'),
|
|
141
|
+
collections: ['customers'],
|
|
142
|
+
documents: { filter: { _id: 'c-42' }, existing: 'keep' }, // or 'replace'
|
|
143
|
+
});
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
- **`at`** is a backup's id, or a `Date`: the newest backup made at or
|
|
147
|
+
before it. The newest of all by default.
|
|
148
|
+
- **Whole** when none of `collections`, `as` and `documents` is given: each
|
|
149
|
+
collection lands whole or not at all.
|
|
150
|
+
- **Part** otherwise: the whole backup is rebuilt in a scratch database
|
|
151
|
+
first, then what was asked for moves into `into`. The report then has
|
|
152
|
+
`collections`.
|
|
153
|
+
- **A collection already there is refused** with `EXISTS` unless
|
|
154
|
+
`replace: true` — whole, or part by `collections` or `as`. With
|
|
155
|
+
`documents`, `existing` says what happens to those there, and `replace`
|
|
156
|
+
is refused, by the types and at run time.
|
|
157
|
+
|
|
158
|
+
[Restore](docs/guide/restore.md) has the detail of both.
|
|
159
|
+
|
|
160
|
+
## Drill: `drill`
|
|
161
|
+
|
|
162
|
+
```ts
|
|
163
|
+
const drilled = await backups.drill();
|
|
164
|
+
// { id: '…', collections: [{ name: 'customers', documents: 1204 }, { name: 'orders', documents: 98311 }] }
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
The newest backup, chain included, restored into `nxgt-drill-<uuid>` on the
|
|
168
|
+
same client, each collection's documents counted (views apart), and the
|
|
169
|
+
database dropped, failed or not. A backup never restored is a hope —
|
|
170
|
+
[running it in production](docs/guide/operations.md).
|
|
171
|
+
|
|
172
|
+
## Lower level
|
|
173
|
+
|
|
174
|
+
`mongoBackups` is built on three exports a consumer can use directly, with
|
|
175
|
+
`@softistx/backup`'s `bindBackup`: for differential backups, keys kept apart
|
|
176
|
+
rather than in one file, a restore with `only`, or a schedule of your own.
|
|
177
|
+
`mongoBackups(…).binding()` gives the `@softistx/backup` binding and keys it
|
|
178
|
+
uses, so the two levels mix.
|
|
179
|
+
|
|
180
|
+
### Back up with `mongoSource`
|
|
181
|
+
|
|
182
|
+
```ts
|
|
183
|
+
import { bindBackup, defineBackup, localRepository } from '@softistx/backup';
|
|
184
|
+
import { mongoSource } from '@softistx/mongo-backup';
|
|
185
|
+
import { MongoClient } from 'mongodb';
|
|
186
|
+
|
|
187
|
+
const client = await MongoClient.connect(process.env.MONGO_URL as string);
|
|
188
|
+
const backups = bindBackup(defineBackup({ name: 'shop-db' }), {
|
|
189
|
+
repositories: [localRepository({ path: '/mnt/backups' })],
|
|
190
|
+
recipients: [process.env.BACKUP_RECIPIENT as string], // age1… — a public key
|
|
191
|
+
});
|
|
192
|
+
const identities = [process.env.BACKUP_IDENTITY as string]; // AGE-SECRET-KEY-1…
|
|
193
|
+
const source = mongoSource({
|
|
194
|
+
db: client.db('shop'),
|
|
195
|
+
collections: (name) => !name.startsWith('cache.'), // or a list of names; all by default
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
const full = await backups.create(source);
|
|
199
|
+
// { kind: 'full', parent: null, entries: …, … } — every collection at one cluster time
|
|
200
|
+
const next = await backups.create(source, { kind: 'incremental', identities });
|
|
201
|
+
// { kind: 'incremental', parent: full.id, … } — the change stream since `full`
|
|
202
|
+
```
|
|
203
|
+
|
|
204
|
+
A full backup holds, for each collection, `metadata/<name>` — its options
|
|
205
|
+
(validator, collation, capped…) and its indexes — then `documents/<name>`,
|
|
206
|
+
every document as concatenated BSON. A view has its metadata only, its
|
|
207
|
+
pipeline included. A GridFS bucket is two collections like any other,
|
|
208
|
+
`<bucket>.files` and `<bucket>.chunks`. `system.*` is never read. Entries
|
|
209
|
+
come in collection-name order — [format](docs/guide/format.md).
|
|
210
|
+
|
|
211
|
+
**Choosing collections.** `collections` — `mongoSource`'s and
|
|
212
|
+
`mongoBackups`' alike — is a list of names or a test on each name. A name
|
|
213
|
+
in the list that the database lacks rejects the backup with
|
|
214
|
+
`TypeError: mongoSource: a collection named in collections is not in the
|
|
215
|
+
database` (`run on "shop": …` through `mongoBackups`) — a typo would otherwise back up nothing, and say nothing. A test
|
|
216
|
+
picks what it returns `true` for. Either applies to views too, and to the
|
|
217
|
+
changes an incremental records. A change to `collections` takes effect at
|
|
218
|
+
the next full backup —
|
|
219
|
+
[getting started](docs/guide/getting-started.md#choosing-collections).
|
|
220
|
+
|
|
221
|
+
An incremental or differential adds **one entry**, `changes/<n>`, to the
|
|
222
|
+
entries of the backup it builds on, which are not read again. It reads the
|
|
223
|
+
database's change stream (with `showExpandedEvents`) from where that backup
|
|
224
|
+
stopped up to the time it started:
|
|
225
|
+
|
|
226
|
+
- **documents** inserted, replaced, updated and deleted. An update is kept
|
|
227
|
+
as the fields it set, unset and the arrays it cut — or as the whole
|
|
228
|
+
document, when the collection keeps **post-images**
|
|
229
|
+
(`changeStreamPreAndPostImages`). An update to a field whose name holds a
|
|
230
|
+
dot or is a number cannot be said without one, and is `UNSUPPORTED`;
|
|
231
|
+
- **collections** created, modified (`collMod`), renamed and dropped, and
|
|
232
|
+
**indexes** created and dropped, in the collections the chain follows:
|
|
233
|
+
those its full backup held, plus those created since that `collections`
|
|
234
|
+
takes. A rename out of them is kept as a drop; a rename **into** them is
|
|
235
|
+
`UNSUPPORTED`, since an incremental never read the documents it brings;
|
|
236
|
+
- a **dropped database**, which a restore applies as one; the next backup
|
|
237
|
+
resumes after it.
|
|
238
|
+
|
|
239
|
+
The stream resumes from the **oplog**. An incremental whose last backup is
|
|
240
|
+
older than the oplog reaches back fails with `HISTORY_LOST` —
|
|
241
|
+
[incremental](docs/guide/incremental.md).
|
|
242
|
+
|
|
243
|
+
### Restore with `mongoTarget`
|
|
244
|
+
|
|
245
|
+
```ts
|
|
246
|
+
import { bindBackup, defineBackup, localRepository } from '@softistx/backup';
|
|
247
|
+
import { mongoTarget } from '@softistx/mongo-backup';
|
|
248
|
+
import { MongoClient } from 'mongodb';
|
|
249
|
+
|
|
250
|
+
const client = await MongoClient.connect(process.env.MONGO_URL as string);
|
|
251
|
+
const backups = bindBackup(defineBackup({ name: 'shop-db' }), {
|
|
252
|
+
repositories: [localRepository({ path: '/mnt/backups' })],
|
|
253
|
+
recipients: [process.env.BACKUP_RECIPIENT as string],
|
|
254
|
+
});
|
|
255
|
+
|
|
256
|
+
const latest = (await backups.list()).backups.at(-1);
|
|
257
|
+
if (latest) {
|
|
258
|
+
await backups.restore(
|
|
259
|
+
latest.id,
|
|
260
|
+
mongoTarget({
|
|
261
|
+
db: client.db('shop-restored'), // the database backed up, or another
|
|
262
|
+
replace: false, // the default: a collection already there is refused with EXISTS
|
|
263
|
+
tmpDir: '/var/tmp', // where a changes entry is staged; the system's temp folder by default
|
|
264
|
+
}),
|
|
265
|
+
{ identities: [process.env.BACKUP_IDENTITY as string] },
|
|
266
|
+
);
|
|
267
|
+
}
|
|
268
|
+
```
|
|
269
|
+
|
|
270
|
+
- **Each collection lands whole or not at all.** It is built under a
|
|
271
|
+
staging name, `nxgt-restore-<uuid>`, with its options, filled, indexed,
|
|
272
|
+
and renamed into place only once its entry has been checked to its end;
|
|
273
|
+
any failure drops the staging collection.
|
|
274
|
+
- **A collection or view already there is refused** with `EXISTS`, before
|
|
275
|
+
any of its documents is read, and nothing of it is touched — unless
|
|
276
|
+
`replace: true`, which swaps it for the restored one.
|
|
277
|
+
- **Changes are staged, then applied.** A `changes/<n>` entry is written to
|
|
278
|
+
`<tmpDir>/nxgt-mongo-changes-XXXXXX/changes.bson`, in a folder only the
|
|
279
|
+
restoring user can read, until its stream has ended cleanly, then applied
|
|
280
|
+
in order, and the folder removed.
|
|
281
|
+
- **A target serves one restore**: it remembers the metadata it read. Make
|
|
282
|
+
a new `mongoTarget` for each `restore`.
|
|
283
|
+
- **Documents are written with `bypassDocumentValidation`**, so a document
|
|
284
|
+
the validator would refuse today comes back as it was.
|
|
285
|
+
|
|
286
|
+
### Restore part of a backup with `restoreCollections`
|
|
287
|
+
|
|
288
|
+
```ts
|
|
289
|
+
import { bindBackup, defineBackup, localRepository } from '@softistx/backup';
|
|
290
|
+
import { restoreCollections } from '@softistx/mongo-backup';
|
|
291
|
+
import { MongoClient } from 'mongodb';
|
|
292
|
+
|
|
293
|
+
const client = await MongoClient.connect(process.env.MONGO_URL as string);
|
|
294
|
+
const backups = bindBackup(defineBackup({ name: 'shop-db' }), {
|
|
295
|
+
repositories: [localRepository({ path: '/mnt/backups' })],
|
|
296
|
+
recipients: [process.env.BACKUP_RECIPIENT as string],
|
|
297
|
+
});
|
|
298
|
+
const latest = (await backups.list()).backups.at(-1);
|
|
299
|
+
|
|
300
|
+
if (latest) {
|
|
301
|
+
await restoreCollections(backups, latest.id, {
|
|
302
|
+
identities: [process.env.BACKUP_IDENTITY as string],
|
|
303
|
+
db: client.db('shop'),
|
|
304
|
+
collections: ['orders'], // names at the backup's time; all by default
|
|
305
|
+
as: { orders: 'orders-before' }, // or (name) => …; its own name by default
|
|
306
|
+
});
|
|
307
|
+
}
|
|
308
|
+
```
|
|
309
|
+
|
|
310
|
+
- **The whole backup is rebuilt first**, chain included, in a scratch
|
|
311
|
+
database — `nxgt-restore-<uuid>` on `db`'s client, or the empty one given
|
|
312
|
+
as `scratch` — then dropped, failed or not. Getting one document costs a
|
|
313
|
+
full restore, and the user needs the right to create that database.
|
|
314
|
+
- **Whole** (no `documents`): each collection moves into `db` with its
|
|
315
|
+
options and indexes, and views are made again on their collection's new
|
|
316
|
+
name. A name already taken is `EXISTS` before anything moves, unless
|
|
317
|
+
`replace: true`.
|
|
318
|
+
- **Documents**: what `filter` matches is merged on `_id`; `existing` says
|
|
319
|
+
whether a document already there is replaced or kept. Views hold no
|
|
320
|
+
documents and are left out.
|
|
321
|
+
- A name in `collections` the backup lacks is `NOT_FOUND`; a key of an `as`
|
|
322
|
+
map that names nothing restored is a `TypeError` —
|
|
323
|
+
[restoring part of a backup](docs/guide/restore.md#restoring-part-of-a-backup).
|
|
324
|
+
|
|
325
|
+
## API
|
|
326
|
+
|
|
327
|
+
| Export | |
|
|
328
|
+
| --- | --- |
|
|
329
|
+
| `mongoBackups({ db, repository, keyFile, name?, collections?, fullEvery?, keep?, tmpDir? })` | the main way in: `{ run(now?), restore(options), drill(), list(), binding() }` for one database. `repository` is an absolute folder or `@softistx/backup` repositories; `name` is `db.databaseName` by default |
|
|
330
|
+
| `generateKeyFile(path)` | writes a new key file, mode `0600`, never over another; resolves to `{ recipient }`. The bin `nxgt-mongo-backup keygen <path>` calls it |
|
|
331
|
+
| `readKeyFile(path, where?)` | the `BackupKeys` in a key file; refuses with `KEY_FILE` one that is not there, one others than its owner can read or write, or one `keygen` did not write. Its messages start with `readKeyFile:`, or `mongoBackups on "<name>":` through `mongoBackups` |
|
|
332
|
+
| `DEFAULT_FULL_EVERY` | `604_800_000`: a week, in milliseconds |
|
|
333
|
+
| `DEFAULT_KEEP` | `{ last: 24, daily: 14, weekly: 8, monthly: 12 }` |
|
|
334
|
+
| `mongoSource({ db, collections? })` | a `BackupSource` of `kind: 'mongo'` for one database. `collections` is a list of names or a test on each name; every collection and view but `system.*` by default |
|
|
335
|
+
| `mongoTarget({ db, replace?, tmpDir? })` | a `RestoreTarget` for one database, the one backed up or another. `replace` is `false` by default; `tmpDir` must be absolute |
|
|
336
|
+
| `restoreCollections(backups, id, options)` | some collections and views of a backup, chain included, into `db`: whole, or the documents a filter takes. `backups` is anything with `restore`, such as a `bindBackup` binding; resolves to `RestoredCollections` |
|
|
337
|
+
| `MongoBackupError` | what this package throws, with a `code` |
|
|
338
|
+
|
|
339
|
+
| Type | |
|
|
340
|
+
| --- | --- |
|
|
341
|
+
| `MongoBackups`, `MongoBackupsOptions` | what `mongoBackups` returns, and what it takes |
|
|
342
|
+
| `RestoreAtOptions` | what `restore` takes: `{ into, at?, collections?, as?, replace? }`, or `{ into, at?, collections?, as?, documents }` — `replace` and `documents` never together |
|
|
343
|
+
| `RunReport` | `{ id, kind, fellBack, entries, storedSize, chain, removed }` |
|
|
344
|
+
| `RestoreReport` | `Restored`, plus `collections` for a partial restore |
|
|
345
|
+
| `DrillReport` | `{ id, collections: { name, documents }[] }` |
|
|
346
|
+
| `BackupKeys` | `{ identity, recipient, signingKey, publicKey }` |
|
|
347
|
+
| `MongoSourceOptions`, `MongoTargetOptions` | what `mongoSource` and `mongoTarget` take |
|
|
348
|
+
| `RestoreCollectionsOptions`, `DocumentSelection` | what `restoreCollections` takes, and its `documents: { filter, existing }` |
|
|
349
|
+
| `RestoredCollections` | `Restored` plus `collections: { name, as, documents? }[]` |
|
|
350
|
+
| `Restorer` | what `restoreCollections` needs of `backups`: its `restore` |
|
|
351
|
+
| `CollectionFilter` | `readonly string[] \| ((name: string) => boolean)` |
|
|
352
|
+
| `MongoBackupErrorCode` | `'SNAPSHOT_TOO_OLD' \| 'CHANGING' \| 'HISTORY_LOST' \| 'UNSUPPORTED' \| 'EXISTS' \| 'MALFORMED' \| 'NOT_FOUND' \| 'KEY_FILE'` |
|
|
353
|
+
|
|
354
|
+
Repositories, `bindBackup`, `create`, `verify`, `prune` and `KeepPolicy`
|
|
355
|
+
are `@softistx/backup`'s.
|
|
356
|
+
|
|
357
|
+
## Errors
|
|
358
|
+
|
|
359
|
+
`MongoBackupError` has a `code`, and a message that says what went wrong
|
|
360
|
+
**without quoting a document, a collection name, a value or a key**: a
|
|
361
|
+
backup error ends up in logs. The server's error, when there is one, is its
|
|
362
|
+
`cause`.
|
|
363
|
+
|
|
364
|
+
**Through `mongoBackups`, the prefix names the call.** The `mongoSource:`,
|
|
365
|
+
`mongoTarget:` and `restoreCollections:` messages below are quoted as the
|
|
366
|
+
lower-level functions throw them. `run`, `restore` and `drill` replace that
|
|
367
|
+
prefix with `run on "shop":`, `restore on "shop":` or `drill on "shop":` —
|
|
368
|
+
the backup's name — and keep the rest, the class and the `code`; a
|
|
369
|
+
`mongoSource:` or `mongoTarget:` one keeps the original error as its
|
|
370
|
+
`cause` (and `restoreCollections`, called directly, renames the
|
|
371
|
+
`mongoTarget:` ones of its scratch restore the same way):
|
|
372
|
+
`restore on "shop": a collection or view the backup holds is already in the database; …`.
|
|
373
|
+
|
|
374
|
+
| `code` | When | |
|
|
375
|
+
| --- | --- | --- |
|
|
376
|
+
| `KEY_FILE` | there is no file at `keyFile`; the `ENOENT` is its `cause`, and the file is looked for again on the next call | `mongoBackups on "shop": there is no key file there; write one with nxgt-mongo-backup keygen` |
|
|
377
|
+
| `KEY_FILE` | its group or others can read or write the key file | `mongoBackups on "shop": others than its owner can read or write the key file; chmod 600 it` |
|
|
378
|
+
| `KEY_FILE` | the key file is not one `keygen` wrote | `mongoBackups on "shop": the key file is not one keygen wrote` |
|
|
379
|
+
| `NOT_FOUND` | `restore` or `drill` with no backup in the repository; `shop` is the backup's name | `restore on "shop": the repository holds no backup yet`, `drill on "shop": the repository holds no backup yet` |
|
|
380
|
+
| `NOT_FOUND` | `restore` with an `at` older than every backup | `restore on "shop": no backup was made at or before that time` |
|
|
381
|
+
| `NOT_FOUND` | a name in `collections` is not in the backup, for a partial restore — `restoreCollections:` when it is called directly | `restore on "shop": a collection named in collections is not in the backup` |
|
|
382
|
+
| `SNAPSHOT_TOO_OLD` | a full backup took longer than the server keeps snapshot history | `mongoSource: the snapshot outlived the history the server keeps; raise minSnapshotHistoryWindowInSeconds, or back up fewer collections at a time` |
|
|
383
|
+
| `CHANGING` | a full backup found the collections, their options or their indexes different before and after pinning its snapshot, five times running | `mongoSource: the collections or their indexes kept changing while the snapshot was taken; try again when they settle` |
|
|
384
|
+
| `HISTORY_LOST` | the oplog no longer reaches back to where the last backup stopped — `run` falls back to a full backup instead | `mongoSource: the change stream cannot resume where the last backup stopped: the oplog no longer reaches back there; make a full backup` |
|
|
385
|
+
| `UNSUPPORTED` | a time-series collection; an update to a dotted or numeric field name without post-images; a rename into the collections the chain follows; more collections created, renamed or dropped since the full backup than a position records; a server that is not a replica set or a sharded cluster | `mongoSource: a collection was renamed into those backed up, with documents an incremental backup never read; make a full backup` |
|
|
386
|
+
| `EXISTS` | a collection or view the restore would write is already there, without `replace` | `mongoTarget: a collection or view the backup holds is already in the database; restore into another one, or pass replace: true` |
|
|
387
|
+
| `EXISTS` | a partial restore, whole, without `replace`: a name it would land under is taken — `restore on "shop":` rather than `restoreCollections:` through `restore` | `restoreCollections: a collection or view to restore is already in the database; restore it under another name, or pass replace: true` |
|
|
388
|
+
| `MALFORMED` | an entry, a change or a recorded position is not one this version wrote | `mongoTarget: a metadata entry is not one this version wrote` |
|
|
389
|
+
|
|
390
|
+
Wiring is a bare `TypeError`, thrown by `mongoBackups` itself:
|
|
391
|
+
`mongoBackups: db must be a MongoDB Db`,
|
|
392
|
+
`mongoBackups: keyFile must be an absolute path`,
|
|
393
|
+
`mongoBackups: repository must be an absolute folder, or repositories`,
|
|
394
|
+
`mongoBackups: keep must be false, or name rules each a whole number, 1 or more`,
|
|
395
|
+
`mongoBackups: fullEvery must be a whole number of milliseconds, an hour or more`,
|
|
396
|
+
`mongoBackups: repository must list repositories, each under a name of its own`,
|
|
397
|
+
`mongoBackups: tmpDir must be an absolute path`,
|
|
398
|
+
`mongoBackups: name must be 1 to 100 characters, …` and
|
|
399
|
+
`mongoBackups: the database's name cannot name a backup; give name: 1 to 100 characters, …`;
|
|
400
|
+
by `run` and `restore`, before anything is read, naming the call and the
|
|
401
|
+
backup: `run on "shop": now must be a valid Date`,
|
|
402
|
+
`restore on "shop": into must be a MongoDB Db`,
|
|
403
|
+
`restore on "shop": at must be a backup's id or a valid Date`,
|
|
404
|
+
`restore on "shop": replace is for whole collections; documents says what happens to those there`
|
|
405
|
+
— and by the lower-level exports. A partial `restore` refuses as
|
|
406
|
+
`restoreCollections` does, but with `restore on "shop":` where it says
|
|
407
|
+
`restoreCollections:` — `restore on "shop": as names a collection not restored`.
|
|
408
|
+
All are listed in
|
|
409
|
+
[errors](docs/guide/errors.md#the-bare-typeerrors). Every `@softistx/backup` error passes through as it is, and so does an
|
|
410
|
+
error from the driver — an `E11000` duplicate key during a restore, a
|
|
411
|
+
server error — **whose message may quote collection names and values**:
|
|
412
|
+
only `MongoBackupError` and the bare `TypeError`s quote none. Every message
|
|
413
|
+
is in [troubleshooting](docs/troubleshooting.md); a handler is in
|
|
414
|
+
[errors](docs/guide/errors.md).
|
|
415
|
+
|
|
416
|
+
## Traps
|
|
417
|
+
|
|
418
|
+
- **A lost key file is lost backups**: nothing reads them without its
|
|
419
|
+
identity. Keep a copy away from the repository, and never commit it.
|
|
420
|
+
- **`name` is the database's name by default**, and a backup name is
|
|
421
|
+
lowercase letters, digits, `.`, `_` and `-`: `client.db('MyShop')` throws
|
|
422
|
+
`mongoBackups: the database's name cannot name a backup; …` — pass
|
|
423
|
+
`name: 'myshop'`.
|
|
424
|
+
- **A full backup must finish inside the server's snapshot window** —
|
|
425
|
+
`minSnapshotHistoryWindowInSeconds`, 300 seconds by default — or it fails
|
|
426
|
+
`SNAPSHOT_TOO_OLD`. Raise it for a large database:
|
|
427
|
+
`db.adminCommand({ setParameter: 1, minSnapshotHistoryWindowInSeconds: 3600 })` —
|
|
428
|
+
[a long snapshot](docs/guide/getting-started.md#a-long-snapshot).
|
|
429
|
+
- **Time-series collections are not backed up**: a backup that meets one
|
|
430
|
+
fails `UNSUPPORTED`. Leave them out:
|
|
431
|
+
`collections: (name) => name !== 'metrics'`.
|
|
432
|
+
- **The oplog must outlast the interval between runs**, or every run is a
|
|
433
|
+
full backup (`fellBack: true`). Check the window with
|
|
434
|
+
`rs.printReplicationInfo()` —
|
|
435
|
+
[the oplog window](docs/guide/incremental.md#the-oplog-window).
|
|
436
|
+
- **An incremental that fails `UNSUPPORTED` fails again on the next `run`**:
|
|
437
|
+
the change it stopped on is still there. Make a full backup:
|
|
438
|
+
`await backups.run(new Date(Date.now() + DEFAULT_FULL_EVERY))` —
|
|
439
|
+
[forcing a full backup](docs/guide/operations.md#forcing-a-full-backup).
|
|
440
|
+
- **A change to `collections` waits for the next full backup** — the same
|
|
441
|
+
way out.
|
|
442
|
+
- **After restoring into the database you back up, make a full backup**:
|
|
443
|
+
the restore's own renames look like renames into the followed
|
|
444
|
+
collections, and the next incremental fails `UNSUPPORTED` —
|
|
445
|
+
[restore](docs/guide/restore.md#restoring-into-the-database-you-back-up).
|
|
446
|
+
- **`drill` and a partial `restore` rebuild the whole backup on your
|
|
447
|
+
cluster**, in a database of their own: the user needs the right to create
|
|
448
|
+
it, and the server the room for it.
|
|
449
|
+
- **A restore is whole per collection, not per backup**: a failure keeps
|
|
450
|
+
the collections already restored. Restore into a fresh database, then
|
|
451
|
+
point the application at it —
|
|
452
|
+
[restore](docs/guide/restore.md#what-a-failure-leaves).
|
|
453
|
+
- **A recorded `dropDatabase` drops the database restored into**, whatever
|
|
454
|
+
else it holds. Restore into a database of its own.
|
|
455
|
+
|
|
456
|
+
## Documentation
|
|
457
|
+
|
|
458
|
+
- [docs/README.md](docs/README.md) — the guide index.
|
|
459
|
+
- [docs/guide/getting-started.md](docs/guide/getting-started.md) — the key
|
|
460
|
+
file, `mongoBackups` and its options, `run`, `restore`, `drill`, `list`
|
|
461
|
+
and `binding`; then `mongoSource`, choosing collections, the snapshot and
|
|
462
|
+
its window.
|
|
463
|
+
- [docs/guide/operations.md](docs/guide/operations.md) — running it in
|
|
464
|
+
production: the key file on the job host, the schedule, the oplog and
|
|
465
|
+
snapshot windows, alerting, drills, forcing a full backup, and the
|
|
466
|
+
`restore` calls for the day you need a backup back.
|
|
467
|
+
- [docs/guide/incremental.md](docs/guide/incremental.md) — what the change
|
|
468
|
+
stream records, post-images and dotted fields, the collections a chain
|
|
469
|
+
follows, renames, a dropped database, and the oplog window.
|
|
470
|
+
- [docs/guide/restore.md](docs/guide/restore.md) — `mongoTarget` and
|
|
471
|
+
`restoreCollections`: the order, staging, `EXISTS` and `replace`, changes
|
|
472
|
+
and renames, another database, what a failure leaves, and restoring part
|
|
473
|
+
of a backup.
|
|
474
|
+
- [docs/guide/format.md](docs/guide/format.md) — the entries, their names
|
|
475
|
+
and bytes, the position, and reading a backup with `bsondump`.
|
|
476
|
+
- [docs/guide/errors.md](docs/guide/errors.md) — `MongoBackupError`, every
|
|
477
|
+
code and message, the `TypeError`s, and a handler for a scheduled job.
|
|
478
|
+
- [docs/troubleshooting.md](docs/troubleshooting.md) — every error, by the
|
|
479
|
+
message you will see.
|
|
480
|
+
- [docs/roadmap.md](docs/roadmap.md) — what is coming, and what has been
|
|
481
|
+
ruled out.
|
|
482
|
+
|
|
483
|
+
## License
|
|
484
|
+
|
|
485
|
+
MIT
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/** The keys a backup job needs, all read from one file. */
|
|
2
|
+
export interface BackupKeys {
|
|
3
|
+
/** The age secret key: to read backups, an incremental's base included. */
|
|
4
|
+
identity: string;
|
|
5
|
+
/** Its public half, `age1…`: every backup is encrypted to it. */
|
|
6
|
+
recipient: string;
|
|
7
|
+
/** The Ed25519 private key, PEM: every manifest is signed with it. */
|
|
8
|
+
signingKey: string;
|
|
9
|
+
/** Its public half, PEM: a manifest must be signed by it to be read. */
|
|
10
|
+
publicKey: string;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Writes a new key file at `path`, readable by its owner alone, synced with
|
|
14
|
+
* its folder, and gives its recipient — the public half, safe to print. A
|
|
15
|
+
* file already there is never overwritten: the backups made with it would
|
|
16
|
+
* be lost with it. A write that fails removes the file it created.
|
|
17
|
+
*/
|
|
18
|
+
export declare function generateKeyFile(path: string): Promise<{
|
|
19
|
+
recipient: string;
|
|
20
|
+
}>;
|
|
21
|
+
/**
|
|
22
|
+
* The keys in the file at `path`, checked and read through one descriptor.
|
|
23
|
+
* `KEY_FILE` when it is not there (the system's error as its `cause`), when
|
|
24
|
+
* others than its owner can read it — as ssh refuses such a key — and when
|
|
25
|
+
* it is not one `generateKeyFile` wrote; any other failure to open it is the
|
|
26
|
+
* system's, as it is. No message quotes the file's content. `where` names
|
|
27
|
+
* the call in messages.
|
|
28
|
+
*/
|
|
29
|
+
export declare function readKeyFile(path: string, where?: string): Promise<BackupKeys>;
|
|
30
|
+
//# sourceMappingURL=key-file.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"key-file.d.ts","sourceRoot":"","sources":["../../src/backups/key-file.ts"],"names":[],"mappings":"AAOA,2DAA2D;AAC3D,MAAM,WAAW,UAAU;IAC1B,2EAA2E;IAC3E,QAAQ,EAAE,MAAM,CAAC;IACjB,iEAAiE;IACjE,SAAS,EAAE,MAAM,CAAC;IAClB,sEAAsE;IACtE,UAAU,EAAE,MAAM,CAAC;IACnB,wEAAwE;IACxE,SAAS,EAAE,MAAM,CAAC;CAClB;AAiBD;;;;;GAKG;AACH,wBAAsB,eAAe,CACpC,IAAI,EAAE,MAAM,GACV,OAAO,CAAC;IAAE,SAAS,EAAE,MAAM,CAAA;CAAE,CAAC,CAgBhC;AAED;;;;;;;GAOG;AACH,wBAAsB,WAAW,CAChC,IAAI,EAAE,MAAM,EACZ,KAAK,SAAgB,GACnB,OAAO,CAAC,UAAU,CAAC,CAsBrB"}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { type BackupInfo, type BoundBackup } from '@softistx/backup';
|
|
2
|
+
import type { BackupKeys } from './key-file.js';
|
|
3
|
+
import { type RestoreReport, type RunReport } from './operations.js';
|
|
4
|
+
import { type MongoBackupsOptions, type RestoreAtOptions } from './options.js';
|
|
5
|
+
import type { DrillReport } from './plan.js';
|
|
6
|
+
export type { RestoreReport, RunReport } from './operations.js';
|
|
7
|
+
export interface MongoBackups {
|
|
8
|
+
/**
|
|
9
|
+
* One scheduled run. `now` decides only full or incremental; the backup
|
|
10
|
+
* and its rotation go by the machine's clock.
|
|
11
|
+
*/
|
|
12
|
+
run(now?: Date): Promise<RunReport>;
|
|
13
|
+
/** A backup, chain included, into `into`: whole, or the part the options name. */
|
|
14
|
+
restore(options: RestoreAtOptions): Promise<RestoreReport>;
|
|
15
|
+
/** The newest backup restored into a database of its own, counted, then dropped. */
|
|
16
|
+
drill(): Promise<DrillReport>;
|
|
17
|
+
/** The backups the repository holds, oldest first. Reads the key file, for the key manifests are signed with. */
|
|
18
|
+
list(): Promise<BackupInfo[]>;
|
|
19
|
+
/** The `@softistx/backup` binding underneath, and the keys: for what this does not do. */
|
|
20
|
+
binding(): Promise<{
|
|
21
|
+
backups: BoundBackup;
|
|
22
|
+
keys: BackupKeys;
|
|
23
|
+
}>;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* MongoDB backups with the decisions made: one key file, a full backup a
|
|
27
|
+
* week and incrementals in between, every backup read back with the key,
|
|
28
|
+
* a rotation after each run, and one `restore` for a whole database or a
|
|
29
|
+
* part of it. Built on `mongoSource`, `mongoTarget` and
|
|
30
|
+
* `restoreCollections`, which stay there for what this does not do.
|
|
31
|
+
*/
|
|
32
|
+
export declare function mongoBackups(options: MongoBackupsOptions): MongoBackups;
|
|
33
|
+
//# sourceMappingURL=mongo-backups.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mongo-backups.d.ts","sourceRoot":"","sources":["../../src/backups/mongo-backups.ts"],"names":[],"mappings":"AAAA,OAAO,EAEN,KAAK,UAAU,EACf,KAAK,WAAW,EAIhB,MAAM,kBAAkB,CAAC;AAC1B,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAC7C,OAAO,EAMN,KAAK,aAAa,EAClB,KAAK,SAAS,EAGd,MAAM,cAAc,CAAC;AACtB,OAAO,EAEN,KAAK,mBAAmB,EACxB,KAAK,gBAAgB,EACrB,MAAM,WAAW,CAAC;AACnB,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,QAAQ,CAAC;AAE1C,YAAY,EAAE,aAAa,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAE7D,MAAM,WAAW,YAAY;IAC5B;;;OAGG;IACH,GAAG,CAAC,GAAG,CAAC,EAAE,IAAI,GAAG,OAAO,CAAC,SAAS,CAAC,CAAC;IACpC,kFAAkF;IAClF,OAAO,CAAC,OAAO,EAAE,gBAAgB,GAAG,OAAO,CAAC,aAAa,CAAC,CAAC;IAC3D,oFAAoF;IACpF,KAAK,IAAI,OAAO,CAAC,WAAW,CAAC,CAAC;IAC9B,iHAAiH;IACjH,IAAI,IAAI,OAAO,CAAC,UAAU,EAAE,CAAC,CAAC;IAC9B,0FAA0F;IAC1F,OAAO,IAAI,OAAO,CAAC;QAAE,OAAO,EAAE,WAAW,CAAC;QAAC,IAAI,EAAE,UAAU,CAAA;KAAE,CAAC,CAAC;CAC/D;AA+BD;;;;;;GAMG;AACH,wBAAgB,YAAY,CAAC,OAAO,EAAE,mBAAmB,GAAG,YAAY,CAcvE"}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { type BackupDefinition, type BackupInfo, type BoundBackup, type Repository, type Restored } from '@softistx/backup';
|
|
2
|
+
import type { RestoredCollections } from '../restore/options.js';
|
|
3
|
+
import { type BackupKeys } from './key-file.js';
|
|
4
|
+
import { type MongoBackupsOptions, type RestoreAtOptions } from './options.js';
|
|
5
|
+
import { type DrillReport } from './plan.js';
|
|
6
|
+
/** What one `run` did, for the logs: ids and sizes, never a key nor a document. */
|
|
7
|
+
export interface RunReport {
|
|
8
|
+
id: string;
|
|
9
|
+
kind: 'full' | 'incremental';
|
|
10
|
+
/** `true` when an incremental was due, but the oplog no longer reached the last backup. */
|
|
11
|
+
fellBack: boolean;
|
|
12
|
+
entries: number;
|
|
13
|
+
storedSize: number;
|
|
14
|
+
/** How many backups a restore of this one reads: itself and those it builds on. */
|
|
15
|
+
chain: number;
|
|
16
|
+
/** The backups the rotation removed. */
|
|
17
|
+
removed: string[];
|
|
18
|
+
}
|
|
19
|
+
/** What `restore` wrote: `collections` only when part of a backup was asked for. */
|
|
20
|
+
export type RestoreReport = Restored & {
|
|
21
|
+
collections?: RestoredCollections['collections'];
|
|
22
|
+
};
|
|
23
|
+
export interface Bound {
|
|
24
|
+
backups: BoundBackup;
|
|
25
|
+
keys: BackupKeys;
|
|
26
|
+
}
|
|
27
|
+
/** What each operation reads: data only, the binding once the key file is read. */
|
|
28
|
+
export interface Context {
|
|
29
|
+
options: MongoBackupsOptions;
|
|
30
|
+
definition: BackupDefinition;
|
|
31
|
+
repositories: readonly [Repository, ...Repository[]];
|
|
32
|
+
/** The key file read and the backup bound, once a call needed them. */
|
|
33
|
+
ready?: Promise<Bound> | undefined;
|
|
34
|
+
}
|
|
35
|
+
/** `p`'s lower-level refusals told as `<call> on "<backup>"`. */
|
|
36
|
+
export declare function asCalled<T>(p: Promise<T>, ctx: Context, call: string): Promise<T>;
|
|
37
|
+
/** The binding, the key file read on first use, and again after a failed read. */
|
|
38
|
+
export declare function bindingOf(ctx: Context): Promise<Bound>;
|
|
39
|
+
/** The backups the repository holds that read with the key, oldest first. */
|
|
40
|
+
export declare function listOf(ctx: Context): Promise<BackupInfo[]>;
|
|
41
|
+
/**
|
|
42
|
+
* One scheduled run: a full backup when none is younger than `fullEvery`,
|
|
43
|
+
* an incremental otherwise — a full one after all when the oplog no longer
|
|
44
|
+
* reaches the last backup, since every incremental would fail alike until
|
|
45
|
+
* one — read back with the key, then the rotation.
|
|
46
|
+
*/
|
|
47
|
+
export declare function runOnce(ctx: Context, now?: Date): Promise<RunReport>;
|
|
48
|
+
/**
|
|
49
|
+
* The backup `at.at` names, into `at.into`: whole through `mongoTarget`,
|
|
50
|
+
* or the part the options name through `restoreCollections`.
|
|
51
|
+
*/
|
|
52
|
+
export declare function restoreAt(ctx: Context, at: RestoreAtOptions): Promise<RestoreReport>;
|
|
53
|
+
/** The newest backup, chain included, into a database of its own, counted, dropped. */
|
|
54
|
+
export declare function drillNewest(ctx: Context): Promise<DrillReport>;
|
|
55
|
+
//# sourceMappingURL=operations.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"operations.d.ts","sourceRoot":"","sources":["../../src/backups/operations.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,KAAK,gBAAgB,EACrB,KAAK,UAAU,EACf,KAAK,WAAW,EAEhB,KAAK,UAAU,EACf,KAAK,QAAQ,EACb,MAAM,kBAAkB,CAAC;AAE1B,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,oBAAoB,CAAC;AAI9D,OAAO,EAAE,KAAK,UAAU,EAAe,MAAM,YAAY,CAAC;AAC1D,OAAO,EAIN,KAAK,mBAAmB,EACxB,KAAK,gBAAgB,EACrB,MAAM,WAAW,CAAC;AACnB,OAAO,EAA0B,KAAK,WAAW,EAAW,MAAM,QAAQ,CAAC;AAE3E,mFAAmF;AACnF,MAAM,WAAW,SAAS;IACzB,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,EAAE,MAAM,GAAG,aAAa,CAAC;IAC7B,2FAA2F;IAC3F,QAAQ,EAAE,OAAO,CAAC;IAClB,OAAO,EAAE,MAAM,CAAC;IAChB,UAAU,EAAE,MAAM,CAAC;IACnB,mFAAmF;IACnF,KAAK,EAAE,MAAM,CAAC;IACd,wCAAwC;IACxC,OAAO,EAAE,MAAM,EAAE,CAAC;CAClB;AAED,oFAAoF;AACpF,MAAM,MAAM,aAAa,GAAG,QAAQ,GAAG;IACtC,WAAW,CAAC,EAAE,mBAAmB,CAAC,aAAa,CAAC,CAAC;CACjD,CAAC;AAEF,MAAM,WAAW,KAAK;IACrB,OAAO,EAAE,WAAW,CAAC;IACrB,IAAI,EAAE,UAAU,CAAC;CACjB;AAED,mFAAmF;AACnF,MAAM,WAAW,OAAO;IACvB,OAAO,EAAE,mBAAmB,CAAC;IAC7B,UAAU,EAAE,gBAAgB,CAAC;IAC7B,YAAY,EAAE,SAAS,CAAC,UAAU,EAAE,GAAG,UAAU,EAAE,CAAC,CAAC;IACrD,uEAAuE;IACvE,KAAK,CAAC,EAAE,OAAO,CAAC,KAAK,CAAC,GAAG,SAAS,CAAC;CACnC;AAMD,iEAAiE;AACjE,wBAAgB,QAAQ,CAAC,CAAC,EACzB,CAAC,EAAE,OAAO,CAAC,CAAC,CAAC,EACb,GAAG,EAAE,OAAO,EACZ,IAAI,EAAE,MAAM,GACV,OAAO,CAAC,CAAC,CAAC,CAEZ;AAED,kFAAkF;AAClF,wBAAgB,SAAS,CAAC,GAAG,EAAE,OAAO,GAAG,OAAO,CAAC,KAAK,CAAC,CAqBtD;AAED,6EAA6E;AAC7E,wBAAsB,MAAM,CAAC,GAAG,EAAE,OAAO,GAAG,OAAO,CAAC,UAAU,EAAE,CAAC,CAEhE;AAED;;;;;GAKG;AACH,wBAAsB,OAAO,CAC5B,GAAG,EAAE,OAAO,EACZ,GAAG,OAAa,GACd,OAAO,CAAC,SAAS,CAAC,CAwCpB;AAED;;;GAGG;AACH,wBAAsB,SAAS,CAC9B,GAAG,EAAE,OAAO,EACZ,EAAE,EAAE,gBAAgB,GAClB,OAAO,CAAC,aAAa,CAAC,CAiCxB;AAED,uFAAuF;AACvF,wBAAsB,WAAW,CAAC,GAAG,EAAE,OAAO,GAAG,OAAO,CAAC,WAAW,CAAC,CAapE"}
|