Skip to content

Commit 4225cc8

Browse files
committed
2 parents 1bc981d + 5e38d48 commit 4225cc8

125 files changed

Lines changed: 13118 additions & 244 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
---
2+
name: postgresql-backup-restore
3+
description: Use when 需要在这个仓库或通用 shell 环境中执行 PostgreSQL 备份、恢复、命令行工具安装提示,或快速确认 pg_dump、pg_restore、psql、globals 备份的可靠用法。
4+
argument-hint: "[场景,例如 备份单库 / 恢复 dump / 安装 pg_dump]"
5+
disable-model-invocation: true
6+
---
7+
8+
# PostgreSQL 备份与恢复
9+
10+
用于在这个仓库里快速找到 PostgreSQL 备份、恢复、工具安装和脚本化执行的可靠入口。
11+
12+
## Quick Start
13+
14+
先记住 4 条:
15+
16+
- 单库备份优先用 `pg_dump`
17+
- 恢复 `.sql``psql`
18+
- 恢复 `.dump``.backup``.tar` 和目录格式用 `pg_restore`
19+
- 角色/表空间等 globals 用 `pg_dumpall --globals-only`
20+
21+
常用入口:
22+
23+
```bash
24+
# Bash 辅助脚本
25+
bash ./.codex/skills/postgresql-backup-restore/pg-backup-restore.sh help
26+
bash ./.codex/skills/postgresql-backup-restore/pg-backup-restore.sh install-hint --platform linux --manager apt
27+
PGPASSWORD=secret bash ./.codex/skills/postgresql-backup-restore/pg-backup-restore.sh backup --host 127.0.0.1 --user postgres --database app --output ./app.dump --dry-run
28+
PGPASSWORD=secret bash ./.codex/skills/postgresql-backup-restore/pg-backup-restore.sh restore --host 127.0.0.1 --user postgres --database app_restore --input ./app.dump --clean --if-exists --dry-run
29+
30+
# PowerShell 工具
31+
pwsh -NoProfile -File ./scripts/pwsh/devops/Postgres-Toolkit.ps1 backup --database app --output ./app.dump --format custom --dry-run
32+
pwsh -NoProfile -File ./scripts/pwsh/devops/Postgres-Toolkit.ps1 restore --database app_restore --input ./app.dump --clean --dry-run
33+
```
34+
35+
详细安装命令、globals 备份/恢复、CSV 导入入口见 [reference.md](reference.md)
36+
37+
## When to Use
38+
39+
适用于这些场景:
40+
41+
- 需要快速判断 `.sql``.dump` 应该分别用哪个工具恢复
42+
- 需要给 Linux、macOS、Windows 输出 PostgreSQL CLI 安装命令
43+
- 需要在 Bash 环境里快速拼出 `pg_dump` / `pg_restore` / `psql` 命令
44+
- 需要复用仓库里的 [Postgres-Toolkit.ps1](../../../scripts/pwsh/devops/Postgres-Toolkit.ps1) 或 skill 自带的 [pg-backup-restore.sh](pg-backup-restore.sh)
45+
46+
不适用于这些场景:
47+
48+
- 自动建表、类型推断、迁移 diff
49+
- 长期双向同步或 CDC
50+
- 复杂 DBA 级性能调优
51+
52+
## Quick Reference
53+
54+
| 场景 | 工具 | 推荐入口 |
55+
|---|---|---|
56+
| 备份单个数据库 | `pg_dump` | `Postgres-Toolkit.ps1 backup` / `pg-backup-restore.sh backup` |
57+
| 恢复 SQL 文本 | `psql` | `Postgres-Toolkit.ps1 restore` / `pg-backup-restore.sh restore` |
58+
| 恢复归档或目录 | `pg_restore` | `Postgres-Toolkit.ps1 restore` / `pg-backup-restore.sh restore` |
59+
| 安装 CLI 工具 | 包管理器 | `pg-backup-restore.sh install-hint` |
60+
| CSV 导入现有表 | `psql \copy` | `Postgres-Toolkit.ps1 import-csv` |
61+
62+
## Instructions
63+
64+
1. 先识别输入类型,再选工具。
65+
`.sql -> psql``.dump/.backup/.tar/目录 -> pg_restore`
66+
67+
2. 优先使用不暴露密码的方式。
68+
首选 `PGPASSWORD``.env` 或进程环境变量;避免把密码直接写进 shell 历史。
69+
70+
3. 需要 Bash 环境辅助时,直接调用 [pg-backup-restore.sh](pg-backup-restore.sh)
71+
这个脚本提供 `install-hint``backup``restore` 三个命令,并支持 `--dry-run`
72+
73+
4. 需要更强的跨平台体验时,优先调用 [Postgres-Toolkit.ps1](../../../scripts/pwsh/devops/Postgres-Toolkit.ps1)
74+
它额外支持 `import-csv``install-tools --apply`
75+
76+
## Common Mistakes
77+
78+
-`.sql` 交给 `pg_restore`
79+
- 在非 `directory` 格式下使用并行 `-j`
80+
- 备份或恢复时把密码直接写进命令行
81+
- 只恢复业务库,却漏掉角色/表空间这类 globals

0 commit comments

Comments
 (0)