backupdrill 2.12.0 → 2.14.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 +110 -15
- package/dist/auth-backup.d.ts +5 -14
- package/dist/auth-backup.js +6 -69
- package/dist/auth-restore.js +1 -1
- package/dist/backup.d.ts +12 -2
- package/dist/backup.js +93 -30
- package/dist/estimate.js +7 -4
- package/dist/grants.js +4 -0
- package/dist/index.js +4 -3
- package/dist/lib.d.ts +2 -2
- package/dist/lib.js +3 -2
- package/dist/manifest.d.ts +4 -2
- package/dist/manifest.js +5 -1
- package/dist/orphaned-foreign-keys.js +8 -1
- package/dist/recovery-doc.d.ts +64 -14
- package/dist/recovery-doc.js +506 -134
- package/dist/restore.js +7 -6
- package/dist/schema-patterns.d.ts +38 -0
- package/dist/schema-patterns.js +115 -0
- package/package.json +1 -1
package/dist/estimate.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { measureStorage } from "./storage.js";
|
|
2
2
|
import { log } from "./log.js";
|
|
3
3
|
import { connectPg } from "./supabase-ca.js";
|
|
4
|
+
import { resolveSchemaPatterns, schemaNamesText } from "./schema-patterns.js";
|
|
4
5
|
// 超出这个月度成本就该提醒用户"备份的 egress 账单快赶上订阅费了"
|
|
5
6
|
const SUBSCRIPTION_FLOOR_USD = 19;
|
|
6
7
|
const GB = 1024 * 1024 * 1024;
|
|
@@ -16,10 +17,12 @@ export async function relationBytes(client, schemas) {
|
|
|
16
17
|
where schemaname = any($1::text[])`, [schemas]);
|
|
17
18
|
return Number(res.rows[0].bytes);
|
|
18
19
|
}
|
|
19
|
-
|
|
20
|
+
/** schemas 是 pg_dump 的 --schema 模式,先在库上解析成名字(和 backup 一样,见 schema-patterns.ts)再量 */
|
|
21
|
+
async function measureDbBytes(databaseUrl, patterns) {
|
|
20
22
|
const client = await connectPg(databaseUrl);
|
|
21
23
|
try {
|
|
22
|
-
|
|
24
|
+
const schemas = await resolveSchemaPatterns(client, patterns);
|
|
25
|
+
return { bytes: await relationBytes(client, schemas), schemas };
|
|
23
26
|
}
|
|
24
27
|
finally {
|
|
25
28
|
await client.end();
|
|
@@ -62,8 +65,8 @@ export function overageNote(pricing) {
|
|
|
62
65
|
}
|
|
63
66
|
export async function runEstimate(config, pricing) {
|
|
64
67
|
log.step("Measuring database size…");
|
|
65
|
-
const dbBytes = await measureDbBytes(config.databaseUrl, config.schemas);
|
|
66
|
-
log.ok(`Database (schema ${
|
|
68
|
+
const { bytes: dbBytes, schemas } = await measureDbBytes(config.databaseUrl, config.schemas);
|
|
69
|
+
log.ok(`Database (schema ${schemaNamesText(schemas)}): ${(dbBytes / GB).toFixed(2)} GB (upper bound)`);
|
|
67
70
|
let storageBytes = 0;
|
|
68
71
|
if (config.supabaseStorage) {
|
|
69
72
|
log.step("Measuring Storage size (listing only, no download)…");
|
package/dist/grants.js
CHANGED
|
@@ -328,6 +328,10 @@ function dollarQuoteTag(base, contents) {
|
|
|
328
328
|
}
|
|
329
329
|
}
|
|
330
330
|
function sqlLiteral(value) {
|
|
331
|
+
// 名字可能取自源库(schema 名),什么字符都可能有。有反斜杠时用 E'' 写法并把反斜杠加倍:
|
|
332
|
+
// 它的含义不随会话的 standard_conforming_strings 变,不会被读成转义序列
|
|
333
|
+
if (value.includes("\\"))
|
|
334
|
+
return `E'${value.replace(/\\/g, "\\\\").replace(/'/g, "''")}'`;
|
|
331
335
|
return `'${value.replace(/'/g, "''")}'`;
|
|
332
336
|
}
|
|
333
337
|
/**
|
package/dist/index.js
CHANGED
|
@@ -60,7 +60,8 @@ program
|
|
|
60
60
|
.option("-c, --config <path>", "path to a JSON config file", "backupdrill.config.json")
|
|
61
61
|
.option("--database-url <url>", "Supabase Session Pooler connection string")
|
|
62
62
|
.option("--project-name <name>", "readable name for this project (used in the object key)")
|
|
63
|
-
.option("--schema <name...>", "schema
|
|
63
|
+
.option("--schema <name...>", "schema patterns to back up (default: public only). Unquoted ASCII letters fold to lowercase; " +
|
|
64
|
+
"use * and ? as wildcards, or '\"Sales\"' for an exact match. Leave out the database name")
|
|
64
65
|
.option("--bucket <name>", "target S3/R2/B2 bucket")
|
|
65
66
|
.option("--endpoint <url>", "S3-compatible endpoint (required for R2/B2; omit for AWS S3)")
|
|
66
67
|
.option("--region <region>", "bucket region (default: auto)")
|
|
@@ -107,7 +108,7 @@ program
|
|
|
107
108
|
// 只有显式要求 Auth(--auth / BACKUPDRILL_AUTH=on)而它没备上,才让这次运行报失败:
|
|
108
109
|
// 数据库备份照样完成;退出码 3 与"整个备份失败"的 1 区分开。默认情况下做不了只打警告,
|
|
109
110
|
// 升级到这个版本的定时任务不会因此变红
|
|
110
|
-
// (显式要求时 planAuth 把所有"做不了"都记成 failed
|
|
111
|
+
// (显式要求时 planAuth 把所有"做不了"都记成 failed;只有主 dump 已经带上两张 Auth 表时记 skipped——数据就在主 dump 里)
|
|
111
112
|
const authRequiredButMissing = config.auth === "on" && manifest.auth?.status === "failed";
|
|
112
113
|
// 2.12.0 起 Storage 失败或只复制了一部分时数据库照样备完;同样用退出码 3 区分,定时任务能看见
|
|
113
114
|
const storageIncomplete = Boolean(manifest.storageFailure || manifest.storage?.partial);
|
|
@@ -403,7 +404,7 @@ program
|
|
|
403
404
|
.description("Measure your DB + Storage size and project monthly Supabase egress cost")
|
|
404
405
|
.option("-c, --config <path>", "path to a JSON config file", "backupdrill.config.json")
|
|
405
406
|
.option("--database-url <url>", "Supabase Session Pooler connection string")
|
|
406
|
-
.option("--schema <name...>", "schema
|
|
407
|
+
.option("--schema <name...>", "schema patterns to measure, same rules as backup --schema (default: public only)")
|
|
407
408
|
.option("--plan <plan>", "your Supabase plan for the cost estimate: free | pro | team", "pro")
|
|
408
409
|
.action(async (opts) => {
|
|
409
410
|
try {
|
package/dist/lib.d.ts
CHANGED
|
@@ -5,8 +5,8 @@ export { runRestore, restoreStatus, finishRestore } from "./restore.js";
|
|
|
5
5
|
export { fileRestoreRecords, restoreRecordKey } from "./restore-record.js";
|
|
6
6
|
export type { RestoreRecord, RestoreRecordStore, RestorePhase } from "./restore-record.js";
|
|
7
7
|
export { recoveryGrantsSql } from "./grants.js";
|
|
8
|
-
export { authRecoveryText } from "./recovery-doc.js";
|
|
9
|
-
export type { AuthRecoveryText, RecoveryDocPlace } from "./recovery-doc.js";
|
|
8
|
+
export { authRecoveryText, renderRunbook, renderRecoveryDoc } from "./recovery-doc.js";
|
|
9
|
+
export type { AuthRecoveryText, RecoveryDocPlace, RecoveryDocSource, RecoveryDocDownloadSource, RunbookContext, DownloadRunbookContext, RecoveryDocContext } from "./recovery-doc.js";
|
|
10
10
|
export { projectRefOf, assertNoHostOverride, libpqChildEnv } from "./restore-engine.js";
|
|
11
11
|
export { sameDatabaseTarget } from "./restore.js";
|
|
12
12
|
export { runEstimate, project } from "./estimate.js";
|
package/dist/lib.js
CHANGED
|
@@ -7,9 +7,10 @@ export { SUPABASE_ROOT_CA, pgConnectOptions, PG_CLIENT_OPTIONS, normalizeConnect
|
|
|
7
7
|
export { runDrill, drillDump } from "./drill.js";
|
|
8
8
|
export { runRestore, restoreStatus, finishRestore } from "./restore.js";
|
|
9
9
|
export { fileRestoreRecords, restoreRecordKey } from "./restore-record.js";
|
|
10
|
-
// RECOVERY.md
|
|
10
|
+
// RECOVERY.md:自有桶、托管 worker 的 Google Drive 目标、托管存储的下载三种目标共用 renderRunbook 这一份,
|
|
11
|
+
// 只有"文件在哪、怎么拿到"不同。授权 SQL 和 Auth 用户那几段也单独导出(老调用方)
|
|
11
12
|
export { recoveryGrantsSql } from "./grants.js";
|
|
12
|
-
export { authRecoveryText } from "./recovery-doc.js";
|
|
13
|
+
export { authRecoveryText, renderRunbook, renderRecoveryDoc } from "./recovery-doc.js";
|
|
13
14
|
// 身份判定三件套对外导出:托管 worker 用它们与 app 侧 shared 的副本做同规则回归(交叉审查)
|
|
14
15
|
export { projectRefOf, assertNoHostOverride, libpqChildEnv } from "./restore-engine.js";
|
|
15
16
|
export { sameDatabaseTarget } from "./restore.js";
|
package/dist/manifest.d.ts
CHANGED
|
@@ -111,11 +111,13 @@ export interface Manifest {
|
|
|
111
111
|
/**
|
|
112
112
|
* Auth 用户备份的结果。只有 included 的快照带 dump 和统计;其余状态说明为什么没备上。
|
|
113
113
|
* - included:auth.dump 在快照里,行数可以合法为 0
|
|
114
|
-
* - skipped:默认开启但做不了(无权读取、没有 auth
|
|
114
|
+
* - skipped:默认开启但做不了(无权读取、没有 auth 表、存储不支持、主 dump 已含 auth、schema 名字不是普通名字、太大),不算失败。
|
|
115
|
+
* schemas_not_plain 是 2.12.1 之前的"用了模式写法";2.12.1 起按解析出的名字判断,记 schema_names_not_plain。
|
|
116
|
+
* server_too_old(2.12.1 起):源库是 PostgreSQL 11,数据库照常备,auth.dump 要 12
|
|
115
117
|
* - failed:权限够、却在导出或上传时出错;只有显式要求时备份才因此失败
|
|
116
118
|
*/
|
|
117
119
|
export declare const AUTH_STATUSES: readonly ["included", "skipped", "failed"];
|
|
118
|
-
export declare const AUTH_SKIP_REASONS: readonly ["no_permission", "no_auth_schema", "unsupported_destination", "schemas_include_auth", "schemas_not_plain", "too_large", "snapshot_unavailable"];
|
|
120
|
+
export declare const AUTH_SKIP_REASONS: readonly ["no_permission", "no_auth_schema", "unsupported_destination", "schemas_include_auth", "schemas_not_plain", "schema_names_not_plain", "too_large", "snapshot_unavailable", "server_too_old"];
|
|
119
121
|
export interface AuthTriggerInfo {
|
|
120
122
|
table: string;
|
|
121
123
|
name: string;
|
package/dist/manifest.js
CHANGED
|
@@ -23,7 +23,9 @@ export const MANIFEST_SCHEMA_VERSION = 2;
|
|
|
23
23
|
/**
|
|
24
24
|
* Auth 用户备份的结果。只有 included 的快照带 dump 和统计;其余状态说明为什么没备上。
|
|
25
25
|
* - included:auth.dump 在快照里,行数可以合法为 0
|
|
26
|
-
* - skipped:默认开启但做不了(无权读取、没有 auth
|
|
26
|
+
* - skipped:默认开启但做不了(无权读取、没有 auth 表、存储不支持、主 dump 已含 auth、schema 名字不是普通名字、太大),不算失败。
|
|
27
|
+
* schemas_not_plain 是 2.12.1 之前的"用了模式写法";2.12.1 起按解析出的名字判断,记 schema_names_not_plain。
|
|
28
|
+
* server_too_old(2.12.1 起):源库是 PostgreSQL 11,数据库照常备,auth.dump 要 12
|
|
27
29
|
* - failed:权限够、却在导出或上传时出错;只有显式要求时备份才因此失败
|
|
28
30
|
*/
|
|
29
31
|
export const AUTH_STATUSES = ["included", "skipped", "failed"];
|
|
@@ -33,8 +35,10 @@ export const AUTH_SKIP_REASONS = [
|
|
|
33
35
|
"unsupported_destination",
|
|
34
36
|
"schemas_include_auth",
|
|
35
37
|
"schemas_not_plain",
|
|
38
|
+
"schema_names_not_plain",
|
|
36
39
|
"too_large",
|
|
37
40
|
"snapshot_unavailable",
|
|
41
|
+
"server_too_old",
|
|
38
42
|
];
|
|
39
43
|
/** 结构断言失败时的统一出错口径:说清哪个字段坏了,让"桶里的 manifest 被篡改/截断"可定位。 */
|
|
40
44
|
function malformed(what) {
|
|
@@ -74,8 +74,15 @@ export function describeOrphanedForeignKey(orphaned) {
|
|
|
74
74
|
// 不说"指向不存在的行":MATCH FULL 下部分为空的行也算违反,它们未必缺对应的行(文案审查)
|
|
75
75
|
return `${rows} in ${orphaned.table} break foreign key ${orphaned.constraint}, which references ${orphaned.referencedTable}`;
|
|
76
76
|
}
|
|
77
|
-
/**
|
|
77
|
+
/**
|
|
78
|
+
* 外键两端的键列里有没有生成列(attgenerated:存储型 's',PostgreSQL 18 起还有虚拟型 'v')。
|
|
79
|
+
* 生成列和 attgenerated 都是 12 才有的:恢复目标是 11 时没有生成列,也不能去查这一列——
|
|
80
|
+
* 查了会报 "column does not exist",把整个恢复事务弄坏
|
|
81
|
+
*/
|
|
78
82
|
async function hasGeneratedKeyColumn(client, fk) {
|
|
83
|
+
const version = await client.query("show server_version_num");
|
|
84
|
+
if (Number(version.rows[0].server_version_num) < 120000)
|
|
85
|
+
return false;
|
|
79
86
|
const qualified = (table) => `${quoteIdent(table.schema)}.${quoteIdent(table.name)}`;
|
|
80
87
|
const { rows } = await client.query(`select exists (
|
|
81
88
|
select 1 from pg_attribute
|
package/dist/recovery-doc.d.ts
CHANGED
|
@@ -6,18 +6,11 @@ import type { Manifest } from "./manifest.js";
|
|
|
6
6
|
* 控制字符换成可见的转义,名字里的换行不会让这一段断开
|
|
7
7
|
*/
|
|
8
8
|
export declare function markdownCode(text: string): string;
|
|
9
|
-
/** 生成手册所需的非秘密快照坐标。刻意不收连接串/密钥类型——签名即防线。 */
|
|
10
|
-
export interface RecoveryDocContext {
|
|
11
|
-
snapshot: string;
|
|
12
|
-
bucket: string;
|
|
13
|
-
endpoint?: string;
|
|
14
|
-
region?: string;
|
|
15
|
-
prefix: string;
|
|
16
|
-
projectName: string;
|
|
17
|
-
}
|
|
18
9
|
/**
|
|
19
10
|
* 手册里的归档在哪。bucket = S3 兼容桶,写完整对象键;folder = 托管 worker 的 Google Drive 目标,
|
|
20
|
-
* 手册就放在归档旁边,写文件名,name 是下载那一步说的"从哪里下"(如 "Google Drive folder")
|
|
11
|
+
* 手册就放在归档旁边,写文件名,name 是下载那一步说的"从哪里下"(如 "Google Drive folder");
|
|
12
|
+
* download = 托管存储的下载(下载脚本建的文件夹:backupdrill-download.json 索引 + files/ 下的编号文件),
|
|
13
|
+
* 写索引里的键 <快照名>/<文件名>
|
|
21
14
|
*/
|
|
22
15
|
export type RecoveryDocPlace = {
|
|
23
16
|
kind: "bucket";
|
|
@@ -25,6 +18,11 @@ export type RecoveryDocPlace = {
|
|
|
25
18
|
kind: "folder";
|
|
26
19
|
name: string;
|
|
27
20
|
};
|
|
21
|
+
/** 引擎内部多一种:托管存储的下载(只由 renderRunbook 传入;导出的 RecoveryDocPlace 保持原样,不破坏下游的穷举判断) */
|
|
22
|
+
type RunbookPlace = RecoveryDocPlace | {
|
|
23
|
+
kind: "download";
|
|
24
|
+
snapshot: string;
|
|
25
|
+
};
|
|
28
26
|
export interface AuthRecoveryText {
|
|
29
27
|
/** "What this snapshot contains" 里 Auth 那一行(带换行);manifest 没有 auth 段时为空串 */
|
|
30
28
|
contentsLine: string;
|
|
@@ -35,9 +33,61 @@ export interface AuthRecoveryText {
|
|
|
35
33
|
} | null;
|
|
36
34
|
}
|
|
37
35
|
/**
|
|
38
|
-
* RECOVERY.md 里和 Auth 用户有关的段落(2.10.0 起)。S3
|
|
39
|
-
*
|
|
40
|
-
* 没有 auth 字段的快照:
|
|
36
|
+
* RECOVERY.md 里和 Auth 用户有关的段落(2.10.0 起)。S3 版、托管 worker 的 Google Drive 版和托管存储版
|
|
37
|
+
* 都由 renderRunbook 生成,共用这一份:单事务恢复脚本里的空目标检查、清理待处理链接、行数核对,不能各写各的。
|
|
38
|
+
* 没有 auth 字段的快照:included 为 null;contentsLine 只在备份范围选了 auth 时有一行(见 authMaybeInMainDump),否则为空
|
|
39
|
+
*/
|
|
40
|
+
/**
|
|
41
|
+
* 没有 auth.dump、备份范围却选了 auth 的快照:Auth 用户可能在主 dump 里。manifest 的表清单是转储前查的,
|
|
42
|
+
* 证明不了归档里有(表归扩展所有、分区落在范围外、转储期间被删都会让归档里没有),手册只告诉读者怎么查,不担保
|
|
43
|
+
*/
|
|
44
|
+
export declare function authMaybeInMainDump(manifest: Manifest): boolean;
|
|
45
|
+
export declare function authRecoveryText(manifest: Manifest, place: RunbookPlace): AuthRecoveryText;
|
|
46
|
+
/**
|
|
47
|
+
* 归档在哪、怎么拿到(手册里唯一随目标不同的部分)。
|
|
48
|
+
* - bucket:S3 兼容桶(CLI 写的 RECOVERY.md);CLI 命令用桶参数
|
|
49
|
+
* - drive:托管 worker 的 Google Drive 目标;开源 CLI 读不了 Drive,不给 CLI 命令
|
|
50
|
+
* - download:托管存储的下载(下载脚本建的文件夹);CLI 命令用 --from-dir(2.12.0 起)
|
|
41
51
|
*/
|
|
42
|
-
export
|
|
52
|
+
export type RecoveryDocSource = {
|
|
53
|
+
kind: "bucket";
|
|
54
|
+
bucket: string;
|
|
55
|
+
endpoint?: string;
|
|
56
|
+
region?: string;
|
|
57
|
+
prefix: string;
|
|
58
|
+
} | {
|
|
59
|
+
kind: "drive";
|
|
60
|
+
folderPath: string;
|
|
61
|
+
};
|
|
62
|
+
/** 托管存储的下载。单独一个类型:2.13.0 导出的 RecoveryDocSource 只有 bucket 和 drive,加进去会破坏下游的穷举判断 */
|
|
63
|
+
export type RecoveryDocDownloadSource = {
|
|
64
|
+
kind: "download";
|
|
65
|
+
};
|
|
66
|
+
export interface RunbookContext {
|
|
67
|
+
snapshot: string;
|
|
68
|
+
projectName: string;
|
|
69
|
+
source: RecoveryDocSource;
|
|
70
|
+
}
|
|
71
|
+
/** 托管存储下载用的上下文;RunbookContext 保持 2.13.0 的样子,下游原有的赋值和判断照样编译 */
|
|
72
|
+
export interface DownloadRunbookContext {
|
|
73
|
+
snapshot: string;
|
|
74
|
+
projectName: string;
|
|
75
|
+
source: RecoveryDocDownloadSource;
|
|
76
|
+
}
|
|
77
|
+
/** 生成手册所需的非秘密快照坐标。刻意不收连接串/密钥类型——签名即防线。 */
|
|
78
|
+
export interface RecoveryDocContext {
|
|
79
|
+
snapshot: string;
|
|
80
|
+
bucket: string;
|
|
81
|
+
endpoint?: string;
|
|
82
|
+
region?: string;
|
|
83
|
+
prefix: string;
|
|
84
|
+
projectName: string;
|
|
85
|
+
}
|
|
86
|
+
/** S3 兼容桶里的手册(CLI 每次备份写进快照前缀) */
|
|
43
87
|
export declare function renderRecoveryDoc(manifest: Manifest, ctx: RecoveryDocContext): string;
|
|
88
|
+
/**
|
|
89
|
+
* RECOVERY.md 全文。三种目标(自有桶、Google Drive、托管下载)共用这一份:恢复步骤、注意事项、授权 SQL
|
|
90
|
+
* 完全相同,只有"文件在哪、怎么拿到"和 CLI 命令的来源参数不同。
|
|
91
|
+
*/
|
|
92
|
+
export declare function renderRunbook(manifest: Manifest, ctx: RunbookContext | DownloadRunbookContext): string;
|
|
93
|
+
export {};
|