@sharadox/database-web-library 0.1.0-migration-typed-schema.5 → 0.2.0-development.17
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/README.md +32 -1
- package/distribution/backup-command/index.d.ts +24 -0
- package/distribution/backup-command/index.d.ts.map +1 -0
- package/distribution/backup-command/index.js +96 -0
- package/distribution/backup-command/index.js.map +1 -0
- package/distribution/backup-command/type.d.ts +17 -0
- package/distribution/backup-command/type.d.ts.map +1 -0
- package/distribution/backup-command/type.js +2 -0
- package/distribution/backup-command/type.js.map +1 -0
- package/distribution/backup-dump/index.d.ts +20 -0
- package/distribution/backup-dump/index.d.ts.map +1 -0
- package/distribution/backup-dump/index.js +44 -0
- package/distribution/backup-dump/index.js.map +1 -0
- package/distribution/backup-dump/type.d.ts +9 -0
- package/distribution/backup-dump/type.d.ts.map +1 -0
- package/distribution/backup-dump/type.js +2 -0
- package/distribution/backup-dump/type.js.map +1 -0
- package/distribution/backup-encryption/index.d.ts +17 -0
- package/distribution/backup-encryption/index.d.ts.map +1 -0
- package/distribution/backup-encryption/index.js +38 -0
- package/distribution/backup-encryption/index.js.map +1 -0
- package/distribution/backup-storage/index.d.ts +17 -0
- package/distribution/backup-storage/index.d.ts.map +1 -0
- package/distribution/backup-storage/index.js +40 -0
- package/distribution/backup-storage/index.js.map +1 -0
- package/distribution/backup-storage/type.d.ts +20 -0
- package/distribution/backup-storage/type.d.ts.map +1 -0
- package/distribution/backup-storage/type.js +2 -0
- package/distribution/backup-storage/type.js.map +1 -0
- package/distribution/database-backup/index.d.ts +20 -0
- package/distribution/database-backup/index.d.ts.map +1 -0
- package/distribution/database-backup/index.js +49 -0
- package/distribution/database-backup/index.js.map +1 -0
- package/distribution/database-backup/type.d.ts +28 -0
- package/distribution/database-backup/type.d.ts.map +1 -0
- package/distribution/database-backup/type.js +2 -0
- package/distribution/database-backup/type.js.map +1 -0
- package/distribution/git-hub-web-identity/index.d.ts +9 -0
- package/distribution/git-hub-web-identity/index.d.ts.map +1 -0
- package/distribution/git-hub-web-identity/index.js +18 -0
- package/distribution/git-hub-web-identity/index.js.map +1 -0
- package/distribution/memory-backup-storage/index.d.ts +12 -0
- package/distribution/memory-backup-storage/index.d.ts.map +1 -0
- package/distribution/memory-backup-storage/index.js +22 -0
- package/distribution/memory-backup-storage/index.js.map +1 -0
- package/distribution/migration/index.d.ts.map +1 -1
- package/distribution/migration/index.js +15 -1
- package/distribution/migration/index.js.map +1 -1
- package/distribution/restore-drill/index.d.ts +12 -0
- package/distribution/restore-drill/index.d.ts.map +1 -0
- package/distribution/restore-drill/index.js +35 -0
- package/distribution/restore-drill/index.js.map +1 -0
- package/distribution/restore-drill/type.d.ts +20 -0
- package/distribution/restore-drill/type.d.ts.map +1 -0
- package/distribution/restore-drill/type.js +2 -0
- package/distribution/restore-drill/type.js.map +1 -0
- package/package.json +4 -1
package/README.md
CHANGED
|
@@ -49,7 +49,7 @@ value.
|
|
|
49
49
|
### `migration`: `migrate(database, migrations, { table = "schema_migration" })` → names applied
|
|
50
50
|
|
|
51
51
|
Applies, in order and in one transaction, the migrations not applied yet. Each is recorded with a SHA-256 of its
|
|
52
|
-
SQL. Throws `MigrationError` with one of these codes:
|
|
52
|
+
SQL, under a UUIDv7 `id` like every other row (a record table made before ids gets one). Throws `MigrationError` with one of these codes:
|
|
53
53
|
|
|
54
54
|
- `changed`: an applied migration was edited;
|
|
55
55
|
- `missing`: an applied migration was removed;
|
|
@@ -118,6 +118,37 @@ create table account (
|
|
|
118
118
|
|
|
119
119
|
Generate them in code with `uuidVersion7` from `@sharadox/web-library` when the id is needed before the insert.
|
|
120
120
|
|
|
121
|
+
## Backups and restore drills
|
|
122
|
+
|
|
123
|
+
A daily, encrypted copy of the production database in an S3 bucket nobody can empty, and a monthly proof that it
|
|
124
|
+
restores. The pieces, usable on their own:
|
|
125
|
+
|
|
126
|
+
- `dumpDatabase(url, { program?, run? })` (`backup-dump`): `pg_dump` of the public schema as plain INSERTs, without
|
|
127
|
+
owners or grants (needs `pg_dump` of the server's major version or later). The address goes in libpq's environment (`PGHOST`, `PGPASSWORD`…), never
|
|
128
|
+
on the command line. `restorableSql(dump)` drops psql's own commands (`\restrict`) so any Postgres can replay it.
|
|
129
|
+
- `encryptBackup(bytes, key)`, `decryptBackup(bytes, key)` (`backup-encryption`): AES-256-GCM under
|
|
130
|
+
`BACKUP_ENCRYPTION_KEY` (at least 32 characters, kept apart from the backups); any change is detected.
|
|
131
|
+
- `backupDatabase({ dump, counts, storage, key, now? })` (`database-backup`): gzip, encrypt, store
|
|
132
|
+
`<UUIDv7>.sql.gz.enc` and its manifest `<UUIDv7>.json` (the UUIDv7 carries the time) (time, size, SHA-256, rows per table).
|
|
133
|
+
`tableCounts(database)` gives the rows per table.
|
|
134
|
+
- `restoreDrill({ storage, key, into })` (`restore-drill`): the newest backup, checked against its SHA-256,
|
|
135
|
+
decrypted, replayed into an empty database (`createMemoryDatabase()`), and every table's rows compared with the
|
|
136
|
+
manifest → `{ ok, createdAt, tables: [{ table, expected, restored }] }`.
|
|
137
|
+
- `createBackupStorage({ bucket, region, prefix, retentionDays, credentials })` (`backup-storage`): S3, write, read and
|
|
138
|
+
list only; each file is stored with Object Lock in compliance mode for `retentionDays` (35 in the backup command), so
|
|
139
|
+
the application's own bucket (Object Lock enabled, no default retention) can hold backups under
|
|
140
|
+
`index/database/backup/`. `createMemoryBackupStorage()`
|
|
141
|
+
for tests.
|
|
142
|
+
- `gitHubWebIdentityToken()` (`git-hub-web-identity`): the GitHub Actions job's identity token for AWS, so the job
|
|
143
|
+
takes an IAM role with no stored key (the job needs `permissions: { id-token: write }`).
|
|
144
|
+
- `runBackupCommand("backup" | "drill")` (`backup-command`): what a site's workflow runs, reading `BACKUP_BUCKET`,
|
|
145
|
+
`BACKUP_REGION`, `BACKUP_PREFIX`, `BACKUP_ROLE`, `BACKUP_ENCRYPTION_KEY` and, for a backup,
|
|
146
|
+
`DATABASE_BACKUP_URL` (a read-only role). It returns the exit code:
|
|
147
|
+
|
|
148
|
+
```sh
|
|
149
|
+
node -e 'import("@sharadox/database-web-library/backup-command").then(async (m) => process.exit(await m.runBackupCommand("backup")))'
|
|
150
|
+
```
|
|
151
|
+
|
|
121
152
|
## License
|
|
122
153
|
|
|
123
154
|
Sharadox Open Source License 0.1.0 (see `LICENSE.md`).
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { BackupParts } from "@sharadox/database-web-library/backup-command/type";
|
|
2
|
+
/**
|
|
3
|
+
* Short-lived AWS credentials for `role`, taken with the GitHub Actions job's own identity (no stored key).
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* createBackupStorage({ bucket, region, prefix, retentionDays: 35, credentials: gitHubRoleCredentials(role) });
|
|
7
|
+
*/
|
|
8
|
+
export declare function gitHubRoleCredentials(role: string, environment?: Record<string, string | undefined>): () => Promise<import("@smithy/types").AwsCredentialIdentity>;
|
|
9
|
+
/** How long every backup stays locked: the bucket's lifecycle deletes backups a day later (36 days). */
|
|
10
|
+
export declare const BACKUP_RETENTION_DAYS = 35;
|
|
11
|
+
/** In a GitHub Actions job: S3 through the job's own identity (no stored key), Neon, and `pg_dump`. */
|
|
12
|
+
export declare const defaultBackupParts: BackupParts;
|
|
13
|
+
/**
|
|
14
|
+
* What a site's backup workflow runs: `backup` (the production database, dumped, encrypted and stored) or `drill`
|
|
15
|
+
* (the newest backup restored into Postgres in memory and checked). Settings: `BACKUP_BUCKET`, `BACKUP_REGION`,
|
|
16
|
+
* `BACKUP_PREFIX`, `BACKUP_ROLE` (the IAM role the job's GitHub identity takes; `BACKUP_PREFIX` is the application bucket's
|
|
17
|
+
* `index/database/backup`), `BACKUP_ENCRYPTION_KEY`, and for a
|
|
18
|
+
* backup `DATABASE_BACKUP_URL` (a read-only role). Returns the exit code.
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* // node -e 'import("@sharadox/database-web-library/backup-command").then(async (m) => process.exit(await m.runBackupCommand("backup")))'
|
|
22
|
+
*/
|
|
23
|
+
export declare function runBackupCommand(command: string, environment?: Record<string, string | undefined>, parts?: Partial<BackupParts>): Promise<number>;
|
|
24
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/backup-command/index.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,oDAAoD,CAAC;AAQtF;;;;;GAKG;AACH,wBAAgB,qBAAqB,CAAC,IAAI,EAAE,MAAM,EAAE,WAAW,GAAE,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,CAAe,gEAOhH;AAED,wGAAwG;AACxG,eAAO,MAAM,qBAAqB,KAAK,CAAC;AAExC,uGAAuG;AACvG,eAAO,MAAM,kBAAkB,EAAE,WAahC,CAAC;AAQF;;;;;;;;;GASG;AACH,wBAAsB,gBAAgB,CACpC,OAAO,EAAE,MAAM,EACf,WAAW,GAAE,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,CAAe,EAC7D,KAAK,GAAE,OAAO,CAAC,WAAW,CAAM,GAC/B,OAAO,CAAC,MAAM,CAAC,CAwCjB"}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { fromWebToken } from "@aws-sdk/credential-providers";
|
|
2
|
+
import { dumpDatabase } from "@sharadox/database-web-library/backup-dump";
|
|
3
|
+
import { createBackupStorage } from "@sharadox/database-web-library/backup-storage";
|
|
4
|
+
import { createDatabase } from "@sharadox/database-web-library/database";
|
|
5
|
+
import { backupDatabase, tableCounts } from "@sharadox/database-web-library/database-backup";
|
|
6
|
+
import { gitHubWebIdentityToken } from "@sharadox/database-web-library/git-hub-web-identity";
|
|
7
|
+
import { createMemoryDatabase } from "@sharadox/database-web-library/memory-database";
|
|
8
|
+
import { restoreDrill } from "@sharadox/database-web-library/restore-drill";
|
|
9
|
+
/**
|
|
10
|
+
* Short-lived AWS credentials for `role`, taken with the GitHub Actions job's own identity (no stored key).
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* createBackupStorage({ bucket, region, prefix, retentionDays: 35, credentials: gitHubRoleCredentials(role) });
|
|
14
|
+
*/
|
|
15
|
+
export function gitHubRoleCredentials(role, environment = process.env) {
|
|
16
|
+
return async () => fromWebToken({
|
|
17
|
+
roleArn: role,
|
|
18
|
+
webIdentityToken: await gitHubWebIdentityToken(environment),
|
|
19
|
+
roleSessionName: "backup",
|
|
20
|
+
})();
|
|
21
|
+
}
|
|
22
|
+
/** How long every backup stays locked: the bucket's lifecycle deletes backups a day later (36 days). */
|
|
23
|
+
export const BACKUP_RETENTION_DAYS = 35;
|
|
24
|
+
/** In a GitHub Actions job: S3 through the job's own identity (no stored key), Neon, and `pg_dump`. */
|
|
25
|
+
export const defaultBackupParts = {
|
|
26
|
+
storage: ({ bucket, region, prefix, role }) => createBackupStorage({
|
|
27
|
+
bucket,
|
|
28
|
+
region,
|
|
29
|
+
prefix,
|
|
30
|
+
retentionDays: BACKUP_RETENTION_DAYS,
|
|
31
|
+
credentials: gitHubRoleCredentials(role),
|
|
32
|
+
}),
|
|
33
|
+
database: (url) => createDatabase({ connectionString: url }),
|
|
34
|
+
dump: dumpDatabase,
|
|
35
|
+
memory: createMemoryDatabase,
|
|
36
|
+
log: (line) => console.log(line),
|
|
37
|
+
};
|
|
38
|
+
const required = (environment, name) => {
|
|
39
|
+
const value = environment[name];
|
|
40
|
+
if (!value)
|
|
41
|
+
throw new Error(`${name} is missing`);
|
|
42
|
+
return value;
|
|
43
|
+
};
|
|
44
|
+
/**
|
|
45
|
+
* What a site's backup workflow runs: `backup` (the production database, dumped, encrypted and stored) or `drill`
|
|
46
|
+
* (the newest backup restored into Postgres in memory and checked). Settings: `BACKUP_BUCKET`, `BACKUP_REGION`,
|
|
47
|
+
* `BACKUP_PREFIX`, `BACKUP_ROLE` (the IAM role the job's GitHub identity takes; `BACKUP_PREFIX` is the application bucket's
|
|
48
|
+
* `index/database/backup`), `BACKUP_ENCRYPTION_KEY`, and for a
|
|
49
|
+
* backup `DATABASE_BACKUP_URL` (a read-only role). Returns the exit code.
|
|
50
|
+
*
|
|
51
|
+
* @example
|
|
52
|
+
* // node -e 'import("@sharadox/database-web-library/backup-command").then(async (m) => process.exit(await m.runBackupCommand("backup")))'
|
|
53
|
+
*/
|
|
54
|
+
export async function runBackupCommand(command, environment = process.env, parts = {}) {
|
|
55
|
+
if (command !== "backup" && command !== "drill")
|
|
56
|
+
throw new Error(`"${command}": the command is backup or drill`);
|
|
57
|
+
const { storage, database, dump, memory, log } = { ...defaultBackupParts, ...parts };
|
|
58
|
+
const store = storage({
|
|
59
|
+
bucket: required(environment, "BACKUP_BUCKET"),
|
|
60
|
+
region: required(environment, "BACKUP_REGION"),
|
|
61
|
+
prefix: required(environment, "BACKUP_PREFIX"),
|
|
62
|
+
role: required(environment, "BACKUP_ROLE"),
|
|
63
|
+
});
|
|
64
|
+
const key = required(environment, "BACKUP_ENCRYPTION_KEY");
|
|
65
|
+
if (command === "backup") {
|
|
66
|
+
const url = required(environment, "DATABASE_BACKUP_URL");
|
|
67
|
+
const source = database(url);
|
|
68
|
+
try {
|
|
69
|
+
const result = await backupDatabase({
|
|
70
|
+
dump: () => dump(url),
|
|
71
|
+
counts: () => tableCounts(source),
|
|
72
|
+
storage: store,
|
|
73
|
+
key,
|
|
74
|
+
});
|
|
75
|
+
const rows = Object.entries(result.tables).map(([table, count]) => `${table} ${count}`);
|
|
76
|
+
log(`backed up ${result.file}: ${rows.join(", ")}`);
|
|
77
|
+
return 0;
|
|
78
|
+
}
|
|
79
|
+
finally {
|
|
80
|
+
await source.destroy();
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
const into = await memory();
|
|
84
|
+
try {
|
|
85
|
+
const result = await restoreDrill({ storage: store, key, into: into.database });
|
|
86
|
+
const wrong = result.tables.filter(({ expected, restored }) => expected !== restored);
|
|
87
|
+
log(result.ok
|
|
88
|
+
? `restore drill passed: the backup of ${result.createdAt}, ${result.tables.length} tables`
|
|
89
|
+
: `restore drill FAILED: ${wrong.map(({ table, expected, restored }) => `${table} ${restored} of ${expected}`).join(", ")}`);
|
|
90
|
+
return result.ok ? 0 : 1;
|
|
91
|
+
}
|
|
92
|
+
finally {
|
|
93
|
+
await into.close();
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/backup-command/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,+BAA+B,CAAC;AAC7D,OAAO,EAAE,YAAY,EAAE,MAAM,4CAA4C,CAAC;AAE1E,OAAO,EAAE,mBAAmB,EAAE,MAAM,+CAA+C,CAAC;AACpF,OAAO,EAAE,cAAc,EAAE,MAAM,yCAAyC,CAAC;AACzE,OAAO,EAAE,cAAc,EAAE,WAAW,EAAE,MAAM,gDAAgD,CAAC;AAC7F,OAAO,EAAE,sBAAsB,EAAE,MAAM,qDAAqD,CAAC;AAC7F,OAAO,EAAE,oBAAoB,EAAE,MAAM,gDAAgD,CAAC;AACtF,OAAO,EAAE,YAAY,EAAE,MAAM,8CAA8C,CAAC;AAE5E;;;;;GAKG;AACH,MAAM,UAAU,qBAAqB,CAAC,IAAY,EAAE,cAAkD,OAAO,CAAC,GAAG;IAC/G,OAAO,KAAK,IAAI,EAAE,CAChB,YAAY,CAAC;QACX,OAAO,EAAE,IAAI;QACb,gBAAgB,EAAE,MAAM,sBAAsB,CAAC,WAAW,CAAC;QAC3D,eAAe,EAAE,QAAQ;KAC1B,CAAC,EAAE,CAAC;AACT,CAAC;AAED,wGAAwG;AACxG,MAAM,CAAC,MAAM,qBAAqB,GAAG,EAAE,CAAC;AAExC,uGAAuG;AACvG,MAAM,CAAC,MAAM,kBAAkB,GAAgB;IAC7C,OAAO,EAAE,CAAC,EAAE,MAAM,EAAE,MAAM,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,EAAE,QAAQ,EAAE,QAAQ,EAAE,EAAE,EAAE,CAAC,QAAQ,KAAK,QAAQ,CAAC,CAAC;QACtF,GAAG,CACD,MAAM,CAAC,EAAE;YACP,CAAC,CAAC,uCAAuC,MAAM,CAAC,SAAS,KAAK,MAAM,CAAC,MAAM,CAAC,MAAM,SAAS;YAC3F,CAAC,CAAC,yBAAyB,KAAK,CAAC,GAAG,CAAC,CAAC,EAAE,KAAK,EAAE,QAAQ,EAAE,QAAQ,EAAE,EAAE,EAAE,CAAC,GAAG,KAAK,IAAI,QAAQ,OAAO,QAAQ,EAAE,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAC9H,CAAC;QACF,OAAO,MAAM,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;IAC3B,CAAC;YAAS,CAAC;QACT,MAAM,IAAI,CAAC,KAAK,EAAE,CAAC;IACrB,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { Kysely } from "kysely";
|
|
2
|
+
import type { BackupStorage } from "@sharadox/database-web-library/backup-storage/type";
|
|
3
|
+
import type { MemoryDatabase } from "@sharadox/database-web-library/memory-database/type";
|
|
4
|
+
/** Where a backup command's pieces come from (tests give their own). */
|
|
5
|
+
export interface BackupParts {
|
|
6
|
+
storage: (options: {
|
|
7
|
+
bucket: string;
|
|
8
|
+
region: string;
|
|
9
|
+
prefix: string;
|
|
10
|
+
role: string;
|
|
11
|
+
}) => BackupStorage;
|
|
12
|
+
database: (url: string) => Kysely<unknown>;
|
|
13
|
+
dump: (url: string) => Promise<Uint8Array>;
|
|
14
|
+
memory: () => Promise<MemoryDatabase<unknown>>;
|
|
15
|
+
log: (line: string) => void;
|
|
16
|
+
}
|
|
17
|
+
//# sourceMappingURL=type.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"type.d.ts","sourceRoot":"","sources":["../../source/backup-command/type.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AACrC,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,oDAAoD,CAAC;AACxF,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,qDAAqD,CAAC;AAE1F,wEAAwE;AACxE,MAAM,WAAW,WAAW;IAC1B,OAAO,EAAE,CAAC,OAAO,EAAE;QAAE,MAAM,EAAE,MAAM,CAAC;QAAC,MAAM,EAAE,MAAM,CAAC;QAAC,MAAM,EAAE,MAAM,CAAC;QAAC,IAAI,EAAE,MAAM,CAAA;KAAE,KAAK,aAAa,CAAC;IACtG,QAAQ,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,MAAM,CAAC,OAAO,CAAC,CAAC;IAC3C,IAAI,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,OAAO,CAAC,UAAU,CAAC,CAAC;IAC3C,MAAM,EAAE,MAAM,OAAO,CAAC,cAAc,CAAC,OAAO,CAAC,CAAC,CAAC;IAC/C,GAAG,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,IAAI,CAAC;CAC7B"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"type.js","sourceRoot":"","sources":["../../source/backup-command/type.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { DumpOptions } from "@sharadox/database-web-library/backup-dump/type";
|
|
2
|
+
/**
|
|
3
|
+
* A plain SQL dump of the database's public schema, made by `pg_dump`: one INSERT per row (so any Postgres, PGlite
|
|
4
|
+
* included, can replay it), without owners or grants. The address goes in libpq's environment (`PGHOST`, `PGUSER`,
|
|
5
|
+
* `PGPASSWORD`…), never on the command line, so the password isn't visible in the process list.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* const sql = await dumpDatabase(process.env.DATABASE_BACKUP_URL!);
|
|
9
|
+
*/
|
|
10
|
+
export declare function dumpDatabase(databaseUrl: string, { program, run }?: DumpOptions): Promise<Uint8Array>;
|
|
11
|
+
/**
|
|
12
|
+
* A dump without psql's own commands (`\restrict`, `\unrestrict`, which pg_dump 18 adds) and without creating the
|
|
13
|
+
* public schema (which every Postgres has) or setting Neon's internal column options, so it runs as plain SQL in an
|
|
14
|
+
* empty database.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* await sql.raw(restorableSql(text)).execute(database);
|
|
18
|
+
*/
|
|
19
|
+
export declare function restorableSql(dump: string): string;
|
|
20
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/backup-dump/index.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,WAAW,EAAiB,MAAM,iDAAiD,CAAC;AAalG;;;;;;;GAOG;AACH,wBAAsB,YAAY,CAChC,WAAW,EAAE,MAAM,EACnB,EAAE,OAAmB,EAAE,GAAgB,EAAE,GAAE,WAAgB,GAC1D,OAAO,CAAC,UAAU,CAAC,CAWrB;AASD;;;;;;;GAOG;AACH,wBAAgB,aAAa,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAKlD"}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { execFile } from "node:child_process";
|
|
2
|
+
const runProgram = (program, parameters, environment) => new Promise((resolve, reject) => {
|
|
3
|
+
execFile(program, parameters, { env: { ...process.env, ...environment }, encoding: "buffer", maxBuffer: 1024 * 1024 * 1024 }, (error, output) => error ? reject(new Error(`${program} failed: ${error.message}`)) : resolve(new Uint8Array(output)));
|
|
4
|
+
});
|
|
5
|
+
/**
|
|
6
|
+
* A plain SQL dump of the database's public schema, made by `pg_dump`: one INSERT per row (so any Postgres, PGlite
|
|
7
|
+
* included, can replay it), without owners or grants. The address goes in libpq's environment (`PGHOST`, `PGUSER`,
|
|
8
|
+
* `PGPASSWORD`…), never on the command line, so the password isn't visible in the process list.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* const sql = await dumpDatabase(process.env.DATABASE_BACKUP_URL!);
|
|
12
|
+
*/
|
|
13
|
+
export async function dumpDatabase(databaseUrl, { program = "pg_dump", run = runProgram } = {}) {
|
|
14
|
+
const url = new URL(databaseUrl);
|
|
15
|
+
return run(program, ["--schema=public", "--inserts", "--no-owner", "--no-privileges", "--quote-all-identifiers"], {
|
|
16
|
+
PGHOST: url.hostname,
|
|
17
|
+
PGPORT: url.port || "5432",
|
|
18
|
+
PGUSER: decodeURIComponent(url.username),
|
|
19
|
+
PGPASSWORD: decodeURIComponent(url.password),
|
|
20
|
+
PGDATABASE: url.pathname.slice(1),
|
|
21
|
+
// Neon always speaks TLS; without a mode in the address, the certificate is checked too.
|
|
22
|
+
PGSSLMODE: url.searchParams.get("sslmode") ?? "verify-full",
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Statements only the source could run: creating the public schema (every Postgres has it) and Neon's internal column
|
|
27
|
+
* option `pg_attver`, which other Postgres builds don't know.
|
|
28
|
+
*/
|
|
29
|
+
const SOURCE_ONLY = /^(CREATE SCHEMA "public";|COMMENT ON SCHEMA "public" IS .*;|ALTER TABLE ONLY .* SET \(pg_attver=\d+\);)$/;
|
|
30
|
+
/**
|
|
31
|
+
* A dump without psql's own commands (`\restrict`, `\unrestrict`, which pg_dump 18 adds) and without creating the
|
|
32
|
+
* public schema (which every Postgres has) or setting Neon's internal column options, so it runs as plain SQL in an
|
|
33
|
+
* empty database.
|
|
34
|
+
*
|
|
35
|
+
* @example
|
|
36
|
+
* await sql.raw(restorableSql(text)).execute(database);
|
|
37
|
+
*/
|
|
38
|
+
export function restorableSql(dump) {
|
|
39
|
+
return dump
|
|
40
|
+
.split("\n")
|
|
41
|
+
.filter((line) => !line.startsWith("\\") && !SOURCE_ONLY.test(line))
|
|
42
|
+
.join("\n");
|
|
43
|
+
}
|
|
44
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
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,UAAU,EAAE,GAAG,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC,CAAC;QACjC,yFAAyF;QACzF,SAAS,EAAE,GAAG,CAAC,YAAY,CAAC,GAAG,CAAC,SAAS,CAAC,IAAI,aAAa;KAC5D,CAAC,CAAC;AACL,CAAC;AAED;;;GAGG;AACH,MAAM,WAAW,GACf,0GAA0G,CAAC;AAE7G;;;;;;;GAOG;AACH,MAAM,UAAU,aAAa,CAAC,IAAY;IACxC,OAAO,IAAI;SACR,KAAK,CAAC,IAAI,CAAC;SACX,MAAM,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;SACnE,IAAI,CAAC,IAAI,CAAC,CAAC;AAChB,CAAC"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/** Runs a program with arguments and extra environment variables; resolves with what it printed. */
|
|
2
|
+
export type ProgramRunner = (program: string, parameters: string[], environment: Record<string, string | undefined>) => Promise<Uint8Array>;
|
|
3
|
+
export interface DumpOptions {
|
|
4
|
+
/** `pg_dump` by default (its major version must be the server's or later). */
|
|
5
|
+
program?: string;
|
|
6
|
+
/** How to run it (tests give their own). */
|
|
7
|
+
run?: ProgramRunner;
|
|
8
|
+
}
|
|
9
|
+
//# sourceMappingURL=type.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"type.d.ts","sourceRoot":"","sources":["../../source/backup-dump/type.ts"],"names":[],"mappings":"AAAA,oGAAoG;AACpG,MAAM,MAAM,aAAa,GAAG,CAC1B,OAAO,EAAE,MAAM,EACf,UAAU,EAAE,MAAM,EAAE,EACpB,WAAW,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,CAAC,KAC5C,OAAO,CAAC,UAAU,CAAC,CAAC;AAEzB,MAAM,WAAW,WAAW;IAC1B,8EAA8E;IAC9E,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,4CAA4C;IAC5C,GAAG,CAAC,EAAE,aAAa,CAAC;CACrB"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"type.js","sourceRoot":"","sources":["../../source/backup-dump/type.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Encrypts a backup with AES-256-GCM under a key kept apart from it (`BACKUP_ENCRYPTION_KEY`), so a copy of the
|
|
3
|
+
* bucket reveals nothing and any change is detected. Layout: `SHARADOX-BACKUP-1`, a 12-byte IV, the 16-byte tag,
|
|
4
|
+
* then the ciphertext.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* const sealed = encryptBackup(gzipSync(dump), process.env.BACKUP_ENCRYPTION_KEY!);
|
|
8
|
+
*/
|
|
9
|
+
export declare function encryptBackup(plain: Uint8Array, key: string): Uint8Array;
|
|
10
|
+
/**
|
|
11
|
+
* Decrypts what `encryptBackup` made; throws for a wrong key, a changed byte, or anything else.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* const plain = decryptBackup(sealed, process.env.BACKUP_ENCRYPTION_KEY!);
|
|
15
|
+
*/
|
|
16
|
+
export declare function decryptBackup(sealed: Uint8Array, key: string): Uint8Array;
|
|
17
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/backup-encryption/index.ts"],"names":[],"mappings":"AASA;;;;;;;GAOG;AACH,wBAAgB,aAAa,CAAC,KAAK,EAAE,UAAU,EAAE,GAAG,EAAE,MAAM,GAAG,UAAU,CAKxE;AAED;;;;;GAKG;AACH,wBAAgB,aAAa,CAAC,MAAM,EAAE,UAAU,EAAE,GAAG,EAAE,MAAM,GAAG,UAAU,CAQzE"}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { createCipheriv, createDecipheriv, createHash, randomBytes } from "node:crypto";
|
|
2
|
+
const MAGIC = new TextEncoder().encode("SHARADOX-BACKUP-1");
|
|
3
|
+
const keyBytes = (key) => {
|
|
4
|
+
if (key.length < 32)
|
|
5
|
+
throw new Error("the backup key must have at least 32 characters");
|
|
6
|
+
return createHash("sha256").update(key).digest();
|
|
7
|
+
};
|
|
8
|
+
/**
|
|
9
|
+
* Encrypts a backup with AES-256-GCM under a key kept apart from it (`BACKUP_ENCRYPTION_KEY`), so a copy of the
|
|
10
|
+
* bucket reveals nothing and any change is detected. Layout: `SHARADOX-BACKUP-1`, a 12-byte IV, the 16-byte tag,
|
|
11
|
+
* then the ciphertext.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* const sealed = encryptBackup(gzipSync(dump), process.env.BACKUP_ENCRYPTION_KEY!);
|
|
15
|
+
*/
|
|
16
|
+
export function encryptBackup(plain, key) {
|
|
17
|
+
const iv = randomBytes(12);
|
|
18
|
+
const cipher = createCipheriv("aes-256-gcm", keyBytes(key), iv);
|
|
19
|
+
const body = Buffer.concat([cipher.update(plain), cipher.final()]);
|
|
20
|
+
return new Uint8Array(Buffer.concat([MAGIC, iv, cipher.getAuthTag(), body]));
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Decrypts what `encryptBackup` made; throws for a wrong key, a changed byte, or anything else.
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* const plain = decryptBackup(sealed, process.env.BACKUP_ENCRYPTION_KEY!);
|
|
27
|
+
*/
|
|
28
|
+
export function decryptBackup(sealed, key) {
|
|
29
|
+
const bytes = Buffer.from(sealed);
|
|
30
|
+
if (!bytes.subarray(0, MAGIC.length).equals(Buffer.from(MAGIC)))
|
|
31
|
+
throw new Error("this is not a backup file");
|
|
32
|
+
const iv = bytes.subarray(MAGIC.length, MAGIC.length + 12);
|
|
33
|
+
const tag = bytes.subarray(MAGIC.length + 12, MAGIC.length + 28);
|
|
34
|
+
const decipher = createDecipheriv("aes-256-gcm", keyBytes(key), iv);
|
|
35
|
+
decipher.setAuthTag(tag);
|
|
36
|
+
return new Uint8Array(Buffer.concat([decipher.update(bytes.subarray(MAGIC.length + 28)), decipher.final()]));
|
|
37
|
+
}
|
|
38
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/backup-encryption/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,gBAAgB,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAExF,MAAM,KAAK,GAAG,IAAI,WAAW,EAAE,CAAC,MAAM,CAAC,mBAAmB,CAAC,CAAC;AAE5D,MAAM,QAAQ,GAAG,CAAC,GAAW,EAAE,EAAE;IAC/B,IAAI,GAAG,CAAC,MAAM,GAAG,EAAE;QAAE,MAAM,IAAI,KAAK,CAAC,iDAAiD,CAAC,CAAC;IACxF,OAAO,UAAU,CAAC,QAAQ,CAAC,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,MAAM,EAAE,CAAC;AACnD,CAAC,CAAC;AAEF;;;;;;;GAOG;AACH,MAAM,UAAU,aAAa,CAAC,KAAiB,EAAE,GAAW;IAC1D,MAAM,EAAE,GAAG,WAAW,CAAC,EAAE,CAAC,CAAC;IAC3B,MAAM,MAAM,GAAG,cAAc,CAAC,aAAa,EAAE,QAAQ,CAAC,GAAG,CAAC,EAAE,EAAE,CAAC,CAAC;IAChE,MAAM,IAAI,GAAG,MAAM,CAAC,MAAM,CAAC,CAAC,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC,EAAE,MAAM,CAAC,KAAK,EAAE,CAAC,CAAC,CAAC;IACnE,OAAO,IAAI,UAAU,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE,EAAE,MAAM,CAAC,UAAU,EAAE,EAAE,IAAI,CAAC,CAAC,CAAC,CAAC;AAC/E,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,aAAa,CAAC,MAAkB,EAAE,GAAW;IAC3D,MAAM,KAAK,GAAG,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;IAClC,IAAI,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAC,EAAE,KAAK,CAAC,MAAM,CAAC,CAAC,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;QAAE,MAAM,IAAI,KAAK,CAAC,2BAA2B,CAAC,CAAC;IAC9G,MAAM,EAAE,GAAG,KAAK,CAAC,QAAQ,CAAC,KAAK,CAAC,MAAM,EAAE,KAAK,CAAC,MAAM,GAAG,EAAE,CAAC,CAAC;IAC3D,MAAM,GAAG,GAAG,KAAK,CAAC,QAAQ,CAAC,KAAK,CAAC,MAAM,GAAG,EAAE,EAAE,KAAK,CAAC,MAAM,GAAG,EAAE,CAAC,CAAC;IACjE,MAAM,QAAQ,GAAG,gBAAgB,CAAC,aAAa,EAAE,QAAQ,CAAC,GAAG,CAAC,EAAE,EAAE,CAAC,CAAC;IACpE,QAAQ,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC;IACzB,OAAO,IAAI,UAAU,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,QAAQ,CAAC,MAAM,CAAC,KAAK,CAAC,QAAQ,CAAC,KAAK,CAAC,MAAM,GAAG,EAAE,CAAC,CAAC,EAAE,QAAQ,CAAC,KAAK,EAAE,CAAC,CAAC,CAAC,CAAC;AAC/G,CAAC"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { BackupStorage, BackupStorageOptions } from "@sharadox/database-web-library/backup-storage/type";
|
|
2
|
+
/**
|
|
3
|
+
* Backups in the application's S3 bucket, under its backup folder. Each file is stored with Object Lock in compliance
|
|
4
|
+
* mode for `retentionDays`, so nobody, not even the account's owner, can delete or change it before then (the
|
|
5
|
+
* bucket must have Object Lock enabled; no default retention is needed): this storage only writes, reads and lists.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* const storage = createBackupStorage({
|
|
9
|
+
* bucket: "luis-pires-decor-web-application",
|
|
10
|
+
* region: "us-east-1",
|
|
11
|
+
* prefix: "index/database/backup",
|
|
12
|
+
* retentionDays: 35,
|
|
13
|
+
* credentials,
|
|
14
|
+
* });
|
|
15
|
+
*/
|
|
16
|
+
export declare function createBackupStorage({ bucket, region, prefix, retentionDays, credentials, now, }: BackupStorageOptions): BackupStorage;
|
|
17
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/backup-storage/index.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,aAAa,EAAE,oBAAoB,EAAE,MAAM,oDAAoD,CAAC;AAE9G;;;;;;;;;;;;;GAaG;AACH,wBAAgB,mBAAmB,CAAC,EAClC,MAAM,EACN,MAAM,EACN,MAAM,EACN,aAAa,EACb,WAAW,EACX,GAAc,GACf,EAAE,oBAAoB,GAAG,aAAa,CAyBtC"}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { GetObjectCommand, ListObjectsV2Command, PutObjectCommand, S3Client } from "@aws-sdk/client-s3";
|
|
2
|
+
/**
|
|
3
|
+
* Backups in the application's S3 bucket, under its backup folder. Each file is stored with Object Lock in compliance
|
|
4
|
+
* mode for `retentionDays`, so nobody, not even the account's owner, can delete or change it before then (the
|
|
5
|
+
* bucket must have Object Lock enabled; no default retention is needed): this storage only writes, reads and lists.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* const storage = createBackupStorage({
|
|
9
|
+
* bucket: "luis-pires-decor-web-application",
|
|
10
|
+
* region: "us-east-1",
|
|
11
|
+
* prefix: "index/database/backup",
|
|
12
|
+
* retentionDays: 35,
|
|
13
|
+
* credentials,
|
|
14
|
+
* });
|
|
15
|
+
*/
|
|
16
|
+
export function createBackupStorage({ bucket, region, prefix, retentionDays, credentials, now = Date.now, }) {
|
|
17
|
+
const client = new S3Client({ region, credentials });
|
|
18
|
+
const at = (name) => `${prefix}/${name}`;
|
|
19
|
+
return {
|
|
20
|
+
put: async (name, bytes, contentType) => {
|
|
21
|
+
await client.send(new PutObjectCommand({
|
|
22
|
+
Bucket: bucket,
|
|
23
|
+
Key: at(name),
|
|
24
|
+
Body: bytes,
|
|
25
|
+
ContentType: contentType,
|
|
26
|
+
ObjectLockMode: "COMPLIANCE",
|
|
27
|
+
ObjectLockRetainUntilDate: new Date(now() + retentionDays * 86_400_000),
|
|
28
|
+
}));
|
|
29
|
+
},
|
|
30
|
+
get: async (name) => {
|
|
31
|
+
const object = await client.send(new GetObjectCommand({ Bucket: bucket, Key: at(name) }));
|
|
32
|
+
return object.Body.transformToByteArray();
|
|
33
|
+
},
|
|
34
|
+
list: async (start) => {
|
|
35
|
+
const listing = await client.send(new ListObjectsV2Command({ Bucket: bucket, Prefix: at(start) }));
|
|
36
|
+
return (listing.Contents ?? []).map(({ Key }) => Key.slice(prefix.length + 1)).sort();
|
|
37
|
+
},
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/backup-storage/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,oBAAoB,EAAE,gBAAgB,EAAE,QAAQ,EAAE,MAAM,oBAAoB,CAAC;AAGxG;;;;;;;;;;;;;GAaG;AACH,MAAM,UAAU,mBAAmB,CAAC,EAClC,MAAM,EACN,MAAM,EACN,MAAM,EACN,aAAa,EACb,WAAW,EACX,GAAG,GAAG,IAAI,CAAC,GAAG,GACO;IACrB,MAAM,MAAM,GAAG,IAAI,QAAQ,CAAC,EAAE,MAAM,EAAE,WAAW,EAAE,CAAC,CAAC;IACrD,MAAM,EAAE,GAAG,CAAC,IAAY,EAAE,EAAE,CAAC,GAAG,MAAM,IAAI,IAAI,EAAE,CAAC;IACjD,OA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interface BackupStorage {
|
|
3
|
+
put(name: string, bytes: Uint8Array, contentType: string): Promise<void>;
|
|
4
|
+
get(name: string): Promise<Uint8Array>;
|
|
5
|
+
/** The names under `prefix`, in name order (which is time order for backups). */
|
|
6
|
+
list(prefix: string): Promise<string[]>;
|
|
7
|
+
}
|
|
8
|
+
export interface BackupStorageOptions {
|
|
9
|
+
bucket: string;
|
|
10
|
+
region: string;
|
|
11
|
+
/** The backups' folder in the bucket (`index/database/backup`). */
|
|
12
|
+
prefix: string;
|
|
13
|
+
/** How many days each file stays locked (compliance mode) from the moment it's stored. */
|
|
14
|
+
retentionDays: number;
|
|
15
|
+
/** Short-lived AWS credentials (from the job's own identity, never a stored key). */
|
|
16
|
+
credentials: import("@aws-sdk/client-s3").S3ClientConfig["credentials"];
|
|
17
|
+
/** The clock, in milliseconds (tests). */
|
|
18
|
+
now?: () => number;
|
|
19
|
+
}
|
|
20
|
+
//# sourceMappingURL=type.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"type.d.ts","sourceRoot":"","sources":["../../source/backup-storage/type.ts"],"names":[],"mappings":"AAAA,qHAAqH;AACrH,MAAM,WAAW,aAAa;IAC5B,GAAG,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IACzE,GAAG,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,UAAU,CAAC,CAAC;IACvC,iFAAiF;IACjF,IAAI,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC,CAAC;CACzC;AAED,MAAM,WAAW,oBAAoB;IACnC,MAAM,EAAE,MAAM,CAAC;IACf,MAAM,EAAE,MAAM,CAAC;IACf,mEAAmE;IACnE,MAAM,EAAE,MAAM,CAAC;IACf,0FAA0F;IAC1F,aAAa,EAAE,MAAM,CAAC;IACtB,qFAAqF;IACrF,WAAW,EAAE,OAAO,oBAAoB,EAAE,cAAc,CAAC,aAAa,CAAC,CAAC;IACxE,0CAA0C;IAC1C,GAAG,CAAC,EAAE,MAAM,MAAM,CAAC;CACpB"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"type.js","sourceRoot":"","sources":["../../source/backup-storage/type.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { type Kysely } from "kysely";
|
|
2
|
+
import type { BackupOptions, BackupResult } from "@sharadox/database-web-library/database-backup/type";
|
|
3
|
+
/**
|
|
4
|
+
* Rows per table in the public schema, for the backup's manifest and the restore drill's check. Table names come
|
|
5
|
+
* from the catalog and are quoted as identifiers.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* await tableCounts(database); // { account: 12, contact: 14, … }
|
|
9
|
+
*/
|
|
10
|
+
export declare function tableCounts<Schema>(database: Kysely<Schema>): Promise<Record<string, number>>;
|
|
11
|
+
/**
|
|
12
|
+
* Makes one backup: the dump, compressed (gzip) and encrypted (AES-256-GCM), stored as
|
|
13
|
+
* `<UUIDv7>.sql.gz.enc` with a manifest beside it (`<UUIDv7>.json`: when, size, SHA-256 and rows per table); the
|
|
14
|
+
* UUIDv7 carries the time, so names sort in time order. The storage locks both, unchangeable, for its retention.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* await backupDatabase({ dump: () => dumpDatabase(url), counts: () => tableCounts(database), storage, key });
|
|
18
|
+
*/
|
|
19
|
+
export declare function backupDatabase({ dump, counts, storage, key, now, }: BackupOptions): Promise<BackupResult>;
|
|
20
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/database-backup/index.ts"],"names":[],"mappings":"AAEA,OAAO,EAAO,KAAK,MAAM,EAAE,MAAM,QAAQ,CAAC;AAE1C,OAAO,KAAK,EAAkB,aAAa,EAAE,YAAY,EAAE,MAAM,qDAAqD,CAAC;AAGvH;;;;;;GAMG;AACH,wBAAsB,WAAW,CAAC,MAAM,EAAE,QAAQ,EAAE,MAAM,CAAC,MAAM,CAAC,GAAG,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAYnG;AAED;;;;;;;GAOG;AACH,wBAAsB,cAAc,CAAC,EACnC,IAAI,EACJ,MAAM,EACN,OAAO,EACP,GAAG,EACH,GAAgB,GACjB,EAAE,aAAa,GAAG,OAAO,CAAC,YAAY,CAAC,CAgBvC"}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { gzipSync } from "node:zlib";
|
|
3
|
+
import { sql } from "kysely";
|
|
4
|
+
import { encryptBackup } from "@sharadox/database-web-library/backup-encryption";
|
|
5
|
+
import { v7 } from "uuid";
|
|
6
|
+
/**
|
|
7
|
+
* Rows per table in the public schema, for the backup's manifest and the restore drill's check. Table names come
|
|
8
|
+
* from the catalog and are quoted as identifiers.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* await tableCounts(database); // { account: 12, contact: 14, … }
|
|
12
|
+
*/
|
|
13
|
+
export async function tableCounts(database) {
|
|
14
|
+
const tables = await sql `select tablename as name from pg_tables
|
|
15
|
+
where schemaname = 'public' order by tablename`.execute(database);
|
|
16
|
+
const counts = {};
|
|
17
|
+
for (const { name } of tables.rows) {
|
|
18
|
+
// Schema-qualified: a replayed dump leaves the session with an empty search path.
|
|
19
|
+
const { rows } = await sql `select count(*) as count from ${sql.table(`public.${name}`)}`.execute(database);
|
|
20
|
+
counts[name] = Number(rows[0].count);
|
|
21
|
+
}
|
|
22
|
+
return counts;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Makes one backup: the dump, compressed (gzip) and encrypted (AES-256-GCM), stored as
|
|
26
|
+
* `<UUIDv7>.sql.gz.enc` with a manifest beside it (`<UUIDv7>.json`: when, size, SHA-256 and rows per table); the
|
|
27
|
+
* UUIDv7 carries the time, so names sort in time order. The storage locks both, unchangeable, for its retention.
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* await backupDatabase({ dump: () => dumpDatabase(url), counts: () => tableCounts(database), storage, key });
|
|
31
|
+
*/
|
|
32
|
+
export async function backupDatabase({ dump, counts, storage, key, now = Date.now(), }) {
|
|
33
|
+
const created = new Date(now);
|
|
34
|
+
const tables = await counts();
|
|
35
|
+
const sealed = encryptBackup(gzipSync(await dump()), key);
|
|
36
|
+
const name = v7({ msecs: now });
|
|
37
|
+
const file = `${name}.sql.gz.enc`;
|
|
38
|
+
const manifest = {
|
|
39
|
+
createdAt: created.toISOString(),
|
|
40
|
+
file,
|
|
41
|
+
bytes: sealed.length,
|
|
42
|
+
sha256: createHash("sha256").update(sealed).digest("hex"),
|
|
43
|
+
tables,
|
|
44
|
+
};
|
|
45
|
+
await storage.put(file, sealed, "application/octet-stream");
|
|
46
|
+
await storage.put(`${name}.json`, new TextEncoder().encode(JSON.stringify(manifest, null, 2)), "application/json");
|
|
47
|
+
return { file, manifest: `${name}.json`, tables };
|
|
48
|
+
}
|
|
49
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/database-backup/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACzC,OAAO,EAAE,QAAQ,EAAE,MAAM,WAAW,CAAC;AACrC,OAAO,EAAE,GAAG,EAAe,MAAM,QAAQ,CAAC;AAC1C,OAAO,EAAE,aAAa,EAAE,MAAM,kDAAkD,CAAC;AAEjF,OAAO,EAAE,EAAE,EAAE,MAAM,MAAM,CAAC;AAE1B;;;;;;GAMG;AACH,MAAM,CAAC,KAAK,UAAU,WAAW,CAAS,QAAwB;IAChE,MAAM,MAAM,GAAG,MAAM,GAAG,CAAkB;mDACO,CAAC,OAAO,CAAC,QAAQ,CAAC,CA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;IAC5D,MAAM,OAAO,CAAC,GAAG,CAAC,GAAG,IAAI,OAAO,EAAE,IAAI,WAAW,EAAE,CAAC,MAAM,CAAC,IAAI,CAAC,SAAS,CAAC,QAAQ,EAAE,IAAI,EAAE,CAAC,CAAC,CAAC,EAAE,kBAAkB,CAAC,CAAC;IACnH,OAAO,EAAE,IAAI,EAAE,QAAQ,EAAE,GAAG,IAAI,OAAO,EAAE,MAAM,EAAE,CAAC;AACpD,CAAC"}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { BackupStorage } from "@sharadox/database-web-library/backup-storage/type";
|
|
2
|
+
export interface BackupOptions {
|
|
3
|
+
/** Makes the plain SQL dump (`dumpDatabase`). */
|
|
4
|
+
dump: () => Promise<Uint8Array>;
|
|
5
|
+
/** Rows per table at the time of the dump (`tableCounts`), which the restore drill checks. */
|
|
6
|
+
counts: () => Promise<Record<string, number>>;
|
|
7
|
+
storage: BackupStorage;
|
|
8
|
+
/** `BACKUP_ENCRYPTION_KEY`: at least 32 characters, kept apart from the backups. */
|
|
9
|
+
key: string;
|
|
10
|
+
/** Milliseconds since 1970 (now by default). */
|
|
11
|
+
now?: number;
|
|
12
|
+
}
|
|
13
|
+
/** What a backup wrote: its file, and the rows it holds per table. */
|
|
14
|
+
export interface BackupResult {
|
|
15
|
+
file: string;
|
|
16
|
+
manifest: string;
|
|
17
|
+
tables: Record<string, number>;
|
|
18
|
+
}
|
|
19
|
+
/** Stored beside each backup file. */
|
|
20
|
+
export interface BackupManifest {
|
|
21
|
+
createdAt: string;
|
|
22
|
+
file: string;
|
|
23
|
+
bytes: number;
|
|
24
|
+
/** Of the encrypted file. */
|
|
25
|
+
sha256: string;
|
|
26
|
+
tables: Record<string, number>;
|
|
27
|
+
}
|
|
28
|
+
//# sourceMappingURL=type.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"type.d.ts","sourceRoot":"","sources":["../../source/database-backup/type.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,oDAAoD,CAAC;AAExF,MAAM,WAAW,aAAa;IAC5B,iDAAiD;IACjD,IAAI,EAAE,MAAM,OAAO,CAAC,UAAU,CAAC,CAAC;IAChC,8FAA8F;IAC9F,MAAM,EAAE,MAAM,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAAC;IAC9C,OAAO,EAAE,aAAa,CAAC;IACvB,oFAAoF;IACpF,GAAG,EAAE,MAAM,CAAC;IACZ,gDAAgD;IAChD,GAAG,CAAC,EAAE,MAAM,CAAC;CACd;AAED,sEAAsE;AACtE,MAAM,WAAW,YAAY;IAC3B,IAAI,EAAE,MAAM,CAAC;IACb,QAAQ,EAAE,MAAM,CAAC;IACjB,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;CAChC;AAED,sCAAsC;AACtC,MAAM,WAAW,cAAc;IAC7B,SAAS,EAAE,MAAM,CAAC;IAClB,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,EAAE,MAAM,CAAC;IACd,6BAA6B;IAC7B,MAAM,EAAE,MAAM,CAAC;IACf,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;CAChC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"type.js","sourceRoot":"","sources":["../../source/database-backup/type.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The GitHub Actions job's identity token for AWS (audience `sts.amazonaws.com`), to take an IAM role without any
|
|
3
|
+
* stored key. The job needs `permissions: { id-token: write }`.
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* const credentials = fromWebToken({ roleArn, webIdentityToken: await gitHubWebIdentityToken() });
|
|
7
|
+
*/
|
|
8
|
+
export declare function gitHubWebIdentityToken(environment?: Record<string, string | undefined>): Promise<string>;
|
|
9
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/git-hub-web-identity/index.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,wBAAsB,sBAAsB,CAC1C,WAAW,GAAE,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,CAAe,GAC5D,OAAO,CAAC,MAAM,CAAC,CAOjB"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The GitHub Actions job's identity token for AWS (audience `sts.amazonaws.com`), to take an IAM role without any
|
|
3
|
+
* stored key. The job needs `permissions: { id-token: write }`.
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* const credentials = fromWebToken({ roleArn, webIdentityToken: await gitHubWebIdentityToken() });
|
|
7
|
+
*/
|
|
8
|
+
export async function gitHubWebIdentityToken(environment = process.env) {
|
|
9
|
+
const url = environment.ACTIONS_ID_TOKEN_REQUEST_URL;
|
|
10
|
+
const token = environment.ACTIONS_ID_TOKEN_REQUEST_TOKEN;
|
|
11
|
+
if (!url || !token)
|
|
12
|
+
throw new Error("no GitHub Actions identity: the job needs permissions id-token: write");
|
|
13
|
+
const response = await fetch(`${url}&audience=sts.amazonaws.com`, { headers: { authorization: `Bearer ${token}` } });
|
|
14
|
+
if (!response.ok)
|
|
15
|
+
throw new Error(`GitHub refused the identity token (${response.status})`);
|
|
16
|
+
return (await response.json()).value;
|
|
17
|
+
}
|
|
18
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/git-hub-web-identity/index.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,MAAM,CAAC,KAAK,UAAU,sBAAsB,CAC1C,cAAkD,OAAO,CAAC,GAAG;IAE7D,MAAM,GAAG,GAAG,WAAW,CAAC,4BAA4B,CAAC;IACrD,MAAM,KAAK,GAAG,WAAW,CAAC,8BAA8B,CAAC;IACzD,IAAI,CAAC,GAAG,IAAI,CAAC,KAAK;QAAE,MAAM,IAAI,KAAK,CAAC,uEAAuE,CAAC,CAAC;IAC7G,MAAM,QAAQ,GAAG,MAAM,KAAK,CAAC,GAAG,GAAG,6BAA6B,EAAE,EAAE,OAAO,EAAE,EAAE,aAAa,EAAE,UAAU,KAAK,EAAE,EAAE,EAAE,CAAC,CAAC;IACrH,IAAI,CAAC,QAAQ,CAAC,EAAE;QAAE,MAAM,IAAI,KAAK,CAAC,sCAAsC,QAAQ,CAAC,MAAM,GAAG,CAAC,CAAC;IAC5F,OAAQ,CAAC,MAAM,QAAQ,CAAC,IAAI,EAAE,CAAuB,CAAC,KAAK,CAAC;AAC9D,CAAC"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { BackupStorage } from "@sharadox/database-web-library/backup-storage/type";
|
|
2
|
+
/**
|
|
3
|
+
* Backup storage in memory, for tests and trying the backup locally: `files` shows what was stored.
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* const storage = createMemoryBackupStorage();
|
|
7
|
+
* await backupDatabase({ dump, counts, storage, key });
|
|
8
|
+
*/
|
|
9
|
+
export declare function createMemoryBackupStorage(): BackupStorage & {
|
|
10
|
+
files: Map<string, Uint8Array>;
|
|
11
|
+
};
|
|
12
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/memory-backup-storage/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,oDAAoD,CAAC;AAExF;;;;;;GAMG;AACH,wBAAgB,yBAAyB,IAAI,aAAa,GAAG;IAAE,KAAK,EAAE,GAAG,CAAC,MAAM,EAAE,UAAU,CAAC,CAAA;CAAE,CAY9F"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Backup storage in memory, for tests and trying the backup locally: `files` shows what was stored.
|
|
3
|
+
*
|
|
4
|
+
* @example
|
|
5
|
+
* const storage = createMemoryBackupStorage();
|
|
6
|
+
* await backupDatabase({ dump, counts, storage, key });
|
|
7
|
+
*/
|
|
8
|
+
export function createMemoryBackupStorage() {
|
|
9
|
+
const files = new Map();
|
|
10
|
+
return {
|
|
11
|
+
files,
|
|
12
|
+
put: async (name, bytes) => void files.set(name, bytes),
|
|
13
|
+
get: async (name) => {
|
|
14
|
+
const bytes = files.get(name);
|
|
15
|
+
if (!bytes)
|
|
16
|
+
throw new Error(`no backup file "${name}"`);
|
|
17
|
+
return bytes;
|
|
18
|
+
},
|
|
19
|
+
list: async (prefix) => [...files.keys()].filter((name) => name.startsWith(prefix)).sort(),
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/memory-backup-storage/index.ts"],"names":[],"mappings":"AAEA;;;;;;GAMG;AACH,MAAM,UAAU,yBAAyB;IACvC,MAAM,KAAK,GAAG,IAAI,GAAG,EAAsB,CAAC;IAC5C,OAAO;QACL,KAAK;QACL,GAAG,EAAE,KAAK,EAAE,IAAI,EAAE,KAAK,EAAE,EAAE,CAAC,KAAK,KAAK,CAAC,GAAG,CAAC,IAAI,EAAE,KAAK,CAAC;QACvD,GAAG,EAAE,KAAK,EAAE,IAAI,EAAE,EAAE;YAClB,MAAM,KAAK,GAAG,KAAK,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;YAC9B,IAAI,CAAC,KAAK;gBAAE,MAAM,IAAI,KAAK,CAAC,mBAAmB,IAAI,GAAG,CAAC,CAAC;YACxD,OAAO,KAAK,CAAC;QACf,CAAC;QACD,IAAI,EAAE,KAAK,EAAE,MAAM,EAAE,EAAE,CAAC,CAAC,GAAG,KAAK,CAAC,IAAI,EAAE,CAAC,CAAC,MAAM,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC,CAAC,IAAI,EAAE;KAC3F,CAAC;AACJ,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/migration/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAO,KAAK,MAAM,EAAE,MAAM,QAAQ,CAAC;AAE1C,OAAO,KAAK,EAAE,cAAc,EAAE,SAAS,EAAE,MAAM,+CAA+C,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/migration/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAO,KAAK,MAAM,EAAE,MAAM,QAAQ,CAAC;AAE1C,OAAO,KAAK,EAAE,cAAc,EAAE,SAAS,EAAE,MAAM,+CAA+C,CAAC;AAmB/F;;;;;;;;GAQG;AACH,wBAAsB,OAAO,CAAC,MAAM,EAClC,QAAQ,EAAE,MAAM,CAAC,MAAM,CAAC,EACxB,UAAU,EAAE,SAAS,EAAE,EACvB,EAAE,KAA0B,EAAE,GAAE,cAAmB,GAClD,OAAO,CAAC,MAAM,EAAE,CAAC,CAuCnB"}
|
|
@@ -3,6 +3,18 @@ import { sql } from "kysely";
|
|
|
3
3
|
import { MigrationError } from "@sharadox/database-web-library/migration-error";
|
|
4
4
|
/** A table name `migrate` accepts: plain lowercase, so it never needs quoting. */
|
|
5
5
|
const IDENTIFIER = /^[a-z_][a-z0-9_]{0,62}$/;
|
|
6
|
+
/** Record tables made before every row had an id get one: a UUIDv7 primary key, with names kept unique. */
|
|
7
|
+
async function addRecordIds(transaction, table) {
|
|
8
|
+
const { rows } = await sql `select 1 from information_schema.columns
|
|
9
|
+
where table_schema = current_schema() and table_name = ${table} and column_name = ${"id"}`.execute(transaction);
|
|
10
|
+
if (rows.length > 0)
|
|
11
|
+
return;
|
|
12
|
+
const records = sql.table(table);
|
|
13
|
+
await sql `alter table ${records} drop constraint ${sql.id(`${table}_pkey`)}`.execute(transaction);
|
|
14
|
+
await sql `alter table ${records} add constraint ${sql.id(`${table}_name_key`)} unique (name)`.execute(transaction);
|
|
15
|
+
await sql `alter table ${records}
|
|
16
|
+
add column id uuid primary key default uuidv7() check (uuid_extract_version(id) = 7)`.execute(transaction);
|
|
17
|
+
}
|
|
6
18
|
const checksum = (text) => createHash("sha256").update(text).digest("hex");
|
|
7
19
|
/**
|
|
8
20
|
* Brings a database up to date: applies, in order, the migrations it hasn't applied yet, all in one transaction (all
|
|
@@ -20,10 +32,12 @@ export async function migrate(database, migrations, { table = "schema_migration"
|
|
|
20
32
|
return database.transaction().execute(async (transaction) => {
|
|
21
33
|
await sql `select pg_advisory_xact_lock(hashtext(${table}))`.execute(transaction);
|
|
22
34
|
await sql `create table if not exists ${records} (
|
|
23
|
-
|
|
35
|
+
id uuid primary key default uuidv7() check (uuid_extract_version(id) = 7),
|
|
36
|
+
name text not null unique,
|
|
24
37
|
checksum text not null,
|
|
25
38
|
applied_at timestamptz not null default now()
|
|
26
39
|
)`.execute(transaction);
|
|
40
|
+
await addRecordIds(transaction, table);
|
|
27
41
|
const applied = (await sql `select name, checksum from ${records} order by name`.execute(transaction)).rows;
|
|
28
42
|
const known = new Map(migrations.map((migration) => [migration.name, migration]));
|
|
29
43
|
for (const record of applied) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../source/migration/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACzC,OAAO,EAAE,GAAG,EAAe,MAAM,QAAQ,CAAC;AAC1C,OAAO,EAAE,cAAc,EAAE,MAAM,gDAAgD,CAAC;AAGhF,kFAAkF;AAClF,MAAM,UAAU,GAAG,yBAAyB,CAAC;AAE7C,MAAM,QAAQ,GAAG,CAAC,IAAY,EAAE,EAAE,CAAC,UAAU,CAAC,QAAQ,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,MAAM,CAAC,K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AAe,OAAO,6BAA6B,SAAS,CAAC,IAAI,KAAK,QAAQ,CAAC,SAAS,CAAC,GAAG,CAAC,GAAG,CAAC,OAAO,CAC/G,WAAW,CACZ,CAAC;QACJ,CAAC;QACD,OAAO,OAAO,CAAC,GAAG,CAAC,CAAC,EAAE,IAAI,EAAE,EAAE,EAAE,CAAC,IAAI,CAAC,CAAC;IACzC,CAAC,CAAC,CAAC;AACL,CAAC"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { RestoreDrillOptions, RestoreDrillResult } from "@sharadox/database-web-library/restore-drill/type";
|
|
2
|
+
/**
|
|
3
|
+
* Proves the newest backup can be restored: checks its SHA-256, decrypts and unpacks it, replays it into an empty
|
|
4
|
+
* Postgres, and compares every table's rows with its manifest. Run it monthly; a backup never restored is a hope.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* const memory = await createMemoryDatabase();
|
|
8
|
+
* const result = await restoreDrill({ storage, key, into: memory.database });
|
|
9
|
+
* if (!result.ok) process.exitCode = 1;
|
|
10
|
+
*/
|
|
11
|
+
export declare function restoreDrill({ storage, key, into }: RestoreDrillOptions): Promise<RestoreDrillResult>;
|
|
12
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/restore-drill/index.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,EAAE,mBAAmB,EAAE,kBAAkB,EAAE,MAAM,mDAAmD,CAAC;AAEjH;;;;;;;;GAQG;AACH,wBAAsB,YAAY,CAAC,EAAE,OAAO,EAAE,GAAG,EAAE,IAAI,EAAE,EAAE,mBAAmB,GAAG,OAAO,CAAC,kBAAkB,CAAC,CAiB3G"}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { gunzipSync } from "node:zlib";
|
|
3
|
+
import { sql } from "kysely";
|
|
4
|
+
import { decryptBackup } from "@sharadox/database-web-library/backup-encryption";
|
|
5
|
+
import { restorableSql } from "@sharadox/database-web-library/backup-dump";
|
|
6
|
+
import { tableCounts } from "@sharadox/database-web-library/database-backup";
|
|
7
|
+
/**
|
|
8
|
+
* Proves the newest backup can be restored: checks its SHA-256, decrypts and unpacks it, replays it into an empty
|
|
9
|
+
* Postgres, and compares every table's rows with its manifest. Run it monthly; a backup never restored is a hope.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* const memory = await createMemoryDatabase();
|
|
13
|
+
* const result = await restoreDrill({ storage, key, into: memory.database });
|
|
14
|
+
* if (!result.ok) process.exitCode = 1;
|
|
15
|
+
*/
|
|
16
|
+
export async function restoreDrill({ storage, key, into }) {
|
|
17
|
+
const manifests = (await storage.list("")).filter((name) => name.endsWith(".json"));
|
|
18
|
+
const newest = manifests.at(-1);
|
|
19
|
+
if (!newest)
|
|
20
|
+
throw new Error("there is no backup to restore");
|
|
21
|
+
const manifest = JSON.parse(new TextDecoder().decode(await storage.get(newest)));
|
|
22
|
+
const sealed = await storage.get(manifest.file);
|
|
23
|
+
if (createHash("sha256").update(sealed).digest("hex") !== manifest.sha256)
|
|
24
|
+
throw new Error(`${manifest.file} doesn't match its checksum`);
|
|
25
|
+
const dump = new TextDecoder().decode(gunzipSync(decryptBackup(sealed, key)));
|
|
26
|
+
// A dump pg_dump made from our own database: replayed as it is, into a database used for nothing else.
|
|
27
|
+
// eslint-disable-next-line @sharadox/database/no-raw-sql
|
|
28
|
+
await sql.raw(restorableSql(dump)).execute(into);
|
|
29
|
+
const restored = await tableCounts(into);
|
|
30
|
+
const tables = Object.entries(manifest.tables)
|
|
31
|
+
.sort(([first], [second]) => first.localeCompare(second))
|
|
32
|
+
.map(([table, expected]) => ({ table, expected, restored: restored[table] ?? 0 }));
|
|
33
|
+
return { ok: tables.every(({ expected, restored }) => expected === restored), createdAt: manifest.createdAt, tables };
|
|
34
|
+
}
|
|
35
|
+
//# sourceMappingURL=index.js.map
|
|
@@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,6BAA6B,CAAC,CAAC;IACjE,MAAM,IAAI,GAAG,IAAI,WAAW,EAAE,CAAC,MAAM,CAAC,UAAU,CAAC,aAAa,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC,CAAC,CAAC;IAC9E,uGAAuG;IACvG,yDAAyD;IACzD,MAAM,GAAG,CAAC,GAAG,CAAC,aAAa,CAAC,IAAI,CAAC,CAAC,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;IACjD,MAAM,QAAQ,GAAG,MAAM,WAAW,CAAC,IAAI,CAAC,CAAC;IACzC,MAAM,MAAM,GAAG,MAAM,CAAC,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAC;SAC3C,IAAI,CAAC,CAAC,CAAC,KAAK,CAAC,EAAE,CAAC,MAAM,CAAC,EAAE,EAAE,CAAC,KAAK,CAAC,aAAa,CAAC,MAAM,CAAC,CAAC;SACxD,GAAG,CAAC,CAAC,CAAC,KAAK,EAAE,QAAQ,CAAC,EAAE,EAAE,CAAC,CAAC,EAAE,KAAK,EAAE,QAAQ,EAAE,QAAQ,EAAE,QAAQ,CAAC,KAAK,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC,CAAC;IACrF,OAAO,EAAE,EAAE,EAAE,MAAM,CAAC,KAAK,CAAC,CAAC,EAAE,QAAQ,EAAE,QAAQ,EAAE,EAAE,EAAE,CAAC,QAAQ,KAAK,QAAQ,CAAC,EAAE,SAAS,EAAE,QAAQ,CAAC,SAAS,EAAE,MAAM,EAAE,CAAC;AACxH,CAAC"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { Kysely } from "kysely";
|
|
2
|
+
import type { BackupStorage } from "@sharadox/database-web-library/backup-storage/type";
|
|
3
|
+
export interface RestoreDrillOptions {
|
|
4
|
+
storage: BackupStorage;
|
|
5
|
+
/** `BACKUP_ENCRYPTION_KEY`. */
|
|
6
|
+
key: string;
|
|
7
|
+
/** An empty database to restore into (`createMemoryDatabase()`: Postgres 18 in memory). */
|
|
8
|
+
into: Kysely<unknown>;
|
|
9
|
+
}
|
|
10
|
+
/** Whether the newest backup came back whole: every table with the rows its manifest promised. */
|
|
11
|
+
export interface RestoreDrillResult {
|
|
12
|
+
ok: boolean;
|
|
13
|
+
createdAt: string;
|
|
14
|
+
tables: {
|
|
15
|
+
table: string;
|
|
16
|
+
expected: number;
|
|
17
|
+
restored: number;
|
|
18
|
+
}[];
|
|
19
|
+
}
|
|
20
|
+
//# sourceMappingURL=type.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"type.d.ts","sourceRoot":"","sources":["../../source/restore-drill/type.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AACrC,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,oDAAoD,CAAC;AAExF,MAAM,WAAW,mBAAmB;IAClC,OAAO,EAAE,aAAa,CAAC;IACvB,+BAA+B;IAC/B,GAAG,EAAE,MAAM,CAAC;IACZ,2FAA2F;IAC3F,IAAI,EAAE,MAAM,CAAC,OAAO,CAAC,CAAC;CACvB;AAED,kGAAkG;AAClG,MAAM,WAAW,kBAAkB;IACjC,EAAE,EAAE,OAAO,CAAC;IACZ,SAAS,EAAE,MAAM,CAAC;IAClB,MAAM,EAAE;QAAE,KAAK,EAAE,MAAM,CAAC;QAAC,QAAQ,EAAE,MAAM,CAAC;QAAC,QAAQ,EAAE,MAAM,CAAA;KAAE,EAAE,CAAC;CACjE"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"type.js","sourceRoot":"","sources":["../../source/restore-drill/type.ts"],"names":[],"mappings":""}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sharadox/database-web-library",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0-development.17",
|
|
4
4
|
"description": "Typed, injection-safe Postgres for web applications: queries, migrations, backups and restore drills.",
|
|
5
5
|
"private": false,
|
|
6
6
|
"license": "SEE LICENSE IN LICENCA.md",
|
|
@@ -40,9 +40,12 @@
|
|
|
40
40
|
"*.{json,md,yml,yaml}": "prettier --write"
|
|
41
41
|
},
|
|
42
42
|
"dependencies": {
|
|
43
|
+
"@aws-sdk/client-s3": "^3.1177.0",
|
|
44
|
+
"@aws-sdk/credential-providers": "^3.1177.0",
|
|
43
45
|
"@neondatabase/serverless": "^1.2.0",
|
|
44
46
|
"@sharadox/web-library": "^0.11.1",
|
|
45
47
|
"kysely": "^0.28.19",
|
|
48
|
+
"uuid": "^14.0.3",
|
|
46
49
|
"zod": "^4.6.7"
|
|
47
50
|
},
|
|
48
51
|
"devDependencies": {
|