@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
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mongo-source.d.ts","sourceRoot":"","sources":["../../src/source/mongo-source.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAsB,MAAM,kBAAkB,CAAC;AACzE,OAAO,KAAK,EAAE,EAAE,EAAE,MAAM,SAAS,CAAC;AAKlC,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,eAAe,CAAC;AAItD,MAAM,WAAW,kBAAkB;IAClC,yFAAyF;IACzF,EAAE,EAAE,EAAE,CAAC;IACP;;;;OAIG;IACH,WAAW,CAAC,EAAE,gBAAgB,GAAG,SAAS,CAAC;CAC3C;AAuED;;;;;;;;GAQG;AACH,wBAAgB,WAAW,CAAC,OAAO,EAAE,kBAAkB,GAAG,YAAY,CAsBrE"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { SourceEntry } from '@softistx/backup';
|
|
2
|
+
import type { Db, Timestamp } from 'mongodb';
|
|
3
|
+
import type { CollectionFilter } from './collections.js';
|
|
4
|
+
/**
|
|
5
|
+
* The cluster's operation time now, read through a session of its own:
|
|
6
|
+
* what a change stream started there misses nothing after.
|
|
7
|
+
*/
|
|
8
|
+
export declare function operationTime(db: Db): Promise<Timestamp>;
|
|
9
|
+
/**
|
|
10
|
+
* A full backup's entries: each collection's metadata, then its documents,
|
|
11
|
+
* every document read in one snapshot session — one cluster time for them
|
|
12
|
+
* all — and the metadata read so that it is that time's too. The next
|
|
13
|
+
* backup's change stream starts just after that time, following the
|
|
14
|
+
* collections this one holds — its metadata entries say which.
|
|
15
|
+
*/
|
|
16
|
+
export declare function snapshotEntries(db: Db, filter: CollectionFilter | undefined, record: (start: Timestamp) => void): AsyncGenerator<SourceEntry>;
|
|
17
|
+
//# sourceMappingURL=snapshot.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"snapshot.d.ts","sourceRoot":"","sources":["../../src/source/snapshot.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AACpD,OAAO,KAAK,EAAiB,EAAE,EAAE,SAAS,EAAE,MAAM,SAAS,CAAC;AAK5D,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,eAAe,CAAC;AAKtD;;;GAGG;AACH,wBAAsB,aAAa,CAAC,EAAE,EAAE,EAAE,GAAG,OAAO,CAAC,SAAS,CAAC,CAgB9D;AA6CD;;;;;;GAMG;AACH,wBAAuB,eAAe,CACrC,EAAE,EAAE,EAAE,EACN,MAAM,EAAE,gBAAgB,GAAG,SAAS,EACpC,MAAM,EAAE,CAAC,KAAK,EAAE,SAAS,KAAK,IAAI,GAChC,cAAc,CAAC,WAAW,CAAC,CAuB7B"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { Db } from 'mongodb';
|
|
2
|
+
/**
|
|
3
|
+
* Takes a `changes/<n>` entry: staged to a file in `tmpDir` until its
|
|
4
|
+
* stream has ended cleanly — a change applied is not taken back, so none
|
|
5
|
+
* is applied before the backup's checks have passed — then applied.
|
|
6
|
+
*/
|
|
7
|
+
export declare function restoreChanges(db: Db, stream: ReadableStream<Uint8Array>, tmpDir: string, replace: boolean): Promise<void>;
|
|
8
|
+
//# sourceMappingURL=changes.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"changes.d.ts","sourceRoot":"","sources":["../../src/target/changes.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAEX,EAAE,EAGF,MAAM,SAAS,CAAC;AAmKjB;;;;GAIG;AACH,wBAAsB,cAAc,CACnC,EAAE,EAAE,EAAE,EACN,MAAM,EAAE,cAAc,CAAC,UAAU,CAAC,EAClC,MAAM,EAAE,MAAM,EACd,OAAO,EAAE,OAAO,GACd,OAAO,CAAC,IAAI,CAAC,CAWf"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { type Db } from 'mongodb';
|
|
2
|
+
import { MongoBackupError } from '../errors.js';
|
|
3
|
+
import type { Metadata } from '../format/metadata.js';
|
|
4
|
+
export declare function exists(db: Db, name: string): Promise<boolean>;
|
|
5
|
+
/** Whether `name` is a view in `db`. */
|
|
6
|
+
export declare function isView(db: Db, name: string): Promise<boolean>;
|
|
7
|
+
export declare function taken(): MongoBackupError;
|
|
8
|
+
export declare function dropIfThere(db: Db, name: string): Promise<void>;
|
|
9
|
+
/** A view, created from its metadata: in place of one there, with `replace`. */
|
|
10
|
+
export declare function restoreView(db: Db, name: string, metadata: Metadata, replace: boolean): Promise<void>;
|
|
11
|
+
/**
|
|
12
|
+
* One collection, landed whole or not at all: created apart under a name
|
|
13
|
+
* of its own with the options it had, filled, indexed, and renamed into
|
|
14
|
+
* place only once its stream ended cleanly — the backup's checks run at
|
|
15
|
+
* the end. Anything that fails on the way drops what was made apart.
|
|
16
|
+
* Without `replace`, a collection already there is refused before any
|
|
17
|
+
* document is read, and the rename itself refuses one made meanwhile.
|
|
18
|
+
*/
|
|
19
|
+
export declare function restoreCollection(db: Db, name: string, metadata: Metadata, stream: ReadableStream<Uint8Array>, replace: boolean): Promise<void>;
|
|
20
|
+
//# sourceMappingURL=collection.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"collection.d.ts","sourceRoot":"","sources":["../../src/target/collection.ts"],"names":[],"mappings":"AAAA,OAAO,EAAQ,KAAK,EAAE,EAAwC,MAAM,SAAS,CAAC;AAC9E,OAAO,EAAE,gBAAgB,EAAc,MAAM,WAAW,CAAC;AAGzD,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,oBAAoB,CAAC;AAUnD,wBAAgB,MAAM,CAAC,EAAE,EAAE,EAAE,EAAE,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,CAK7D;AAED,wCAAwC;AACxC,wBAAsB,MAAM,CAAC,EAAE,EAAE,EAAE,EAAE,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,CAKnE;AAED,wBAAgB,KAAK,IAAI,gBAAgB,CAMxC;AAED,wBAAsB,WAAW,CAAC,EAAE,EAAE,EAAE,EAAE,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAIrE;AAED,gFAAgF;AAChF,wBAAsB,WAAW,CAChC,EAAE,EAAE,EAAE,EACN,IAAI,EAAE,MAAM,EACZ,QAAQ,EAAE,QAAQ,EAClB,OAAO,EAAE,OAAO,GACd,OAAO,CAAC,IAAI,CAAC,CAMf;AA2BD;;;;;;;GAOG;AACH,wBAAsB,iBAAiB,CACtC,EAAE,EAAE,EAAE,EACN,IAAI,EAAE,MAAM,EACZ,QAAQ,EAAE,QAAQ,EAClB,MAAM,EAAE,cAAc,CAAC,UAAU,CAAC,EAClC,OAAO,EAAE,OAAO,GACd,OAAO,CAAC,IAAI,CAAC,CA2Bf"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { RestoreTarget } from '@softistx/backup';
|
|
2
|
+
import type { Db } from 'mongodb';
|
|
3
|
+
export interface MongoTargetOptions {
|
|
4
|
+
/** The database to restore into: the one backed up, or another. */
|
|
5
|
+
db: Db;
|
|
6
|
+
/**
|
|
7
|
+
* Replace a collection or view already there. `false` by default: one
|
|
8
|
+
* there is refused with `EXISTS`, and nothing of it is touched.
|
|
9
|
+
*/
|
|
10
|
+
replace?: boolean | undefined;
|
|
11
|
+
/** Where a `changes/<n>` entry is staged before it applies. The system's temporary folder by default. */
|
|
12
|
+
tmpDir?: string | undefined;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* A MongoDB database as a restore target. Takes the entries a
|
|
16
|
+
* `mongoSource` backup holds, in their order: each collection's metadata,
|
|
17
|
+
* then its documents — landed whole or not at all — then the changes an
|
|
18
|
+
* incremental backup recorded, applied once each entry was read and
|
|
19
|
+
* checked to its end. A view is created from its metadata.
|
|
20
|
+
*/
|
|
21
|
+
export declare function mongoTarget(options: MongoTargetOptions): RestoreTarget;
|
|
22
|
+
//# sourceMappingURL=mongo-target.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mongo-target.d.ts","sourceRoot":"","sources":["../../src/target/mongo-target.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AACtD,OAAO,KAAK,EAAE,EAAE,EAAE,MAAM,SAAS,CAAC;AAOlC,MAAM,WAAW,kBAAkB;IAClC,mEAAmE;IACnE,EAAE,EAAE,EAAE,CAAC;IACP;;;OAGG;IACH,OAAO,CAAC,EAAE,OAAO,GAAG,SAAS,CAAC;IAC9B,yGAAyG;IACzG,MAAM,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;CAC5B;AA0CD;;;;;;GAMG;AACH,wBAAgB,WAAW,CAAC,OAAO,EAAE,kBAAkB,GAAG,aAAa,CAgCtE"}
|
package/docs/README.md
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
# `@softistx/mongo-backup` documentation
|
|
2
|
+
|
|
3
|
+
Encrypted, signed backups of a MongoDB database on `@softistx/backup`.
|
|
4
|
+
`mongoBackups` is the way in: one key file, a full backup a week and
|
|
5
|
+
incrementals in between, each read back and rotated, a restore of a whole
|
|
6
|
+
database or part of it, and a drill. Underneath, `mongoSource` reads a full
|
|
7
|
+
backup in one snapshot at one cluster time and incrementals from the change
|
|
8
|
+
stream, and `mongoTarget` and `restoreCollections` land each collection
|
|
9
|
+
whole or not at all. It runs on Bun, against a replica set (a sharded
|
|
10
|
+
cluster is untested). Its peers are `@softistx/backup`, `mongodb` 7 and
|
|
11
|
+
`typescript`.
|
|
12
|
+
|
|
13
|
+
| Page | Read it when |
|
|
14
|
+
| --- | --- |
|
|
15
|
+
| [Getting started](guide/getting-started.md) | you want a first backup, restore and drill: the key file (`nxgt-mongo-backup keygen`, `generateKeyFile`, `readKeyFile`, `KEY_FILE`), `mongoBackups` and its options and defaults, what `run`, `restore`, `drill`, `list` and `binding` return and throw; then the lower level — `mongoSource`, choosing collections, what a snapshot guarantees, the window it must fit in, views and GridFS, and a job by hand |
|
|
16
|
+
| [Incremental](guide/incremental.md) | you want to know what an incremental stores, or make them by hand: what the change stream records, updates and post-images, dotted field names, the collections a chain follows and when a change to `collections` takes effect, renames into and out of them, a dropped database, where the next backup resumes, the oplog window and `HISTORY_LOST`, and a weekly schedule |
|
|
17
|
+
| [Restore](guide/restore.md) | you want to know what a restore does — `mongoBackups`' `restore` included — or restore by hand: `mongoTarget` and its options, the order entries land in, staging under `nxgt-restore-<uuid>`, `EXISTS` and `replace`, how changes are staged and applied, renames and `EXISTS`, one target per restore, restoring one collection with `only`, restoring into another database, what a failure leaves, restoring into the database you back up, and restoring part of a backup with `restoreCollections` — some collections and views, under other names, whole or only the documents a query takes |
|
|
18
|
+
| [Format](guide/format.md) | you want to know what a backup holds: `metadata/<name>`, `documents/<name>` and `changes/<n>`, their bytes, the recorded position and how it says which collections a chain follows, the fingerprints, and reading a backup with `bsondump` without this package |
|
|
19
|
+
| [Running it in production](guide/operations.md) | you are putting `mongoBackups` on a schedule: the key file on the job host, `run` hourly and what it does on `HISTORY_LOST` and on the repository's lock, the oplog and snapshot windows, alerting on failure, fallback and silence, `drill`, forcing a full backup, the `restore` call for each thing you may need back, and moving from a job by hand |
|
|
20
|
+
| [Errors](guide/errors.md) | you are handling a failure: `MongoBackupError`, its eight codes and every message — `KEY_FILE` and the `NOT_FOUND`s of `mongoBackups` among them — the driver errors that pass through, the bare `TypeError`s, and a handler for `run` on a schedule |
|
|
21
|
+
| [Troubleshooting](troubleshooting.md) | a call threw and you have the message |
|
|
22
|
+
| [Roadmap](roadmap.md) | you want to know what is coming, and what has been ruled out |
|
|
23
|
+
|
|
24
|
+
The [README](../README.md) is the short version: install, the key file,
|
|
25
|
+
one example for `run`, `restore` and `drill`, the lower level, the API, the
|
|
26
|
+
errors and the traps. Repositories, keys,
|
|
27
|
+
signing, rotation and the chain rules are `@softistx/backup`'s —
|
|
28
|
+
[its documentation](https://github.com/softistx/nxgt-data/tree/develop/packages/backup/docs).
|
|
@@ -0,0 +1,411 @@
|
|
|
1
|
+
# Errors
|
|
2
|
+
|
|
3
|
+
This page lists everything `@softistx/mongo-backup` throws — `MongoBackupError`
|
|
4
|
+
and its eight codes, and the bare `TypeError`s — with every message, and a
|
|
5
|
+
handler for a scheduled job.
|
|
6
|
+
|
|
7
|
+
```ts
|
|
8
|
+
import { MongoBackupError, mongoBackups } from '@softistx/mongo-backup';
|
|
9
|
+
|
|
10
|
+
const backups = mongoBackups({ db, repository: '/mnt/backups', keyFile: '/etc/backup/shop.key' });
|
|
11
|
+
try {
|
|
12
|
+
console.log(JSON.stringify(await backups.run())); // HISTORY_LOST is handled inside: a full backup
|
|
13
|
+
} catch (error) {
|
|
14
|
+
if (error instanceof MongoBackupError) console.error(`backup failed (${error.code}): ${error.message}`);
|
|
15
|
+
throw error; // exit non-zero: the scheduler alerts
|
|
16
|
+
}
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## `MongoBackupError`
|
|
20
|
+
|
|
21
|
+
```ts
|
|
22
|
+
type MongoBackupErrorCode =
|
|
23
|
+
| 'SNAPSHOT_TOO_OLD'
|
|
24
|
+
| 'CHANGING'
|
|
25
|
+
| 'HISTORY_LOST'
|
|
26
|
+
| 'UNSUPPORTED'
|
|
27
|
+
| 'EXISTS'
|
|
28
|
+
| 'MALFORMED'
|
|
29
|
+
| 'NOT_FOUND'
|
|
30
|
+
| 'KEY_FILE';
|
|
31
|
+
|
|
32
|
+
class MongoBackupError extends Error {
|
|
33
|
+
readonly name: 'MongoBackupError';
|
|
34
|
+
readonly code: MongoBackupErrorCode;
|
|
35
|
+
constructor(message: string, code: MongoBackupErrorCode, options?: { cause?: unknown });
|
|
36
|
+
}
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
- **Its message never quotes a document, a collection name or a value.** A
|
|
40
|
+
backup error ends up in logs, and a collection name can say more than it
|
|
41
|
+
should. To know which collection, look at the backup's entries, or at the
|
|
42
|
+
`cause`. That holds for `MongoBackupError` and the bare `TypeError`s
|
|
43
|
+
only: an error from the driver passes through unchanged, and its message
|
|
44
|
+
may quote names and values — [errors that pass through](#errors-that-pass-through).
|
|
45
|
+
- **Nor does it quote a key**, nor the content of the key file.
|
|
46
|
+
- **`cause` is the server's error**, when there is one — for
|
|
47
|
+
`SNAPSHOT_TOO_OLD` and `HISTORY_LOST` — and the file system's `ENOENT`
|
|
48
|
+
for a missing key file.
|
|
49
|
+
- **It passes through `@softistx/backup` as it is**: `create` and `restore`
|
|
50
|
+
reject with it, never wrapped. Check `instanceof MongoBackupError`
|
|
51
|
+
before `code` — `@softistx/backup`'s `BackupError` has a `code` too, and so
|
|
52
|
+
does Node's `ENOENT`.
|
|
53
|
+
|
|
54
|
+
## The codes
|
|
55
|
+
|
|
56
|
+
### `KEY_FILE`
|
|
57
|
+
|
|
58
|
+
The key file `mongoBackups` was given cannot be used. It is read on the
|
|
59
|
+
first call — `run`, `restore`, `drill`, `list` or `binding` — not by
|
|
60
|
+
`mongoBackups` itself. `readKeyFile` throws the same, its messages
|
|
61
|
+
starting with `readKeyFile:` rather than `mongoBackups on "shop":`. A failed read is
|
|
62
|
+
tried again on the next call, so fixing the file needs no restart —
|
|
63
|
+
[the key file](getting-started.md#the-key-file).
|
|
64
|
+
|
|
65
|
+
| Message | When |
|
|
66
|
+
| --- | --- |
|
|
67
|
+
| `mongoBackups on "shop": there is no key file there; write one with nxgt-mongo-backup keygen` | nothing at that path; the `ENOENT` is its `cause`. Any other failure to open it — `EACCES`, `ENOTDIR` — is the system's error, as it is |
|
|
68
|
+
| `mongoBackups on "shop": others than its owner can read or write the key file; chmod 600 it` | its group or others have any right on it |
|
|
69
|
+
| `mongoBackups on "shop": the key file is not one keygen wrote` | it holds no age identity, no Ed25519 private key, or one that does not parse — an identity with a bad checksum, or a private key that is not Ed25519, among them. No `cause`: the parsers' own errors quote the key |
|
|
70
|
+
|
|
71
|
+
```ts
|
|
72
|
+
import { MongoBackupError } from '@softistx/mongo-backup';
|
|
73
|
+
|
|
74
|
+
try {
|
|
75
|
+
await backups.run();
|
|
76
|
+
} catch (error) {
|
|
77
|
+
if (error instanceof MongoBackupError && error.code === 'KEY_FILE') {
|
|
78
|
+
console.error(error.message); // never the key: safe to log
|
|
79
|
+
process.exit(78); // a configuration error: no point in retrying before someone fixes the file
|
|
80
|
+
}
|
|
81
|
+
throw error;
|
|
82
|
+
}
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
### Through `mongoBackups`
|
|
86
|
+
|
|
87
|
+
**`run`, `restore` and `drill` rename the messages below.** A message that
|
|
88
|
+
starts with `mongoSource:` or `mongoTarget:` when you call those directly
|
|
89
|
+
starts, through `mongoBackups`, with the call and the backup's name —
|
|
90
|
+
`run on "shop":`, `restore on "shop":` or `drill on "shop":` — and the rest
|
|
91
|
+
is unchanged. The class and the `code` stay the same, and the original
|
|
92
|
+
error, with its own prefix, is the `cause`:
|
|
93
|
+
|
|
94
|
+
```ts
|
|
95
|
+
await backups.restore({ into: client.db('shop') }); // 'shop' already holds a collection
|
|
96
|
+
// MongoBackupError (EXISTS): restore on "shop": a collection or view the backup holds is already in the database; restore into another one, or pass replace: true
|
|
97
|
+
// error.cause.message: mongoTarget: a collection or view the backup holds is already in the database; …
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
Search for the part after the colon: every message on this page, and on
|
|
101
|
+
[troubleshooting](../troubleshooting.md), is quoted with its direct prefix.
|
|
102
|
+
A partial `restore` throws `restoreCollections:`'s messages under its own
|
|
103
|
+
prefix at once, so those have no `cause`. An error
|
|
104
|
+
from the driver or from `@softistx/backup` is not renamed. `restoreCollections` called directly does the same for the `mongoTarget:`
|
|
105
|
+
refusals of the restore it runs into its scratch database: they read
|
|
106
|
+
`restoreCollections: …`, the original as `cause`.
|
|
107
|
+
|
|
108
|
+
### `SNAPSHOT_TOO_OLD`
|
|
109
|
+
|
|
110
|
+
A full backup took longer than the server keeps the history its snapshot
|
|
111
|
+
reads — [a long snapshot](getting-started.md#a-long-snapshot).
|
|
112
|
+
|
|
113
|
+
```text
|
|
114
|
+
mongoSource: the snapshot outlived the history the server keeps; raise minSnapshotHistoryWindowInSeconds, or back up fewer collections at a time
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
### `CHANGING`
|
|
118
|
+
|
|
119
|
+
A full backup reads the collections, their options and their indexes
|
|
120
|
+
before it pins its snapshot and again after, and pins again while the two
|
|
121
|
+
differ. After five attempts it gives up: something kept creating, dropping,
|
|
122
|
+
renaming or indexing collections the filter takes. Nothing was stored; run
|
|
123
|
+
the backup again once that work has settled —
|
|
124
|
+
[one cluster time](getting-started.md#one-cluster-time).
|
|
125
|
+
|
|
126
|
+
```text
|
|
127
|
+
mongoSource: the collections or their indexes kept changing while the snapshot was taken; try again when they settle
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
```ts
|
|
131
|
+
async function fullBackup(attempts = 3) {
|
|
132
|
+
for (let attempt = 1; ; attempt++) {
|
|
133
|
+
try {
|
|
134
|
+
return await backups.create(source);
|
|
135
|
+
} catch (error) {
|
|
136
|
+
if (!(error instanceof MongoBackupError && error.code === 'CHANGING') || attempt === attempts) {
|
|
137
|
+
throw error;
|
|
138
|
+
}
|
|
139
|
+
await Bun.sleep(60_000); // let the migration or the index build finish
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
### `HISTORY_LOST`
|
|
146
|
+
|
|
147
|
+
An incremental or differential cannot resume where its parent stopped: the
|
|
148
|
+
oplog no longer reaches back there. Every incremental on that parent fails
|
|
149
|
+
the same way; the next backup must be full —
|
|
150
|
+
[the oplog window](incremental.md#the-oplog-window).
|
|
151
|
+
|
|
152
|
+
```text
|
|
153
|
+
mongoSource: the change stream cannot resume where the last backup stopped: the oplog no longer reaches back there; make a full backup
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
### `UNSUPPORTED`
|
|
157
|
+
|
|
158
|
+
The database holds something this version does not back up. The first
|
|
159
|
+
four repeat until the cause is gone or a full backup moves past it; the
|
|
160
|
+
last two come from the deployment.
|
|
161
|
+
|
|
162
|
+
| Message | When |
|
|
163
|
+
| --- | --- |
|
|
164
|
+
| `mongoSource: a collection is of a type this version does not back up (a time-series one); leave it out with collections` | a full backup meets a time-series collection, or an incremental sees one created among the collections it takes — [choosing collections](getting-started.md#choosing-collections) |
|
|
165
|
+
| `mongoSource: an update touched a field whose name holds a dot or is a number, which a change cannot say without its post-image; turn on changeStreamPreAndPostImages for that collection, or make a full backup` | an incremental meets such an update in a collection the chain follows, without post-images — [post-images](incremental.md#post-images-and-dotted-field-names) |
|
|
166
|
+
| `mongoSource: a collection was renamed into those backed up, with documents an incremental backup never read; make a full backup` | a collection the chain does not follow was renamed to a name the filter takes — a restore's own renames from `nxgt-restore-<uuid>`, into the database backed up, among them — [renames](incremental.md#renames) |
|
|
167
|
+
| `mongoSource: the collections created, renamed or dropped since the full backup are too many to record; make a full backup` | an incremental whose chain has seen thousands of collections created, renamed or dropped since its full backup — [format](format.md#the-position-and-the-fingerprints) |
|
|
168
|
+
| `mongoSource: the server gave no operation time; a backup needs a replica set or a sharded cluster` | an incremental, against a server that is not one |
|
|
169
|
+
| `mongoSource: the server gave no snapshot time; a backup needs a replica set or a sharded cluster` | a full backup, against a server that is not one |
|
|
170
|
+
|
|
171
|
+
Against a standalone `mongod`, the server may refuse the snapshot read or
|
|
172
|
+
the change stream with its own error first; either way, the answer is a
|
|
173
|
+
replica set — a single-node one is enough.
|
|
174
|
+
|
|
175
|
+
### `EXISTS`
|
|
176
|
+
|
|
177
|
+
A collection or view the restore would write is already in the target
|
|
178
|
+
database — one the backup holds, one a recorded `create` makes, or the new
|
|
179
|
+
name of a recorded rename that replaced nothing at the source. Nothing of
|
|
180
|
+
it was touched — [`EXISTS` and `replace`](restore.md#exists-and-replace).
|
|
181
|
+
|
|
182
|
+
| Message | When |
|
|
183
|
+
| --- | --- |
|
|
184
|
+
| `mongoTarget: a collection or view the backup holds is already in the database; restore into another one, or pass replace: true` | a `restore` through `mongoTarget`, without `replace` |
|
|
185
|
+
| `restoreCollections: a collection or view to restore is already in the database; restore it under another name, or pass replace: true` | `restoreCollections` in whole mode, without `replace`: a name `as` gives is taken in `db`, checked before anything moves, or taken in the meantime and refused at its rename — [whole collections](restore.md#whole-collections) |
|
|
186
|
+
| `restoreCollections: the scratch database holds collections; give an empty one` | the `scratch` given is not empty; it is left as it was — [the scratch database](restore.md#the-scratch-database) |
|
|
187
|
+
|
|
188
|
+
### `MALFORMED`
|
|
189
|
+
|
|
190
|
+
An entry, a change or a position is not one this version wrote: a backup
|
|
191
|
+
made by something else, or by a later version with a format this one does
|
|
192
|
+
not read.
|
|
193
|
+
|
|
194
|
+
| Message | When |
|
|
195
|
+
| --- | --- |
|
|
196
|
+
| `mongoSource: the backup built on recorded no position this version reads; make a full backup` | an incremental's parent recorded no position, or one this version did not write |
|
|
197
|
+
| `mongoSource: an entry of the backup built on was asked for again; it has no fingerprint, so make a full backup` | the parent recorded an entry without a fingerprint |
|
|
198
|
+
| `mongoTarget: an entry name is not one this version writes` | an entry named other than `metadata/…`, `documents/…` or `changes/<6 to 9 digits>` |
|
|
199
|
+
| `mongoTarget: a metadata entry is larger than 16 MiB` | a metadata entry far larger than options and indexes can be |
|
|
200
|
+
| `mongoTarget: a metadata entry is not one this version wrote` | a metadata entry whose `format`, `type`, `options` or `indexes` is not this version's |
|
|
201
|
+
| `mongoTarget: a collection’s documents came before its metadata` | `documents/<c>` without `metadata/<c>` before it — `only` picked one and not the other |
|
|
202
|
+
| `mongoTarget: an entry is not a sequence of BSON documents` | a `documents/` or `changes/` entry with a length that cannot be a document's, or cut inside one |
|
|
203
|
+
| `mongoTarget: a change is not one this version wrote` | a change record this version cannot apply |
|
|
204
|
+
|
|
205
|
+
The apostrophe in `collection’s` is a typographic one, `’`: search for
|
|
206
|
+
`documents came before its metadata`.
|
|
207
|
+
|
|
208
|
+
### `NOT_FOUND`
|
|
209
|
+
|
|
210
|
+
`mongoBackups`' `restore` and `drill` found no backup to restore. The
|
|
211
|
+
message names the call and the backup — `name`, or the database's name;
|
|
212
|
+
`shop` here:
|
|
213
|
+
|
|
214
|
+
| Message | When |
|
|
215
|
+
| --- | --- |
|
|
216
|
+
| `restore on "shop": the repository holds no backup yet` | `restore` without `at`, before the first `run` |
|
|
217
|
+
| `drill on "shop": the repository holds no backup yet` | `drill`, before the first `run` |
|
|
218
|
+
| `restore on "shop": no backup was made at or before that time` | `restore` with an `at` date older than every backup the repository holds |
|
|
219
|
+
|
|
220
|
+
An `at` id the repository does not hold is `@softistx/backup`'s `BackupError`
|
|
221
|
+
`NOT_FOUND`, not this one.
|
|
222
|
+
|
|
223
|
+
A name in the list `restoreCollections` was given as `collections` is not
|
|
224
|
+
in the backup — as its collections were named at the backup's time, after
|
|
225
|
+
every rename the chain replays. It comes after the backup was rebuilt, and
|
|
226
|
+
before anything reaches `db` —
|
|
227
|
+
[`collections`](restore.md#collections-names-at-the-backups-time).
|
|
228
|
+
|
|
229
|
+
```text
|
|
230
|
+
restoreCollections: a collection named in collections is not in the backup
|
|
231
|
+
```
|
|
232
|
+
|
|
233
|
+
`mongoBackups`' `restore` with `collections` goes through
|
|
234
|
+
`restoreCollections`, and throws the same, naming its own call:
|
|
235
|
+
`restore on "shop": a collection named in collections is not in the backup`.
|
|
236
|
+
|
|
237
|
+
`@softistx/backup`'s `BackupError` has a `NOT_FOUND` too — a backup id that is
|
|
238
|
+
not there: check `instanceof` before `code`.
|
|
239
|
+
|
|
240
|
+
## The bare `TypeError`s
|
|
241
|
+
|
|
242
|
+
Wiring that could never work is a `TypeError`, not a `MongoBackupError`.
|
|
243
|
+
**Through `mongoBackups`, the messages name the call and the backup.**
|
|
244
|
+
A partial `restore` — `collections`, `as` or `documents` — refuses as
|
|
245
|
+
`restoreCollections` does, with `restore on "<name>":` where the table
|
|
246
|
+
says `restoreCollections:`: `restore on "shop": as names a collection not
|
|
247
|
+
restored`, `restore on "shop": a collection or view to restore is already
|
|
248
|
+
in the database; …`. Called directly, `restoreCollections` keeps its own
|
|
249
|
+
prefix. `shop` is the backup's name in the examples on this page.
|
|
250
|
+
|
|
251
|
+
`mongoBackups` throws its own at once, before it reads the key file or
|
|
252
|
+
touches the database; `run` rejects with its one, and `restore` with its
|
|
253
|
+
three, before reading anything, and the options it hands on are checked by `mongoTarget` and
|
|
254
|
+
`restoreCollections`, below.
|
|
255
|
+
`mongoSource` and `mongoTarget` throw theirs at once, but for
|
|
256
|
+
`mongoSource: a collection named in collections is not in the database`,
|
|
257
|
+
which `create` throws when the full backup lists the collections.
|
|
258
|
+
`restoreCollections` rejects with its own before reading anything, but for
|
|
259
|
+
the three about `as` it can only tell from the backup's names — anything
|
|
260
|
+
a function gives, a map naming a collection after one that keeps its
|
|
261
|
+
own, and a map key naming nothing restored: those come after the rebuild,
|
|
262
|
+
and before anything reaches `db`.
|
|
263
|
+
|
|
264
|
+
| Message | When |
|
|
265
|
+
| --- | --- |
|
|
266
|
+
| `mongoBackups: db must be a MongoDB Db` | `db` is missing, or is not a driver `Db` |
|
|
267
|
+
| `mongoBackups: keyFile must be an absolute path` | `keyFile` is missing, not a string, or relative |
|
|
268
|
+
| `mongoBackups: repository must be an absolute folder, or repositories` | `repository` is a relative path |
|
|
269
|
+
| `mongoBackups: keep must be false, or name rules each a whole number, 1 or more` | `keep` names no rule, or a rule that is not a whole number of 1 or more — checked by `mongoBackups(…)`, before any backup is stored |
|
|
270
|
+
| `mongoBackups: fullEvery must be a whole number of milliseconds, an hour or more` | `fullEvery` is under 3 600 000, or not a whole number |
|
|
271
|
+
| `mongoBackups: repository must list repositories, each under a name of its own` | `repository` is an empty list, or two repositories in it have the same name |
|
|
272
|
+
| `mongoBackups: tmpDir must be an absolute path` | a relative `tmpDir` |
|
|
273
|
+
| `mongoBackups: name must be 1 to 100 characters, lowercase letters, digits, ".", "_" and "-", starting with a letter or a digit, without ".partial-"` | `name` is given, and is not a backup name; `defineBackup`'s `TypeError` is its `cause` |
|
|
274
|
+
| `mongoBackups: the database's name cannot name a backup; give name: 1 to 100 characters, lowercase letters, digits, ".", "_" and "-", starting with a letter or a digit, without ".partial-"` | no `name`, and the database's name is not a backup name — `client.db('MyShop')`: pass `name: 'myshop'`. `defineBackup`'s `TypeError` is its `cause` |
|
|
275
|
+
| `restore on "shop": into must be a MongoDB Db` | `restore` with an `into` that is missing, or not a driver `Db`; it rejects before anything is read |
|
|
276
|
+
| `restore on "shop": at must be a backup's id or a valid Date` | `restore` with an `at` that is an invalid `Date` — `new Date('yesterday')`; it rejects before anything is read |
|
|
277
|
+
| `restore on "shop": replace is for whole collections; documents says what happens to those there` | `restore` with `documents` and `replace` both; the types refuse it too |
|
|
278
|
+
| `run on "shop": now must be a valid Date` | `run` with a `now` that is not a valid `Date`; it rejects before anything is read |
|
|
279
|
+
| `mongoSource: db must be a MongoDB Db` | `db` is missing, or is not a driver `Db` — a name, a client |
|
|
280
|
+
| `mongoSource: collections must be a list of names or a function` | `collections` is a string, or a list holding something else |
|
|
281
|
+
| `mongoSource: a collection named in collections is not in the database` | a name in the list that the database lacks |
|
|
282
|
+
| `mongoTarget: db must be a MongoDB Db` | `db` is missing, or is not a driver `Db` |
|
|
283
|
+
| `mongoTarget: tmpDir must be an absolute path` | a relative `tmpDir` |
|
|
284
|
+
| `restoreCollections: db must be a MongoDB Db` | `db` is missing, or is not a driver `Db` |
|
|
285
|
+
| `restoreCollections: scratch must be a MongoDB Db` | `scratch` is given, and is not a driver `Db` |
|
|
286
|
+
| `restoreCollections: scratch must be on db's client` | `scratch` comes from another `MongoClient` |
|
|
287
|
+
| `restoreCollections: scratch must be another database` | `scratch` has `db`'s name |
|
|
288
|
+
| `restoreCollections: documents must be { filter, existing: 'replace' \| 'keep' }` | a `filter` that is not a query object, or an `existing` other than those two |
|
|
289
|
+
| `restoreCollections: replace is for whole collections; documents says what happens to those there` | `replace` given with `documents` |
|
|
290
|
+
| `restoreCollections: as must give a collection name` | `as` gives an empty name, one holding `$` or a NUL, one starting with `system.`, or something not a string |
|
|
291
|
+
| `restoreCollections: as gives two collections one name` | `as` gives two collections or views the same name |
|
|
292
|
+
| `restoreCollections: as names a collection not restored` | a key of an `as` map names no collection or view restored — a typo, or a view when `documents` is given; checked once the backup has been rebuilt |
|
|
293
|
+
|
|
294
|
+
`mongoBackups` checks what `@softistx/backup` would refuse — the name, the
|
|
295
|
+
repositories, `tmpDir` — when it is called, so none of `defineBackup`'s or
|
|
296
|
+
`bindBackup`'s `TypeError`s reaches you from a later call.
|
|
297
|
+
|
|
298
|
+
## Errors that pass through
|
|
299
|
+
|
|
300
|
+
- **The driver's errors** — a lost connection, an authentication failure, a
|
|
301
|
+
write the server refuses on restore (`E11000 duplicate key error`, say) —
|
|
302
|
+
reject `create` or `restore` unchanged, as the driver threw them. **Their
|
|
303
|
+
messages may quote collection names, index keys and values**: redact them
|
|
304
|
+
before they reach a shared log, or log `error.code` and `error.name` only.
|
|
305
|
+
- **`@softistx/backup`'s** — `NOT_FOUND`, `INTEGRITY`, `DECRYPT`, `LOCKED`,
|
|
306
|
+
`PARTIAL`, … — are its own `BackupError`, documented with it:
|
|
307
|
+
[errors](https://github.com/softistx/nxgt-data/blob/develop/packages/backup/docs/guide/errors.md).
|
|
308
|
+
|
|
309
|
+
## A handler for a scheduled job
|
|
310
|
+
|
|
311
|
+
`run` already starts a new chain on `HISTORY_LOST`. What is left: skip a
|
|
312
|
+
run that met another on the repository's lock, make a full backup when the
|
|
313
|
+
chain cannot go on, and alert on everything else.
|
|
314
|
+
|
|
315
|
+
```ts
|
|
316
|
+
import { BackupError } from '@softistx/backup';
|
|
317
|
+
import { MongoBackupError, mongoBackups, mongoSource } from '@softistx/mongo-backup';
|
|
318
|
+
import { MongoClient } from 'mongodb';
|
|
319
|
+
|
|
320
|
+
const client = await MongoClient.connect(process.env.MONGO_URL as string);
|
|
321
|
+
const db = client.db('shop');
|
|
322
|
+
const backups = mongoBackups({ db, repository: '/mnt/backups', keyFile: '/etc/backup/shop.key' });
|
|
323
|
+
|
|
324
|
+
/** Whether `error` is another run holding the repository's lock. */
|
|
325
|
+
function locked(error: unknown): boolean {
|
|
326
|
+
if (!(error instanceof BackupError)) return false;
|
|
327
|
+
if (error.code === 'LOCKED') return true;
|
|
328
|
+
// NOT_STORED: stored nowhere, each repository's error in `outcomes`.
|
|
329
|
+
// PARTIAL is not skipped: a copy was stored, and the run stopped before verify.
|
|
330
|
+
return (
|
|
331
|
+
error.code === 'NOT_STORED' &&
|
|
332
|
+
error.outcomes.some((o) => !o.stored && o.error instanceof BackupError && o.error.code === 'LOCKED')
|
|
333
|
+
);
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
try {
|
|
337
|
+
console.log(JSON.stringify(await backups.run()));
|
|
338
|
+
} catch (error) {
|
|
339
|
+
if (locked(error)) {
|
|
340
|
+
console.warn('another run holds the repository; skipped');
|
|
341
|
+
} else if (error instanceof MongoBackupError && (error.code === 'UNSUPPORTED' || error.code === 'MALFORMED')) {
|
|
342
|
+
// A full backup gets past a rename, a dotted update or a parent this version
|
|
343
|
+
// cannot read. A time-series collection fails it too, and that error reaches you.
|
|
344
|
+
const { backups: bound, keys } = await backups.binding();
|
|
345
|
+
const full = await bound.create(mongoSource({ db }));
|
|
346
|
+
await bound.verify(full.id, { identities: [keys.identity] });
|
|
347
|
+
} else {
|
|
348
|
+
throw error; // KEY_FILE, CHANGING, SNAPSHOT_TOO_OLD, the driver's: a person must look
|
|
349
|
+
}
|
|
350
|
+
} finally {
|
|
351
|
+
await client.close();
|
|
352
|
+
}
|
|
353
|
+
```
|
|
354
|
+
|
|
355
|
+
### By hand
|
|
356
|
+
|
|
357
|
+
The same for a job on the lower level — `bindBackup` and `mongoSource` —
|
|
358
|
+
that makes its own incrementals: start a new chain when the chain cannot go
|
|
359
|
+
on, and alert on everything else.
|
|
360
|
+
|
|
361
|
+
```ts
|
|
362
|
+
import { BackupError, bindBackup, defineBackup, localRepository } from '@softistx/backup';
|
|
363
|
+
import { MongoBackupError, mongoSource } from '@softistx/mongo-backup';
|
|
364
|
+
import { MongoClient } from 'mongodb';
|
|
365
|
+
|
|
366
|
+
const client = await MongoClient.connect(process.env.MONGO_URL as string);
|
|
367
|
+
const backups = bindBackup(defineBackup({ name: 'shop-db' }), {
|
|
368
|
+
repositories: [localRepository({ path: '/mnt/backups' })],
|
|
369
|
+
recipients: [process.env.BACKUP_RECIPIENT as string],
|
|
370
|
+
});
|
|
371
|
+
const identities = [process.env.BACKUP_IDENTITY as string];
|
|
372
|
+
const source = mongoSource({ db: client.db('shop') });
|
|
373
|
+
|
|
374
|
+
/** A full backup, tried again while the collections are changing. */
|
|
375
|
+
async function fullBackup(attempts = 3) {
|
|
376
|
+
for (let attempt = 1; ; attempt++) {
|
|
377
|
+
try {
|
|
378
|
+
return await backups.create(source);
|
|
379
|
+
} catch (error) {
|
|
380
|
+
const changing = error instanceof MongoBackupError && error.code === 'CHANGING';
|
|
381
|
+
if (!changing || attempt === attempts) throw error;
|
|
382
|
+
await Bun.sleep(60_000);
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
/** Whether a new full backup is the answer to `error`. */
|
|
388
|
+
function startsOver(error: unknown): boolean {
|
|
389
|
+
if (error instanceof BackupError) return error.code === 'NOT_FOUND';
|
|
390
|
+
if (!(error instanceof MongoBackupError)) return false;
|
|
391
|
+
switch (error.code) {
|
|
392
|
+
case 'HISTORY_LOST': // the oplog moved on
|
|
393
|
+
case 'MALFORMED': // a parent this version cannot build on
|
|
394
|
+
// A full backup gets past a rename or a dotted update. A time-series
|
|
395
|
+
// collection fails it too, with the same code, and that error reaches you.
|
|
396
|
+
case 'UNSUPPORTED':
|
|
397
|
+
return true;
|
|
398
|
+
default:
|
|
399
|
+
return false; // SNAPSHOT_TOO_OLD and CHANGING come from a full backup; EXISTS and NOT_FOUND from a restore
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
try {
|
|
404
|
+
await backups.create(source, { kind: 'incremental', identities });
|
|
405
|
+
} catch (error) {
|
|
406
|
+
if (!startsOver(error)) throw error; // alert: a person must look
|
|
407
|
+
await fullBackup();
|
|
408
|
+
} finally {
|
|
409
|
+
await client.close();
|
|
410
|
+
}
|
|
411
|
+
```
|