backupdrill 0.1.3 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +12 -0
- package/dist/drill.d.ts +17 -1
- package/dist/drill.js +71 -5
- package/dist/index.js +5 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -6,6 +6,8 @@ Backups stream through the CLI on your machine (or your CI) straight to your buc
|
|
|
6
6
|
|
|
7
7
|
> Want this on a schedule, with **restore drills that prove your backup actually restores**, plus alerts and reports? That's the hosted service → **[backupdrill.com](https://backupdrill.com)**. This CLI is the DIY layer; the cloud adds scheduling, drills, alerting, and reports on top.
|
|
8
8
|
|
|
9
|
+
🎥 Prefer watching? [Where do your backups actually go — explained in about a minute](https://backupdrill.com/guides/test-supabase-backup-restore#explainer-video).
|
|
10
|
+
|
|
9
11
|
## Why
|
|
10
12
|
|
|
11
13
|
- Supabase's own database restore only brings back `storage.objects` **metadata**, not your actual Storage files.
|
|
@@ -146,6 +148,16 @@ backupdrill drill --verify-all-files # also checksum every Storage file, not jus
|
|
|
146
148
|
|
|
147
149
|
Backups record your installed Postgres extensions in the manifest, and the drill pre-installs them in the sandbox before restoring — including switching to a pgvector-enabled sandbox image when your database uses `vector` columns.
|
|
148
150
|
|
|
151
|
+
### Run your own checks against the restored copy
|
|
152
|
+
|
|
153
|
+
The drill's structural checks (tables, rows, checksums) can't know what "working" means for *your* app — RLS behavior and business workflows only have answers in your code. `--check-cmd` hands you the restored sandbox before it's destroyed:
|
|
154
|
+
|
|
155
|
+
```bash
|
|
156
|
+
backupdrill drill --check-cmd "npm run smoke" # BACKUPDRILL_SANDBOX_URL points at the restored copy
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
Your command runs after all structural checks pass, with `BACKUPDRILL_SANDBOX_URL` set to the sandbox's connection string. Exit 0 means pass; the result shows up in the report as an `app checks` line, and a non-zero exit fails the drill (the report keeps structural and app-level failures clearly separate). Entirely optional — without the flag, nothing changes and the report has no `app checks` line. There's a 10-minute timeout, and `--keep` leaves the sandbox container running when a drill fails so you can inspect it (`docker rm -f <id>` when done).
|
|
160
|
+
|
|
149
161
|
When you actually need to recover, `restore` puts the database back into a target you name and pulls the Storage files down to a local folder:
|
|
150
162
|
|
|
151
163
|
```bash
|
package/dist/drill.d.ts
CHANGED
|
@@ -33,14 +33,30 @@ export declare function classifyPostDataErrors(stderr: string): PostDataResult;
|
|
|
33
33
|
* 归档器致命错误等)绝不能谎报"全部恢复"——记为通用失败。
|
|
34
34
|
*/
|
|
35
35
|
export declare function postDataResult(code: number | null, stderr: string): PostDataResult;
|
|
36
|
+
export declare const APP_CHECK_TIMEOUT_MS: number;
|
|
37
|
+
/**
|
|
38
|
+
* 语义层钩子:结构检查全绿后、销毁沙箱前,把沙箱连接串经 BACKUPDRILL_SANDBOX_URL
|
|
39
|
+
* 交给用户命令执行(RLS 行为、业务流可用性——判据只存在于应用侧,引擎不解释
|
|
40
|
+
* 命令输出,只裁决退出码:0 = pass)。
|
|
41
|
+
* 子进程输出定向到 stderr:stdout 是 CLI 的机器可读 JSON 通道,不能被污染。
|
|
42
|
+
* 红线:托管 worker 绝不能把用户输入传到这里(任意代码执行);此钩子只属于
|
|
43
|
+
* 用户在自己机器上运行的 CLI(见 PRD §1.4 演练后应用自检钩子)。
|
|
44
|
+
*/
|
|
45
|
+
export declare function runAppCheck(command: string, connString: string, timeoutMs?: number): Promise<DrillCheck>;
|
|
36
46
|
/**
|
|
37
47
|
* 演练核心:把一份本地 dump 恢复进临时 Postgres 并对照 manifest 校验,然后销毁容器。
|
|
38
48
|
* 与 S3 解耦,便于离线测试。`preChecks` 用于把下载阶段的校验(如归档校验和)带进报告。
|
|
39
49
|
*/
|
|
40
|
-
export declare function drillDump(dumpPath: string, manifest: Manifest, snapshot: string, preChecks?: DrillCheck[]
|
|
50
|
+
export declare function drillDump(dumpPath: string, manifest: Manifest, snapshot: string, preChecks?: DrillCheck[], opts?: {
|
|
51
|
+
appCheckCommand?: string;
|
|
52
|
+
keepSandboxOnFailure?: boolean;
|
|
53
|
+
}): Promise<DrillReport>;
|
|
41
54
|
/** 演练一份快照:从桶里下载→drillDump(含 Storage 校验)→报告。 */
|
|
42
55
|
export declare function runDrill(config: BackupConfig, opts?: {
|
|
43
56
|
snapshot?: string;
|
|
44
57
|
verifyAllFiles?: boolean;
|
|
58
|
+
/** CLI 专用。红线:托管 worker 绝不能传入(在 worker 上执行用户命令 = RCE)。 */
|
|
59
|
+
appCheckCommand?: string;
|
|
60
|
+
keepSandboxOnFailure?: boolean;
|
|
45
61
|
}): Promise<DrillReport>;
|
|
46
62
|
export {};
|
package/dist/drill.js
CHANGED
|
@@ -308,14 +308,63 @@ async function verifyStorage(s3, config, manifest, verifyAll) {
|
|
|
308
308
|
: `${mismatches.length} failed in ${scope}: ${mismatches.slice(0, 5).join(", ")}`,
|
|
309
309
|
};
|
|
310
310
|
}
|
|
311
|
+
// ── 应用自检钩子(语义层)────────────────────────────────────────────
|
|
312
|
+
// 挂死的 smoke test 不能永远挡住沙箱销毁;10 分钟对"冒烟"级检查绰绰有余
|
|
313
|
+
export const APP_CHECK_TIMEOUT_MS = 10 * 60 * 1000;
|
|
314
|
+
/**
|
|
315
|
+
* 语义层钩子:结构检查全绿后、销毁沙箱前,把沙箱连接串经 BACKUPDRILL_SANDBOX_URL
|
|
316
|
+
* 交给用户命令执行(RLS 行为、业务流可用性——判据只存在于应用侧,引擎不解释
|
|
317
|
+
* 命令输出,只裁决退出码:0 = pass)。
|
|
318
|
+
* 子进程输出定向到 stderr:stdout 是 CLI 的机器可读 JSON 通道,不能被污染。
|
|
319
|
+
* 红线:托管 worker 绝不能把用户输入传到这里(任意代码执行);此钩子只属于
|
|
320
|
+
* 用户在自己机器上运行的 CLI(见 PRD §1.4 演练后应用自检钩子)。
|
|
321
|
+
*/
|
|
322
|
+
export function runAppCheck(command, connString, timeoutMs = APP_CHECK_TIMEOUT_MS) {
|
|
323
|
+
const started = Date.now();
|
|
324
|
+
return new Promise((resolve) => {
|
|
325
|
+
const proc = spawn(command, {
|
|
326
|
+
shell: true,
|
|
327
|
+
stdio: ["ignore", 2, 2],
|
|
328
|
+
env: { ...process.env, BACKUPDRILL_SANDBOX_URL: connString },
|
|
329
|
+
timeout: timeoutMs,
|
|
330
|
+
killSignal: "SIGKILL",
|
|
331
|
+
});
|
|
332
|
+
proc.on("error", (error) => {
|
|
333
|
+
resolve({
|
|
334
|
+
name: "app checks",
|
|
335
|
+
pass: false,
|
|
336
|
+
detail: `command failed to start: ${error.message}`,
|
|
337
|
+
});
|
|
338
|
+
});
|
|
339
|
+
proc.on("close", (code, signal) => {
|
|
340
|
+
const seconds = Math.round((Date.now() - started) / 100) / 10;
|
|
341
|
+
if (signal) {
|
|
342
|
+
resolve({
|
|
343
|
+
name: "app checks",
|
|
344
|
+
pass: false,
|
|
345
|
+
detail: `terminated by ${signal} after ${seconds}s (timeout ${timeoutMs / 1000}s)`,
|
|
346
|
+
});
|
|
347
|
+
return;
|
|
348
|
+
}
|
|
349
|
+
resolve({
|
|
350
|
+
name: "app checks",
|
|
351
|
+
pass: code === 0,
|
|
352
|
+
detail: code === 0
|
|
353
|
+
? `command exited 0 in ${seconds}s`
|
|
354
|
+
: `command exited ${code} after ${seconds}s`,
|
|
355
|
+
});
|
|
356
|
+
});
|
|
357
|
+
});
|
|
358
|
+
}
|
|
311
359
|
// ── 主流程 ──────────────────────────────────────────────────────────
|
|
312
360
|
/**
|
|
313
361
|
* 演练核心:把一份本地 dump 恢复进临时 Postgres 并对照 manifest 校验,然后销毁容器。
|
|
314
362
|
* 与 S3 解耦,便于离线测试。`preChecks` 用于把下载阶段的校验(如归档校验和)带进报告。
|
|
315
363
|
*/
|
|
316
|
-
export async function drillDump(dumpPath, manifest, snapshot, preChecks = []) {
|
|
364
|
+
export async function drillDump(dumpPath, manifest, snapshot, preChecks = [], opts = {}) {
|
|
317
365
|
const checks = [...preChecks];
|
|
318
366
|
const pg = await startEphemeralPostgres(sandboxImage(manifest));
|
|
367
|
+
let pass = false; // 异常路径视为失败,供 finally 决定 --keep 是否保留沙箱
|
|
319
368
|
try {
|
|
320
369
|
// 旧 manifest(≤0.1.1)没有 extensions 字段 → 不装任何扩展,行为与从前一致
|
|
321
370
|
const extensions = manifest.database.extensions ?? [];
|
|
@@ -363,9 +412,16 @@ export async function drillDump(dumpPath, manifest, snapshot, preChecks = []) {
|
|
|
363
412
|
});
|
|
364
413
|
const verify = await verifyRestored(pg.connString, manifest);
|
|
365
414
|
checks.push(...verify.checks);
|
|
415
|
+
// 语义层钩子:只在结构层全绿时才跑——结构已失败时再跑应用检查,只会把
|
|
416
|
+
// 一次失败报成两层失败,误导排查方向。没配置则不跑、报告里不出现该行。
|
|
417
|
+
if (opts.appCheckCommand && checks.every((c) => c.pass)) {
|
|
418
|
+
log.step("Running app checks…");
|
|
419
|
+
checks.push(await runAppCheck(opts.appCheckCommand, pg.connString));
|
|
420
|
+
}
|
|
421
|
+
pass = checks.every((c) => c.pass);
|
|
366
422
|
return {
|
|
367
423
|
snapshot,
|
|
368
|
-
pass
|
|
424
|
+
pass,
|
|
369
425
|
checks,
|
|
370
426
|
restoreSeconds,
|
|
371
427
|
restoredTableCount: verify.tableCount,
|
|
@@ -373,8 +429,15 @@ export async function drillDump(dumpPath, manifest, snapshot, preChecks = []) {
|
|
|
373
429
|
};
|
|
374
430
|
}
|
|
375
431
|
finally {
|
|
376
|
-
|
|
377
|
-
|
|
432
|
+
if (opts.keepSandboxOnFailure && !pass) {
|
|
433
|
+
// --rm 容器不 docker rm 就一直活着;用户排查完 stop/rm 时 --rm 自动清理
|
|
434
|
+
log.warn(`--keep: sandbox left running for inspection — ${pg.connString} ` +
|
|
435
|
+
`(remove with: docker rm -f ${pg.containerId.slice(0, 12)})`);
|
|
436
|
+
}
|
|
437
|
+
else {
|
|
438
|
+
log.step("Tearing down ephemeral Postgres…");
|
|
439
|
+
await execFileAsync("docker", ["rm", "-f", pg.containerId]).catch(() => { });
|
|
440
|
+
}
|
|
378
441
|
}
|
|
379
442
|
}
|
|
380
443
|
/** 演练一份快照:从桶里下载→drillDump(含 Storage 校验)→报告。 */
|
|
@@ -415,7 +478,10 @@ export async function runDrill(config, opts = {}) {
|
|
|
415
478
|
log.step("Verifying Storage files…");
|
|
416
479
|
preChecks.push(await verifyStorage(s3, config, manifest, opts.verifyAllFiles ?? false));
|
|
417
480
|
}
|
|
418
|
-
return await drillDump(dumpPath, manifest, snapshot, preChecks
|
|
481
|
+
return await drillDump(dumpPath, manifest, snapshot, preChecks, {
|
|
482
|
+
appCheckCommand: opts.appCheckCommand,
|
|
483
|
+
keepSandboxOnFailure: opts.keepSandboxOnFailure,
|
|
484
|
+
});
|
|
419
485
|
}
|
|
420
486
|
finally {
|
|
421
487
|
await rm(workdir, { recursive: true, force: true }).catch(() => { });
|
package/dist/index.js
CHANGED
|
@@ -75,6 +75,9 @@ program
|
|
|
75
75
|
.option("--prefix <prefix>", "key prefix inside the bucket (default: backupdrill)")
|
|
76
76
|
.option("--project-name <name>", "project name used in the object key")
|
|
77
77
|
.option("--verify-all-files", "checksum every Storage file, not just a sample")
|
|
78
|
+
.option("--check-cmd <command>", "after structural checks pass, run your own smoke test against the restored sandbox " +
|
|
79
|
+
"(connection string in BACKUPDRILL_SANDBOX_URL; exit 0 = pass, reported as 'app checks')")
|
|
80
|
+
.option("--keep", "if the drill fails, keep the sandbox container running for inspection")
|
|
78
81
|
.action(async (opts) => {
|
|
79
82
|
try {
|
|
80
83
|
const config = await loadConfig({
|
|
@@ -94,6 +97,8 @@ program
|
|
|
94
97
|
const report = await runDrill(config, {
|
|
95
98
|
snapshot: opts.snapshot,
|
|
96
99
|
verifyAllFiles: opts.verifyAllFiles,
|
|
100
|
+
appCheckCommand: opts.checkCmd,
|
|
101
|
+
keepSandboxOnFailure: opts.keep,
|
|
97
102
|
});
|
|
98
103
|
console.error("");
|
|
99
104
|
for (const c of report.checks) {
|
package/package.json
CHANGED