@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.
Files changed (72) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +484 -2
  3. package/dist/backups/key-file.d.ts +30 -0
  4. package/dist/backups/key-file.d.ts.map +1 -0
  5. package/dist/backups/mongo-backups.d.ts +33 -0
  6. package/dist/backups/mongo-backups.d.ts.map +1 -0
  7. package/dist/backups/operations.d.ts +55 -0
  8. package/dist/backups/operations.d.ts.map +1 -0
  9. package/dist/backups/options.d.ts +60 -0
  10. package/dist/backups/options.d.ts.map +1 -0
  11. package/dist/backups/plan.d.ts +20 -0
  12. package/dist/backups/plan.d.ts.map +1 -0
  13. package/dist/chunks/index-j0tptz7w.js +97 -0
  14. package/dist/chunks/index-j0tptz7w.js.map +11 -0
  15. package/dist/cli.d.ts +3 -0
  16. package/dist/cli.d.ts.map +1 -0
  17. package/dist/cli.js +31 -0
  18. package/dist/cli.js.map +10 -0
  19. package/dist/errors.d.ts +43 -0
  20. package/dist/errors.d.ts.map +1 -0
  21. package/dist/format/bson-stream.d.ts +13 -0
  22. package/dist/format/bson-stream.d.ts.map +1 -0
  23. package/dist/format/change.d.ts +61 -0
  24. package/dist/format/change.d.ts.map +1 -0
  25. package/dist/format/document.d.ts +7 -0
  26. package/dist/format/document.d.ts.map +1 -0
  27. package/dist/format/metadata.d.ts +14 -0
  28. package/dist/format/metadata.d.ts.map +1 -0
  29. package/dist/format/names.d.ts +22 -0
  30. package/dist/format/names.d.ts.map +1 -0
  31. package/dist/format/position.d.ts +29 -0
  32. package/dist/format/position.d.ts.map +1 -0
  33. package/dist/index.d.ts +17 -0
  34. package/dist/index.d.ts.map +1 -0
  35. package/dist/index.js +1340 -0
  36. package/dist/index.js.map +32 -0
  37. package/dist/restore/landing.d.ts +26 -0
  38. package/dist/restore/landing.d.ts.map +1 -0
  39. package/dist/restore/options.d.ts +59 -0
  40. package/dist/restore/options.d.ts.map +1 -0
  41. package/dist/restore/restore-collections.d.ts +12 -0
  42. package/dist/restore/restore-collections.d.ts.map +1 -0
  43. package/dist/source/catalog.d.ts +28 -0
  44. package/dist/source/catalog.d.ts.map +1 -0
  45. package/dist/source/changes.d.ts +12 -0
  46. package/dist/source/changes.d.ts.map +1 -0
  47. package/dist/source/collections.d.ts +20 -0
  48. package/dist/source/collections.d.ts.map +1 -0
  49. package/dist/source/events.d.ts +46 -0
  50. package/dist/source/events.d.ts.map +1 -0
  51. package/dist/source/followed.d.ts +13 -0
  52. package/dist/source/followed.d.ts.map +1 -0
  53. package/dist/source/mongo-source.d.ts +24 -0
  54. package/dist/source/mongo-source.d.ts.map +1 -0
  55. package/dist/source/snapshot.d.ts +17 -0
  56. package/dist/source/snapshot.d.ts.map +1 -0
  57. package/dist/target/changes.d.ts +8 -0
  58. package/dist/target/changes.d.ts.map +1 -0
  59. package/dist/target/collection.d.ts +20 -0
  60. package/dist/target/collection.d.ts.map +1 -0
  61. package/dist/target/mongo-target.d.ts +22 -0
  62. package/dist/target/mongo-target.d.ts.map +1 -0
  63. package/docs/README.md +28 -0
  64. package/docs/guide/errors.md +411 -0
  65. package/docs/guide/format.md +179 -0
  66. package/docs/guide/getting-started.md +716 -0
  67. package/docs/guide/incremental.md +323 -0
  68. package/docs/guide/operations.md +237 -0
  69. package/docs/guide/restore.md +684 -0
  70. package/docs/roadmap.md +61 -0
  71. package/docs/troubleshooting.md +1257 -0
  72. package/package.json +70 -5
@@ -0,0 +1,179 @@
1
+ # Format
2
+
3
+ This page describes what a `mongoSource` backup holds — its entries, their
4
+ names and their bytes — and how to read one without this package.
5
+
6
+ ```ts
7
+ import { bindBackup, defineBackup, directoryTarget, localRepository } from '@softistx/backup';
8
+
9
+ const backups = bindBackup(defineBackup({ name: 'shop-db' }), {
10
+ repositories: [localRepository({ path: '/mnt/backups' })],
11
+ recipients: [process.env.BACKUP_RECIPIENT as string],
12
+ });
13
+
14
+ const latest = (await backups.list()).backups.at(-1);
15
+ if (!latest) throw new Error('no backup to inspect');
16
+
17
+ // Every entry as a plain file: metadata/orders, documents/orders, changes/000001, …
18
+ await backups.restore(latest.id, directoryTarget({ path: '/srv/inspect' }), {
19
+ identities: [process.env.BACKUP_IDENTITY as string],
20
+ });
21
+ ```
22
+
23
+ How entries are compressed, encrypted and stored — `0.age`, the catalog,
24
+ the manifest — is `@softistx/backup`'s
25
+ [format](https://github.com/softistx/nxgt-data/blob/develop/packages/backup/docs/guide/format.md).
26
+ What follows is what is inside each entry.
27
+
28
+ ## The entries
29
+
30
+ | Name | Holds | In |
31
+ | --- | --- | --- |
32
+ | `metadata/<collection>` | a collection's or a view's options and indexes, as canonical Extended JSON | every backup |
33
+ | `documents/<collection>` | a collection's documents, as concatenated BSON | every backup, except for a view |
34
+ | `changes/<n>` | the changes one incremental or differential read, as concatenated BSON | incremental and differential backups |
35
+
36
+ - **`<collection>` is the collection's name**, as it is: `metadata/fs.files`,
37
+ `documents/orders`.
38
+ - **`<n>` is six digits**, from `000001`, numbered in the order the changes
39
+ apply: an incremental on an incremental holds its parent's
40
+ `changes/000001` and its own `changes/000002`.
41
+ - **A full backup's entries come in collection-name order**, each
42
+ collection's metadata just before its documents; an incremental's come in
43
+ its parent's order, then its own `changes/<n>`. A restore takes them in
44
+ that order — [restore](restore.md#the-order).
45
+
46
+ ## `metadata/<collection>`
47
+
48
+ One JSON document, canonical Extended JSON — so a number keeps its kind:
49
+
50
+ ```json
51
+ {
52
+ "format": "nxgt-mongo-backup-metadata/1",
53
+ "type": "collection",
54
+ "options": {
55
+ "validator": { "$jsonSchema": { "required": ["name"] } },
56
+ "collation": { "locale": "fr", "strength": { "$numberInt": "2" } }
57
+ },
58
+ "indexes": [
59
+ { "key": { "name": { "$numberInt": "1" } }, "name": "by_name", "unique": true }
60
+ ]
61
+ }
62
+ ```
63
+
64
+ | Field | |
65
+ | --- | --- |
66
+ | `format` | `nxgt-mongo-backup-metadata/1`. A restore refuses any other with `MALFORMED` |
67
+ | `type` | `collection` or `view` |
68
+ | `options` | what `listCollections` gave as the collection's options: validator, collation, `capped` and `size`, `changeStreamPreAndPostImages`; for a view, `viewOn` and `pipeline` |
69
+ | `indexes` | what `listIndexes` gave, without the `_id` index, `v` and `ns` |
70
+
71
+ The options and indexes are those read around the snapshot, not after
72
+ it: an index built while the backup runs is not here, but in the next
73
+ incremental's `changes/<n>` —
74
+ [one cluster time](getting-started.md#one-cluster-time).
75
+
76
+ A restore refuses a metadata entry over 16 MiB with `MALFORMED`: options
77
+ and indexes never come near it.
78
+
79
+ ## `documents/<collection>`
80
+
81
+ Every document of the collection, each as its own BSON bytes, one after the
82
+ other — the format `mongodump` writes to a `.bson` file. Each document is
83
+ read raw from the server and written as it came: no number changes kind,
84
+ and nothing is re-encoded.
85
+
86
+ A restore reads them back one document at a time, never the whole entry:
87
+ a length that cannot be a document's — under 5 bytes, or over 17 MiB — or
88
+ a stream that ends inside one is refused with
89
+ `MongoBackupError (MALFORMED): mongoTarget: an entry is not a sequence of BSON documents`.
90
+
91
+ ## `changes/<n>`
92
+
93
+ One BSON document per change, in the order they apply, each with an `op`
94
+ and the collection it is about, `coll`:
95
+
96
+ | `op` | Other fields |
97
+ | --- | --- |
98
+ | `insert`, `replace` | `key` (the document's `_id`), `doc` (the whole document) |
99
+ | `update` | `key`, `set` (fields and their new values, by path), `unset` (paths), `truncated` (`{ field, newSize }` for each array cut) |
100
+ | `delete` | `key` |
101
+ | `create` | `options` |
102
+ | `createIndexes` | `indexes` |
103
+ | `dropIndexes` | `names` |
104
+ | `modify` | `changes`: what `collMod` was given |
105
+ | `drop` | — |
106
+ | `rename` | `to`: the new name, in the same database; `dropTarget`: `true` when the rename replaced a collection of that name at the source, `false` otherwise |
107
+ | `dropDatabase` | no `coll` |
108
+
109
+ A record that is none of these — a `rename` without a boolean `dropTarget`
110
+ among them — is refused on restore with
111
+ `MongoBackupError (MALFORMED): mongoTarget: a change is not one this version wrote`.
112
+
113
+ On restore, an entry is staged whole before any change applies, to
114
+ `<tmpDir>/nxgt-mongo-changes-XXXXXX/changes.bson`: a folder made with
115
+ `mkdtemp`, readable only by the user that runs the restore, and removed
116
+ afterwards — [restore](restore.md#changes).
117
+
118
+ ## The position and the fingerprints
119
+
120
+ `mongoSource` records **where the next incremental starts, and how the
121
+ collections it follows differ from the full backup's**, as the backup's
122
+ position, kept encrypted in its
123
+ catalog — canonical Extended JSON, with either a cluster time (after a full
124
+ backup) or a resume token (after an incremental):
125
+
126
+ ```json
127
+ { "format": "nxgt-mongo-backup-position/1", "startAtOperationTime": { "$timestamp": { "t": 1791158100, "i": 4 } }, "added": [], "removed": [] }
128
+ ```
129
+
130
+ ```json
131
+ { "format": "nxgt-mongo-backup-position/1", "startAfter": { "_data": "8267…" }, "added": ["reviews"], "removed": ["drafts"] }
132
+ ```
133
+
134
+ | Field | |
135
+ | --- | --- |
136
+ | `format` | `nxgt-mongo-backup-position/1` |
137
+ | `startAtOperationTime` | after a full backup: the first cluster time its snapshot does not hold |
138
+ | `startAfter` | after an incremental: the resume token of the last change it read, or the stream's own once it caught up |
139
+ | `added` | collections and views the chain follows that its full backup does not hold — created since, and taken by the filter — sorted |
140
+ | `removed` | collections and views the full backup holds that the chain follows no more — dropped or renamed away — sorted |
141
+
142
+ The chain follows its full backup's collections and views — one
143
+ `metadata/<name>` entry each, in the catalog every backup of the chain
144
+ lists — plus `added`, less `removed`: so the position stays small however
145
+ many collections the database holds —
146
+ [incremental](incremental.md#the-collections-a-chain-follows). A position
147
+ is at most 64 KiB, `@softistx/backup`'s limit; past it the incremental fails
148
+ `UNSUPPORTED` — `mongoSource: the collections created, renamed or dropped
149
+ since the full backup are too many to record; make a full backup`.
150
+
151
+ An incremental built on a backup with no position this version reads — one
152
+ without `added` and `removed` — fails
153
+ with `MALFORMED` — `mongoSource: the backup built on recorded no position
154
+ this version reads; make a full backup`.
155
+
156
+ Each entry also has a **fingerprint**: `snapshot:<cluster time>` for a full
157
+ backup's entries, `changes:<parent id>:changes/<n>` for a changes entry. An
158
+ incremental gives its parent's entries back with the fingerprints they were
159
+ recorded with, so `@softistx/backup` points to them and never reads them again;
160
+ an entry recorded without one cannot be given back, and the incremental
161
+ fails with
162
+ `MALFORMED` — `mongoSource: an entry of the backup built on was asked for again; it has no fingerprint, so make a full backup`.
163
+
164
+ ## Reading a backup without this package
165
+
166
+ Restore it to a folder with `directoryTarget`, as at the top of this page,
167
+ then read each file with the MongoDB Database Tools:
168
+
169
+ ```sh
170
+ cat /srv/inspect/metadata/orders # options and indexes, as JSON
171
+ bsondump /srv/inspect/documents/orders | head # the documents, one JSON line each
172
+ bsondump /srv/inspect/changes/000001 | head # the changes, one JSON line each
173
+ ```
174
+
175
+ A `documents/<collection>` file is what `mongodump` would have written as
176
+ `<collection>.bson`. `directoryTarget` refuses an entry whose name is not a
177
+ safe relative path, so a collection named with a `\` or a `..` segment
178
+ needs a target of your own — `@softistx/backup`'s
179
+ [sources and targets](https://github.com/softistx/nxgt-data/blob/develop/packages/backup/docs/guide/sources-and-targets.md#writing-a-target).