提取最小化、引用完整的数据子集,用于本地开发和调试。
将整个生产数据库复制到本地机器是不可行的。但复现一个 bug 通常需要导致该问题的确切数据。dbslice 通过仅提取所需的记录,并遵循外键关系确保引用完整性,解决了这一问题。
# 全局安装
uv tool install dbslice # 或: pip install dbslice
# 提取一个订单及其所有相关记录
dbslice extract postgres://localhost/myapp --seed "orders.id=12345" > subset.sql
# 导入本地数据库
psql -d localdb < subset.sql
| 数据库 | 状态 |
|---|---|
| PostgreSQL | 完全支持 |
| MySQL | 计划中(尚未实现) |
| SQLite | 计划中(尚未实现) |
# 使用 uv 安装(推荐)
uv add dbslice
# 不安装直接试用
uvx dbslice --help
# 或使用 pip
pip install dbslice
# 按主键提取
dbslice extract postgres://user:pass@host:5432/db --seed "orders.id=12345"
# 使用 WHERE 子句提取
dbslice extract postgres://localhost/db --seed "orders:status='failed' AND created_at > '2024-01-01'"
# 多个种子
dbslice extract postgres://localhost/db \
--seed "orders.id=100" \
--seed "orders.id=101"
# 限制深度(默认:3)
dbslice extract postgres://... --seed "orders.id=1" --depth 2
# 方向:up(仅父表)、down(仅子表)、both(默认)
dbslice extract postgres://... --seed "orders.id=1" --direction up
# 自动匿名化检测到的敏感字段
dbslice extract postgres://... --seed "users.id=1" --anonymize
# 编校其他字段
dbslice extract postgres://... --seed "users.id=1" --anonymize --redact "audit_logs.ip_address"
在本地浏览器界面中可视化映射列、应用合规配置文件并生成即用型配置。
dbslice map postgres://localhost/myapp
# 自定义端口
dbslice map postgres://localhost/myapp --port 8888
# 也可用 uvx(无需安装)
uvx dbslice map postgres://localhost/myapp
| 将列映射到匿名化规则 | 生成并导出配置 |
![]() | ![]() |
运行在 127.0.0.1:9473,配有一次性的会话令牌——数据不会离开你的机器。一键应用 GDPR、HIPAA 或 PCI-DSS 配置文件,审查哪些内容会被屏蔽,然后下载 YAML。
# HIPAA 安全港 — 自动屏蔽全部 18 类标识符
dbslice extract postgres://... --seed "patients.id=1" --compliance hipaa --compliance-strict
# 多个配置文件 + 审计清单
dbslice extract postgres://... --seed "users.id=1" --compliance gdpr --compliance pci-dss -f subset.sql
# 生成 subset.sql + subset.manifest.json
# SQL(默认)
dbslice extract postgres://... --seed "orders.id=1" --output sql
# JSON 固件
dbslice extract postgres://... --seed "orders.id=1" --output json --out-file fixtures/
# CSV
dbslice extract postgres://... --seed "orders.id=1" --output csv --out-file data/
对于数据库模式中未定义的关系(Django 通用外键、隐式关系):
# dbslice.yaml
database:
url: postgres://localhost:5432/myapp
virtual_foreign_keys:
- source_table: notifications
source_columns: [object_id]
target_table: orders
description: "通过 ContentType 到 orders 的通用外键"
- source_table: audit_log
source_columns: [user_id]
target_table: users
description: "无数据库约束的隐式外键"
dbslice extract --config dbslice.yaml --seed "users.id=1"
dbslice inspect postgres://localhost/myapp
# 从数据库生成配置
dbslice init postgres://localhost/myapp --out-file dbslice.yaml
# 使用配置
dbslice extract --config dbslice.yaml --seed "orders.id=12345"
dbslice 是轻量级、零配置的 Python 方案:一分钟内完成安装和提取。
git clone https://github.com/nabroleonx/dbslice.git
cd dbslice
uv sync --dev
uv run pytest
MIT
| 功能 | dbslice | Jailer | Greenmask | slice-db |
|---|
| 语言 | Python | Java | Go | Ruby |
| 配置 | 零配置 | 需要模型文件 | 需要配置 | 手动 YAML |
| 设置时间 | 秒级 | 小时级 | 中等 | 中等 |
| 匿名化 | 内置(Faker) | 基于插件 | 高级转换器 | 不支持 |
| 合规配置文件 | GDPR, HIPAA, PCI-DSS | 无 | 无 | 无 |
| 列映射 UI | 内置(本地) | 无 | 无 | 无 |
| PII 值扫描 | 两阶段(掩码前/后) | 无 | 无 | 无 |
| 子集提取 | 外键遍历 | 外键遍历 | 有限 | 外键遍历 |
| 输出格式 | SQL, JSON, CSV | SQL, XML, CSV | SQL | 仅 SQL |
| 循环处理 | 自动 | 手动配置 | 不适用 | 手动 |
| 流式处理 | 内置 | 可配置 | 内置 | 不支持 |
| 维护状态 | 活跃 | 活跃 | 活跃 | 停止维护 |