backupdrill 2.11.2 → 2.12.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 +10 -0
- package/dist/backup.d.ts +3 -0
- package/dist/backup.js +53 -23
- package/dist/dir-store.d.ts +55 -0
- package/dist/dir-store.js +208 -0
- package/dist/drill.js +24 -3
- package/dist/index.js +60 -17
- package/dist/lib.d.ts +7 -2
- package/dist/lib.js +6 -1
- package/dist/manifest.d.ts +23 -0
- package/dist/manifest.js +39 -0
- package/dist/ping.d.ts +5 -0
- package/dist/ping.js +20 -0
- package/dist/recovery-doc.js +7 -2
- package/dist/restore.js +26 -8
- package/dist/snapshot-store.d.ts +19 -1
- package/dist/snapshot-store.js +17 -1
- package/dist/storage-restore.d.ts +2 -6
- package/dist/storage-restore.js +6 -13
- package/dist/storage.d.ts +81 -8
- package/dist/storage.js +456 -52
- package/package.json +1 -1
- package/plugin/README.md +2 -2
- package/plugin/skills/supabase-backup/SKILL.md +6 -6
- package/plugin/skills/supabase-backup/assets/scheduled-backup.yml +1 -1
package/dist/lib.js
CHANGED
|
@@ -17,4 +17,9 @@ export { runEstimate, project } from "./estimate.js";
|
|
|
17
17
|
export { loadConfig, requireBucket } from "./config.js";
|
|
18
18
|
export { targetClient, resolveSnapshot, getObjectText, downloadToFile, hashObject, } from "./snapshots.js";
|
|
19
19
|
// 快照位置接口:备份、演练、恢复都可以传入自己的实现(托管 worker 的 Google Drive 目标)
|
|
20
|
-
export { s3SnapshotStore } from "./snapshot-store.js";
|
|
20
|
+
export { s3SnapshotStore, s3StorageSource } from "./snapshot-store.js";
|
|
21
|
+
// 2.12.0:下载到本地的快照(托管存储下载脚本的产出)与 restore --from-dir
|
|
22
|
+
export { prepareDownloadedSnapshot, parseDownloadIndex, DOWNLOAD_INDEX_FILE } from "./dir-store.js";
|
|
23
|
+
// 2.12.0:Storage 复制的时间预算、停滞中止、写请求计数(runBackup 的 opts.storage)
|
|
24
|
+
export { StorageSyncError, STORAGE_KEY_MAX_BYTES, STORAGE_STALL_MS, describeStorageError } from "./storage.js";
|
|
25
|
+
export { STORAGE_NOT_COPIED_REASONS } from "./manifest.js";
|
package/dist/manifest.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { StorageNotCopiedReason } from "./storage.js";
|
|
1
2
|
import { type GrantedObject } from "./grants.js";
|
|
2
3
|
/**
|
|
3
4
|
* manifest.json —— 每次快照的自描述清单。
|
|
@@ -54,6 +55,8 @@ export interface ExtensionInfo {
|
|
|
54
55
|
* - (仍是 2)2.9.0 加 database.grants:可加字段,旧读取端忽略它、照旧恢复,不升版本,
|
|
55
56
|
* 免得还没升级的 CLI 和 worker 拒读新快照。
|
|
56
57
|
* - (仍是 2)2.10.0 加 auth:同样是可加字段。旧读取端忽略它和 auth.dump,演练和恢复与之前完全一样。
|
|
58
|
+
* - (仍是 2)2.12.0 加 storage.vanished / storage.partial / storageFailure:可加字段。旧读取端把部分
|
|
59
|
+
* 复制的快照当成"文件清单就是这些",恢复与演练照旧只处理清单里的文件。
|
|
57
60
|
*/
|
|
58
61
|
export declare const MANIFEST_SCHEMA_VERSION = 2;
|
|
59
62
|
export interface Manifest {
|
|
@@ -85,6 +88,24 @@ export interface Manifest {
|
|
|
85
88
|
fileCount: number;
|
|
86
89
|
totalBytes: number;
|
|
87
90
|
files: StorageFile[];
|
|
91
|
+
/** 2.12.0 起:列出时在、复制时已被删的文件(不算缺,不在 files 里) */
|
|
92
|
+
vanished?: {
|
|
93
|
+
bucket: string;
|
|
94
|
+
key: string;
|
|
95
|
+
}[];
|
|
96
|
+
/** 2.12.0 起:有文件没复制时在场;缺席 = 列出的文件(除了被删的)都在 files 里 */
|
|
97
|
+
partial?: {
|
|
98
|
+
copied: number;
|
|
99
|
+
total: number;
|
|
100
|
+
notCopied: {
|
|
101
|
+
reason: StorageNotCopiedReason;
|
|
102
|
+
count: number;
|
|
103
|
+
}[];
|
|
104
|
+
};
|
|
105
|
+
};
|
|
106
|
+
/** 2.12.0 起:这次 Storage 整体失败(列不出、读不了、写不进),数据库照常完成;此时 storage 为 null */
|
|
107
|
+
storageFailure?: {
|
|
108
|
+
reason: string;
|
|
88
109
|
};
|
|
89
110
|
}
|
|
90
111
|
/**
|
|
@@ -159,6 +180,8 @@ export interface StorageFile {
|
|
|
159
180
|
metadata?: Record<string, unknown>;
|
|
160
181
|
lastModified?: string;
|
|
161
182
|
}
|
|
183
|
+
/** manifest.storage.partial.notCopied[].reason 的取值 */
|
|
184
|
+
export declare const STORAGE_NOT_COPIED_REASONS: readonly StorageNotCopiedReason[];
|
|
162
185
|
/**
|
|
163
186
|
* 解析桶里的 manifest.json,并做**向前兼容**校验:比本程序更新的格式一律拒绝,
|
|
164
187
|
* 明确告诉用户升级,绝不按旧结构硬解析(那会静默产出错误的演练/恢复结果)。
|
package/dist/manifest.js
CHANGED
|
@@ -16,6 +16,8 @@ import { GRANT_PRIVILEGES, GRANT_ROLES, GRANTED_OBJECT_KINDS } from "./grants.js
|
|
|
16
16
|
* - (仍是 2)2.9.0 加 database.grants:可加字段,旧读取端忽略它、照旧恢复,不升版本,
|
|
17
17
|
* 免得还没升级的 CLI 和 worker 拒读新快照。
|
|
18
18
|
* - (仍是 2)2.10.0 加 auth:同样是可加字段。旧读取端忽略它和 auth.dump,演练和恢复与之前完全一样。
|
|
19
|
+
* - (仍是 2)2.12.0 加 storage.vanished / storage.partial / storageFailure:可加字段。旧读取端把部分
|
|
20
|
+
* 复制的快照当成"文件清单就是这些",恢复与演练照旧只处理清单里的文件。
|
|
19
21
|
*/
|
|
20
22
|
export const MANIFEST_SCHEMA_VERSION = 2;
|
|
21
23
|
/**
|
|
@@ -259,8 +261,45 @@ function assertManifestShape(m) {
|
|
|
259
261
|
}
|
|
260
262
|
}
|
|
261
263
|
}
|
|
264
|
+
if (m.storage.vanished !== undefined) {
|
|
265
|
+
if (!Array.isArray(m.storage.vanished))
|
|
266
|
+
malformed("storage.vanished");
|
|
267
|
+
for (const v of m.storage.vanished) {
|
|
268
|
+
if (typeof v?.bucket !== "string" || typeof v?.key !== "string")
|
|
269
|
+
malformed("storage.vanished[]");
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
if (m.storage.partial !== undefined) {
|
|
273
|
+
const p = m.storage.partial;
|
|
274
|
+
if (typeof p !== "object" || p === null)
|
|
275
|
+
malformed("storage.partial");
|
|
276
|
+
// copied 必须就是 files 的条数,total = copied + 各原因没复制的个数:不然"备了 N 个"的说法和清单对不上
|
|
277
|
+
if (!Number.isSafeInteger(p.copied) || p.copied !== m.storage.files.length || !Number.isSafeInteger(p.total)) {
|
|
278
|
+
malformed("storage.partial.copied/total");
|
|
279
|
+
}
|
|
280
|
+
if (!Array.isArray(p.notCopied) || p.notCopied.length === 0)
|
|
281
|
+
malformed("storage.partial.notCopied");
|
|
282
|
+
for (const n of p.notCopied) {
|
|
283
|
+
if (!STORAGE_NOT_COPIED_REASONS.includes(n?.reason) || !Number.isSafeInteger(n.count) || n.count <= 0) {
|
|
284
|
+
malformed("storage.partial.notCopied[]");
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
if (p.total !== p.copied + p.notCopied.reduce((sum, n) => sum + n.count, 0)) {
|
|
288
|
+
malformed("storage.partial.total does not match copied + notCopied");
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
if (m.storageFailure !== undefined) {
|
|
293
|
+
if (typeof m.storageFailure !== "object" || m.storageFailure === null || typeof m.storageFailure.reason !== "string") {
|
|
294
|
+
malformed("storageFailure");
|
|
295
|
+
}
|
|
296
|
+
// 失败的那次 Storage 一个文件也不算进快照:两者同时出现说明 manifest 被拼错了
|
|
297
|
+
if (m.storage !== null)
|
|
298
|
+
malformed("storageFailure with a storage section");
|
|
262
299
|
}
|
|
263
300
|
}
|
|
301
|
+
/** manifest.storage.partial.notCopied[].reason 的取值 */
|
|
302
|
+
export const STORAGE_NOT_COPIED_REASONS = ["time_budget", "read_stalled", "key_too_long"];
|
|
264
303
|
/**
|
|
265
304
|
* 解析桶里的 manifest.json,并做**向前兼容**校验:比本程序更新的格式一律拒绝,
|
|
266
305
|
* 明确告诉用户升级,绝不按旧结构硬解析(那会静默产出错误的演练/恢复结果)。
|
package/dist/ping.d.ts
CHANGED
|
@@ -14,6 +14,11 @@ export declare function createPinger(rawUrl: string | undefined, opts?: {
|
|
|
14
14
|
}): Pinger;
|
|
15
15
|
/** backup 成功时发给监控的一行。快照名取对象键里的目录名(和 drill --snapshot 认的是同一个) */
|
|
16
16
|
export declare function backupSummary(manifest: Manifest): string;
|
|
17
|
+
/**
|
|
18
|
+
* 备份完成、但有要处理的部分没备全时(退出码 3)的通知:先说哪部分出了问题、为什么,再说数据库存在了哪个快照。
|
|
19
|
+
* Storage 和 Auth 同时出问题时两样都写。
|
|
20
|
+
*/
|
|
21
|
+
export declare function incompleteBackupSummary(manifest: Manifest, authRequiredButMissing: boolean): string;
|
|
17
22
|
/** drill 跑完(没抛错)后发哪种 ping、带什么:通过 = 成功;没通过 = 失败,列出没过的检查 */
|
|
18
23
|
export declare function drillOutcome(report: DrillReport): {
|
|
19
24
|
pass: boolean;
|
package/dist/ping.js
CHANGED
|
@@ -128,6 +128,26 @@ export function backupSummary(manifest) {
|
|
|
128
128
|
(manifest.auth?.status === "included" ? ` + ${manifest.auth.rows.users} Auth users` : "") +
|
|
129
129
|
`, snapshot ${manifest.dump.key.split("/").at(-2) ?? manifest.createdAt}`);
|
|
130
130
|
}
|
|
131
|
+
/**
|
|
132
|
+
* 备份完成、但有要处理的部分没备全时(退出码 3)的通知:先说哪部分出了问题、为什么,再说数据库存在了哪个快照。
|
|
133
|
+
* Storage 和 Auth 同时出问题时两样都写。
|
|
134
|
+
*/
|
|
135
|
+
export function incompleteBackupSummary(manifest, authRequiredButMissing) {
|
|
136
|
+
const problems = [];
|
|
137
|
+
const partial = manifest.storage?.partial;
|
|
138
|
+
if (manifest.storageFailure)
|
|
139
|
+
problems.push(`Storage backup failed (${manifest.storageFailure.reason}).`);
|
|
140
|
+
else if (partial)
|
|
141
|
+
problems.push(`Storage backup incomplete. Copied ${partial.copied} of ${partial.total} files.`);
|
|
142
|
+
if (authRequiredButMissing) {
|
|
143
|
+
problems.push(`Auth users were required but not backed up (${manifest.auth?.reason ?? "unknown reason"}).`);
|
|
144
|
+
}
|
|
145
|
+
const saved = `Database saved in snapshot ${manifest.dump.key.split("/").at(-2) ?? manifest.createdAt} ` +
|
|
146
|
+
`(${manifest.database.tableCount} tables` +
|
|
147
|
+
(manifest.auth?.status === "included" ? `, ${manifest.auth.rows.users} Auth users` : "") +
|
|
148
|
+
`).`;
|
|
149
|
+
return `backupdrill ${TOOL_VERSION}. ${problems.join(" ")} ${saved}`;
|
|
150
|
+
}
|
|
131
151
|
/** drill 跑完(没抛错)后发哪种 ping、带什么:通过 = 成功;没通过 = 失败,列出没过的检查 */
|
|
132
152
|
export function drillOutcome(report) {
|
|
133
153
|
const outcome = `snapshot ${report.snapshot}, ${report.restoredTableCount} tables / ` +
|
package/dist/recovery-doc.js
CHANGED
|
@@ -298,8 +298,13 @@ ${extensions.length
|
|
|
298
298
|
? ` - extensions required: ${extensions.map((e) => `${e.name} (schema "${e.schema}")`).join(", ")}`
|
|
299
299
|
: " - no extensions recorded"}
|
|
300
300
|
- **Storage files**: ${storage
|
|
301
|
-
?
|
|
302
|
-
|
|
301
|
+
? storage.partial
|
|
302
|
+
? `copied ${storage.partial.copied} of ${storage.partial.total} files (${mb(storage.totalBytes)} MB) to \`${base}/storage/<bucket>/<key>\`. ` +
|
|
303
|
+
`See manifest.json for file checksums and \`storage.partial\` for skip counts by reason`
|
|
304
|
+
: `${storage.fileCount} files, ${mb(storage.totalBytes)} MB under \`${base}/storage/<bucket>/<key>\` — per-file sha256 in manifest.json`
|
|
305
|
+
: manifest.storageFailure
|
|
306
|
+
? `not in this snapshot — copying them failed (${manifest.storageFailure.reason}); the database part is complete`
|
|
307
|
+
: "none (database-only snapshot)"}
|
|
303
308
|
${authText.contentsLine}- **Machine-readable inventory**: \`${base}/manifest.json\`
|
|
304
309
|
|
|
305
310
|
## Not covered — plan for these separately
|
package/dist/restore.js
CHANGED
|
@@ -4,9 +4,8 @@ import { createWriteStream } from "node:fs";
|
|
|
4
4
|
import { tmpdir } from "node:os";
|
|
5
5
|
import { join, dirname, resolve, sep } from "node:path";
|
|
6
6
|
import { pipeline } from "node:stream/promises";
|
|
7
|
-
import { GetObjectCommand } from "@aws-sdk/client-s3";
|
|
8
7
|
import { parseManifest } from "./manifest.js";
|
|
9
|
-
import { s3SnapshotStore } from "./snapshot-store.js";
|
|
8
|
+
import { s3SnapshotStore, s3StorageSource } from "./snapshot-store.js";
|
|
10
9
|
import { afterCommit, deferredForeignKeysOf, restoreDatabaseArtifact, userObjectCountSql, installExtensions, projectRefOf, refFromStorageEndpoint, assertNoHostOverride, normalizeHost, } from "./restore-engine.js";
|
|
11
10
|
import { checkExternalReferences, describe } from "./external-refs.js";
|
|
12
11
|
import { verifyRestored } from "./drill.js";
|
|
@@ -128,6 +127,17 @@ export function assertRequestApplies(manifest, wantsDatabase, wantsStorageUpload
|
|
|
128
127
|
throw new Error("nothing to do: no database restore was requested (--database) and this snapshot has no " +
|
|
129
128
|
"Storage files to download.");
|
|
130
129
|
}
|
|
130
|
+
// 部分复制、一个文件也没复制进来的快照:本地下载无事可做;上传到目标时,没记下 bucket 设置也无事可做
|
|
131
|
+
// (没文件也能建 bucket)。不能把空操作报成一次成功恢复。Storage 本来就是空的完整快照不在此列
|
|
132
|
+
const nothingCopied = Boolean(manifest.storage?.partial) && manifest.storage.files.length === 0;
|
|
133
|
+
if (!wantsDatabase && !wantsStorageUpload && nothingCopied) {
|
|
134
|
+
throw new Error("nothing to do: no database restore was requested (--database) and none of this snapshot's " +
|
|
135
|
+
"Storage files were copied when it was taken.");
|
|
136
|
+
}
|
|
137
|
+
if (!wantsDatabase && wantsStorageUpload && nothingCopied && !manifest.storage.buckets?.length) {
|
|
138
|
+
throw new Error("nothing to do: this snapshot has no Storage files or bucket settings to restore. " +
|
|
139
|
+
"To restore its database, add --database.");
|
|
140
|
+
}
|
|
131
141
|
}
|
|
132
142
|
/**
|
|
133
143
|
* 正式执行的确认门(PRD §5.3.3):用户必须键入目标 project ref(或非 Supabase 目标的
|
|
@@ -606,10 +616,22 @@ export async function runRestore(config, opts) {
|
|
|
606
616
|
storageFilesWritten: 0,
|
|
607
617
|
};
|
|
608
618
|
assertRequestApplies(manifest, !!opts.targetDatabaseUrl, !!storageTarget);
|
|
619
|
+
// 2.12.0:这份快照里 Storage 只复制了一部分,或这次没备上 —— 恢复前说清楚,别让人以为文件齐了
|
|
620
|
+
if (manifest.storage?.partial) {
|
|
621
|
+
const p = manifest.storage.partial;
|
|
622
|
+
log.warn(`This snapshot holds ${p.copied} of ${p.total} Storage files; the others were not copied when it was taken.`);
|
|
623
|
+
}
|
|
624
|
+
else if (manifest.storageFailure) {
|
|
625
|
+
log.warn(`Storage files are not in this snapshot (copying them failed: ${manifest.storageFailure.reason}).`);
|
|
626
|
+
}
|
|
609
627
|
// 这次运行会不会碰 Storage(回传到目标,或下载到本地)。只有会碰时,空目标门才该
|
|
610
628
|
// 提示"库已恢复、只重试 Storage";skipStorage 的纯数据库运行(托管恢复)不该看到这句。
|
|
611
629
|
const alsoRestoringStorage = !!manifest.storage && !opts.skipStorage;
|
|
612
|
-
|
|
630
|
+
// 本地目录(restore --from-dir)只提供 storageSource;桶里的快照按 storageFiles 的位置读
|
|
631
|
+
const storageLocation = manifest.storage && !reader.storageSource ? reader.storageFiles(manifest) : null;
|
|
632
|
+
const storageSource = manifest.storage
|
|
633
|
+
? (reader.storageSource?.(manifest) ?? (storageLocation ? s3StorageSource(storageLocation) : null))
|
|
634
|
+
: null;
|
|
613
635
|
if (alsoRestoringStorage && !storageSource) {
|
|
614
636
|
throw new Error("the manifest lists Storage files, but this snapshot location cannot hold them — " +
|
|
615
637
|
"restore the database only (skip Storage).");
|
|
@@ -982,11 +1004,7 @@ export async function runRestore(config, opts) {
|
|
|
982
1004
|
throw new Error(`unsafe storage path escapes output directory: ${f.bucket}/${f.key}`);
|
|
983
1005
|
}
|
|
984
1006
|
await mkdir(dirname(dest), { recursive: true });
|
|
985
|
-
|
|
986
|
-
Bucket: storageSource.bucket,
|
|
987
|
-
Key: `${storageSource.base}/storage/${f.bucket}/${f.key}`,
|
|
988
|
-
}));
|
|
989
|
-
await pipeline(res.Body, createWriteStream(dest));
|
|
1007
|
+
await pipeline(await storageSource.open(f.bucket, f.key), createWriteStream(dest));
|
|
990
1008
|
result.storageFilesWritten += 1;
|
|
991
1009
|
}
|
|
992
1010
|
result.storageDir = outDir;
|
package/dist/snapshot-store.d.ts
CHANGED
|
@@ -19,6 +19,19 @@ export interface StorageFilesLocation {
|
|
|
19
19
|
bucket: string;
|
|
20
20
|
base: string;
|
|
21
21
|
}
|
|
22
|
+
/**
|
|
23
|
+
* 读快照里的 Storage 文件(恢复回传、本地下载用)。range 为闭区间字节范围。
|
|
24
|
+
* S3 兼容桶经 s3StorageSource 包一层;下载到本地的快照(restore --from-dir)读本地文件。
|
|
25
|
+
*/
|
|
26
|
+
export interface StorageObjectSource {
|
|
27
|
+
open(bucket: string, key: string, range?: {
|
|
28
|
+
start: number;
|
|
29
|
+
end: number;
|
|
30
|
+
}): Promise<Readable>;
|
|
31
|
+
}
|
|
32
|
+
export declare function s3StorageSource(location: StorageFilesLocation): StorageObjectSource;
|
|
33
|
+
/** 两种形态都收:老调用方传桶位置,新的传 StorageObjectSource */
|
|
34
|
+
export declare function asStorageSource(source: StorageObjectSource | StorageFilesLocation): StorageObjectSource;
|
|
22
35
|
export interface DumpUpload {
|
|
23
36
|
/** 转储全部写完才 resolve;任何失败都 reject */
|
|
24
37
|
done: Promise<void>;
|
|
@@ -60,8 +73,13 @@ export interface SnapshotReader {
|
|
|
60
73
|
bytes: number;
|
|
61
74
|
sha256: string;
|
|
62
75
|
}>;
|
|
63
|
-
/** 这份快照的 Storage
|
|
76
|
+
/** 这份快照的 Storage 文件在哪个桶位置(演练按它抽样);实现不支持 Storage 文件时为 null */
|
|
64
77
|
storageFiles(manifest: Manifest): StorageFilesLocation | null;
|
|
78
|
+
/**
|
|
79
|
+
* 可选(2.12.0 起):恢复时从哪读 Storage 文件。没实现时由 storageFiles 的桶位置读;
|
|
80
|
+
* 本地目录(restore --from-dir)没有桶,只实现这一个。
|
|
81
|
+
*/
|
|
82
|
+
storageSource?(manifest: Manifest): StorageObjectSource | null;
|
|
65
83
|
}
|
|
66
84
|
/** S3 兼容桶(S3 / R2 / B2 / Wasabi)。行为与抽出这层之前逐字一致。 */
|
|
67
85
|
export declare function s3SnapshotStore(config: BackupConfig): SnapshotStore;
|
package/dist/snapshot-store.js
CHANGED
|
@@ -1,9 +1,25 @@
|
|
|
1
|
-
import { PutObjectCommand } from "@aws-sdk/client-s3";
|
|
1
|
+
import { GetObjectCommand, PutObjectCommand } from "@aws-sdk/client-s3";
|
|
2
2
|
import { Upload } from "@aws-sdk/lib-storage";
|
|
3
3
|
import { requireBucket } from "./config.js";
|
|
4
4
|
import { targetClient, resolveSnapshot, getObjectText, downloadToFile } from "./snapshots.js";
|
|
5
5
|
import { renderRecoveryDoc } from "./recovery-doc.js";
|
|
6
6
|
import { dumpPartSizeFor } from "./part-size.js";
|
|
7
|
+
export function s3StorageSource(location) {
|
|
8
|
+
return {
|
|
9
|
+
async open(bucket, key, range) {
|
|
10
|
+
const got = await location.s3.send(new GetObjectCommand({
|
|
11
|
+
Bucket: location.bucket,
|
|
12
|
+
Key: `${location.base}/storage/${bucket}/${key}`,
|
|
13
|
+
...(range ? { Range: `bytes=${range.start}-${range.end}` } : {}),
|
|
14
|
+
}));
|
|
15
|
+
return got.Body;
|
|
16
|
+
},
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
/** 两种形态都收:老调用方传桶位置,新的传 StorageObjectSource */
|
|
20
|
+
export function asStorageSource(source) {
|
|
21
|
+
return "open" in source ? source : s3StorageSource(source);
|
|
22
|
+
}
|
|
7
23
|
/** 写入时的键都在 <prefix>/<projectName>/<快照名>/ 下;读取时 Storage 文件的 base 从 dump.key 反推。 */
|
|
8
24
|
function storageBaseOf(manifest) {
|
|
9
25
|
return manifest.dump.key.replace(/\/dump\.pgcustom$/, "");
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { type
|
|
1
|
+
import { type StorageFilesLocation, type StorageObjectSource } from "./snapshot-store.js";
|
|
2
2
|
import type { Manifest, StorageFile } from "./manifest.js";
|
|
3
3
|
export interface StorageRestoreTarget {
|
|
4
4
|
supabaseUrl: string;
|
|
@@ -72,8 +72,4 @@ export declare function planStorageRestore(target: StorageRestoreTarget, manifes
|
|
|
72
72
|
verifiedResidue: number;
|
|
73
73
|
warnings: string[];
|
|
74
74
|
}>;
|
|
75
|
-
export declare function restoreStorage(
|
|
76
|
-
s3: S3Client;
|
|
77
|
-
bucket: string;
|
|
78
|
-
base: string;
|
|
79
|
-
}, target: StorageRestoreTarget, manifest: Manifest): Promise<StorageRestoreSummary>;
|
|
75
|
+
export declare function restoreStorage(sourceOrLocation: StorageObjectSource | StorageFilesLocation, target: StorageRestoreTarget, manifest: Manifest): Promise<StorageRestoreSummary>;
|
package/dist/storage-restore.js
CHANGED
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
// >= 比较会被多余对象掩蔽缺失(评审第 8 轮)。
|
|
8
8
|
import { createHash } from "node:crypto";
|
|
9
9
|
import { Readable } from "node:stream";
|
|
10
|
-
import {
|
|
10
|
+
import { asStorageSource } from "./snapshot-store.js";
|
|
11
11
|
import { sampleStorageFiles } from "./drill.js";
|
|
12
12
|
import { log } from "./log.js";
|
|
13
13
|
// PRD §5.4.2:默认小并发、有界超时与重试,均有安全上限
|
|
@@ -228,12 +228,7 @@ async function uploadViaTus(source, target, file, summary) {
|
|
|
228
228
|
let chunk = null;
|
|
229
229
|
for (let readAttempt = 1; readAttempt <= MAX_ATTEMPTS; readAttempt++) {
|
|
230
230
|
try {
|
|
231
|
-
|
|
232
|
-
Bucket: source.bucket,
|
|
233
|
-
Key: `${source.base}/storage/${file.bucket}/${file.key}`,
|
|
234
|
-
Range: `bytes=${offset}-${end}`,
|
|
235
|
-
}));
|
|
236
|
-
chunk = await collectBody(got.Body);
|
|
231
|
+
chunk = await collectBody(await source.open(file.bucket, file.key, { start: offset, end }));
|
|
237
232
|
break;
|
|
238
233
|
}
|
|
239
234
|
catch (error) {
|
|
@@ -332,14 +327,11 @@ async function uploadFile(source, target, file, summary) {
|
|
|
332
327
|
let lastError = null;
|
|
333
328
|
for (let attempt = 1; attempt <= MAX_ATTEMPTS; attempt++) {
|
|
334
329
|
try {
|
|
335
|
-
const
|
|
336
|
-
Bucket: source.bucket,
|
|
337
|
-
Key: `${source.base}/storage/${file.bucket}/${file.key}`,
|
|
338
|
-
}));
|
|
330
|
+
const body = await source.open(file.bucket, file.key);
|
|
339
331
|
const res = await fetch(`${target.supabaseUrl}/storage/v1/object/${encodeURIComponent(file.bucket)}/${encodedKey}`, {
|
|
340
332
|
method: "POST",
|
|
341
333
|
headers: { ...storageHeaders(target.serviceRoleKey), ...headers },
|
|
342
|
-
body: Readable.toWeb(
|
|
334
|
+
body: Readable.toWeb(body),
|
|
343
335
|
signal: AbortSignal.timeout(UPLOAD_TIMEOUT_MS),
|
|
344
336
|
// @ts-expect-error half-duplex for streaming body
|
|
345
337
|
duplex: "half",
|
|
@@ -581,7 +573,8 @@ export async function planStorageRestore(target, manifest) {
|
|
|
581
573
|
warnings: [...scratch.bucketAttrDrift, ...scratch.metadataNotes],
|
|
582
574
|
};
|
|
583
575
|
}
|
|
584
|
-
export async function restoreStorage(
|
|
576
|
+
export async function restoreStorage(sourceOrLocation, target, manifest) {
|
|
577
|
+
const source = asStorageSource(sourceOrLocation);
|
|
585
578
|
const summary = emptySummary();
|
|
586
579
|
const { plans, verifiedResidue } = await readOnlyStoragePlan(target, manifest, summary);
|
|
587
580
|
summary.filesSkippedIdentical = verifiedResidue.size;
|
package/dist/storage.d.ts
CHANGED
|
@@ -23,17 +23,90 @@ export declare function measureStorage(cfg: SupabaseStorageConfig): Promise<{
|
|
|
23
23
|
totalBytes: number;
|
|
24
24
|
buckets: string[];
|
|
25
25
|
}>;
|
|
26
|
+
/** 目标键上限(S3 与 R2 都是 1,024 字节,UTF-8) */
|
|
27
|
+
export declare const STORAGE_KEY_MAX_BYTES = 1024;
|
|
28
|
+
/** 读取多久没有新字节就放弃这个文件 */
|
|
29
|
+
export declare const STORAGE_STALL_MS: number;
|
|
30
|
+
/** 写请求计数的回报粒度 */
|
|
31
|
+
export declare const STORAGE_WRITE_REPORT_EVERY = 1000;
|
|
32
|
+
/** 没复制的原因:时间预算用完 / 读取停滞 / 目标键超过上限 */
|
|
33
|
+
export type StorageNotCopiedReason = "time_budget" | "read_stalled" | "key_too_long";
|
|
34
|
+
export interface StorageSyncOptions {
|
|
35
|
+
/**
|
|
36
|
+
* 时间预算的硬边界:截止前估算传不完的文件不开始,在传的到点中止,截止后不再复制。
|
|
37
|
+
* 省略 = 不设时间预算(CLI 默认)。托管服务传"任务期限 − 30 分钟"。
|
|
38
|
+
*/
|
|
39
|
+
deadline?: Date;
|
|
40
|
+
/** 读取停滞多久中止这个文件,也是列举时单个请求的时限;默认 STORAGE_STALL_MS */
|
|
41
|
+
stallMs?: number;
|
|
42
|
+
/** 源读完后、截止之后再等目标确认多久;默认 STORAGE_COMMIT_GRACE_MS(只在设了 deadline 时有意义) */
|
|
43
|
+
commitGraceMs?: number;
|
|
44
|
+
/**
|
|
45
|
+
* 写请求计数回报(给托管服务按账号累计操作费):复制途中每 STORAGE_WRITE_REPORT_EVERY 次回报一次增量,
|
|
46
|
+
* 结束时回报余数。含分片的每一片和 SDK 重试的每一次。
|
|
47
|
+
*/
|
|
48
|
+
onWriteRequests?: (delta: number) => void | Promise<void>;
|
|
49
|
+
}
|
|
50
|
+
export interface StorageSyncResult {
|
|
51
|
+
fileCount: number;
|
|
52
|
+
totalBytes: number;
|
|
53
|
+
files: StorageFile[];
|
|
54
|
+
buckets: string[];
|
|
55
|
+
/** 列出时在、读取时已经不存在的文件(用户在复制途中删了它),不算缺 */
|
|
56
|
+
vanished: {
|
|
57
|
+
bucket: string;
|
|
58
|
+
key: string;
|
|
59
|
+
}[];
|
|
60
|
+
/** 有文件没复制时给出;null = 列出的文件(除了被删的)全部复制完 */
|
|
61
|
+
partial: StoragePartial | null;
|
|
62
|
+
/** 本次复制发往目标的请求数 */
|
|
63
|
+
writeRequests: number;
|
|
64
|
+
/**
|
|
65
|
+
* 其中 onWriteRequests 没确认收下的数(回报失败,或最后一批 30 秒内没回来);没传 onWriteRequests 时为 0。
|
|
66
|
+
* 调用方据此补记。最后一批超时的那次回调可能之后才落下,补记要能认出重复。
|
|
67
|
+
*/
|
|
68
|
+
writeRequestsUnconfirmed: number;
|
|
69
|
+
}
|
|
70
|
+
export interface StoragePartial {
|
|
71
|
+
copied: number;
|
|
72
|
+
/** 分母 = 开始时列出的文件数 − 复制途中被删的 */
|
|
73
|
+
total: number;
|
|
74
|
+
notCopied: {
|
|
75
|
+
reason: StorageNotCopiedReason;
|
|
76
|
+
count: number;
|
|
77
|
+
}[];
|
|
78
|
+
}
|
|
26
79
|
/**
|
|
27
|
-
*
|
|
28
|
-
*
|
|
80
|
+
* Storage 阶段整体失败(列不出来、读不了、写不进目标):数据库那部分不受影响,由调用方记成
|
|
81
|
+
* "这次 Storage 失败",备份照常完成。
|
|
82
|
+
*/
|
|
83
|
+
export declare class StorageSyncError extends Error {
|
|
84
|
+
readonly cause?: unknown | undefined;
|
|
85
|
+
constructor(message: string, cause?: unknown | undefined);
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* 写进 manifest、RECOVERY.md 和日志的失败原因:只用归一化的说法和 S3 错误码,
|
|
89
|
+
* 不带服务端返回的原文(可能含密钥 ID、请求 ID 之类的诊断信息)。
|
|
90
|
+
*/
|
|
91
|
+
export declare function describeStorageError(error: unknown): string;
|
|
92
|
+
/**
|
|
93
|
+
* 把 Supabase Storage 的文件流式拷贝到目标桶,边拷边算 sha256。目标键:<base>/storage/<bucket>/<原始 key>。
|
|
94
|
+
*
|
|
95
|
+
* 开始时先把所有 bucket 列完,这份清单就是本次的分母;之后:
|
|
96
|
+
* - 列出时在、读取时 404:跳过,记进 vanished(用户在复制途中删了它,不算缺);
|
|
97
|
+
* - 目标键超过 1,024 字节:不复制,记"键过长";
|
|
98
|
+
* - 读取停滞(stallMs 没有新字节):中止这个文件,记"读取停滞",继续下一个;
|
|
99
|
+
* - 设了 deadline:估算传不完的不开始,在传的到点中止,截止后停止,都记"时间预算";
|
|
100
|
+
* 有没复制的文件时 partial 非空。列不出来、读文件出错(404 以外)、写目标出错:抛 StorageSyncError。
|
|
29
101
|
*/
|
|
30
102
|
export declare function syncStorage(cfg: SupabaseStorageConfig, target: {
|
|
31
103
|
client: S3Client;
|
|
32
104
|
bucket: string;
|
|
33
105
|
base: string;
|
|
34
|
-
}): Promise<
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
106
|
+
}, opts?: StorageSyncOptions): Promise<StorageSyncResult>;
|
|
107
|
+
/** 源读完、到了截止还没等到目标确认时,再多等多久;之后中止并记"没复制" */
|
|
108
|
+
export declare const STORAGE_COMMIT_GRACE_MS = 60000;
|
|
109
|
+
/** 没设 deadline 时(CLI 默认),源读完后最多等目标确认多久 */
|
|
110
|
+
export declare const STORAGE_CONFIRM_TIMEOUT_MS: number;
|
|
111
|
+
/** 发往目标的单个写请求(一片或一次 PutObject)最多等多久;超时算写目标出错 */
|
|
112
|
+
export declare const STORAGE_WRITE_REQUEST_MS: number;
|