MCPcopy Index your code
hub / github.com/StevenYangCoder/dbkeeper-core

github.com/StevenYangCoder/dbkeeper-core @v0.3.0

Chat with this repo
repository ↗ · DeepWiki ↗ · release v0.3.0 ↗ · + Follow
216 symbols 558 edges 38 files 183 documented · 85%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

DbKeeper (数据库备份工具)

一、背景

在生产实际环境中,用户可能存在多种数据库、多实例数据库、多数据库的定时备份与恢复需求。例如: - 存在多种数据库,如 MySQL、达梦、PG。 - 同一数据库存在多实例,如达梦在不同 IP/端口/实例。 - 同一 IP 与端口下也可能存在多个数据库。

因此需要一个统一的、多数据库的备份工具,并支持多份保留与多地备份。

二、工具说明

  1. 名称:数据库备份工具(DbKeeper)。
  2. 支持 MySQL、达梦、PG 数据库。
  3. 支持多线程备份,线程池可配置。
  4. 支持配置备份工作目录、数据库 IP、端口、用户、密码、数据库名、数据库类型(MySQL/达梦/PG)。
  5. 支持将备份后的文件迁移到多地进行备份,内置存储支持 local/host/s3/webdav,并支持配置保留数量。
  6. 备份完成后支持通知:目前支持发送指定 通知渠道 请求进行通知。

三、软件架构要求

  1. 使用 DDD 的模式进行开发,领域对象使用充血模型。
  2. 所有文件格式均使用 UTF-8。
  3. 每个文件、每个结构体、每个方法等均要详细备注,包括业务逻辑、方法内主要逻辑、以及使用场景等。
  4. 所有备注、注释、异常提示、日志等均使用中文。
  5. 数据库目前使用 SQLite,后续可能支持其他数据库,所以 SQL 需要写通用 SQL,确保切换数据库时没有切换成本。
  6. 数据库初始化脚本单独文件夹存放,表名、字段名需要中文注释。
  7. 配置文件使用 yaml 格式。
  8. 所有业务组件的 ID 使用雪花算法生成,并转换成字符串。

四、功能点

  1. 配置文件支持如下结构:

点击查看详细配置文件

说明:S3 的 path 需要包含 bucket/前缀,例如 snapshots-bucket/mysql/97_snapshots/

  1. 支持配置多个数据库备份,多个数据库可并发进行备份,受并发参数控制。

补充:数据库导出工具需要预先安装。可选字段 cmd_path 用于指定导出命令路径(如 mysqldumppg_dumpdexp)。

  1. 本地使用 SQLite 保存备份过程中的元数据。

  2. 所有备份文件在导出后,将日志文件和备份文件打包成 tar ,然后压缩为 .zst(Zstandard)格式。

  3. 文件命名规则:{db_type}_{ip}_{port}_{schema}_{version}_{timestamp}.tar.zst

  4. 软件版本号在代码中硬编码维护:internal/appmeta/version.go
  5. 构建时间通过编译参数写入:go build -ldflags "-X dbkeeper-core/internal/appmeta.BuildTime=2026-03-03T16:30:00+08:00"
  6. 导出后先压缩,再计算哈希,然后执行远程备份。

五、构建与使用

构建

PowerShell

$env:CGO_ENABLED="0"; $env:GOOS="linux"; $env:GOARCH="amd64"; $bt=Get-Date -Format "yyyy-MM-ddTHH:mm:sszzz"; go build -trimpath -ldflags "-s -w -X dbkeeper-core/internal/appmeta.BuildTime=$bt" -o "dist\dbkeeper-core-linux-amd64" .

bash

CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -trimpath -ldflags "-s -w -X dbkeeper-core/internal/appmeta.BuildTime=$(date '+%Y-%m-%dT%H:%M:%S%z')" -o dist/dbkeeper-core-linux-amd64 .

如果你想带时区冒号(如 +08:00):

CGO_ENABLED=0 GOOS=linux GOARCH=amd64 \
go build -trimpath \
-ldflags "-s -w -X dbkeeper-core/internal/appmeta.BuildTime=$(date '+%Y-%m-%dT%H:%M:%S%:z')" \
-o dist/dbkeeper-core-linux-amd64 .

特别说明

本软件不包含任何数据库客户端实用程序。

用户必须依据供应商许可安装数据库工具。

使用

  1. 目前该工具仅支持MySQLdmPG

  2. 使用对应数据库时,确保当前系统安装了对应系统的备份工具,如:MySQL使用mysqldumpdm使用dexppg使用pg_dump

  3. 配置文件约定:运行时若不存在 config.yaml,程序会优先从同目录 config-backup.yaml 复制生成;若同目录也不存在,则使用二进制内嵌的 config-backup.yaml 模板生成 config.yaml。因此发布包可以只放二进制,也可以额外放 config-backup.yaml 作为外部可维护模板。

  4. 使用命令

```sh # 设置执行权限 chmod 111 ./dbkeeper-core-linux-amd64

# 执行 ./dbkeeper-core-linux-amd64 -config ./config.yaml ```

解压

备份产物为 .tar.zst 格式(tar 打包 + zstd 压缩),包含数据库导出文件和执行日志。

① 安装 zstd(如果没有)

CentOS / RHEL / Stream 9:

sudo dnf install zstd

Ubuntu / Debian:

sudo apt install zstd

② 解压

一步完成解压和解包:

tar -I zstd -xf file.tar.zst

或分步执行:

zstd -d file.tar.zst   # 解压得到 file.tar
tar xf file.tar         # 解包得到原始文件

Extension points exported contracts — how you extend this code

SnapshotsRegistry (Interface)
SnapshotsRegistry 定义快照适配器注册表接口。 使用场景:根据配置中的 db_type 字段(如 mysql/dm/pg)选择对应的数据库备份实现。 默认实现:internal/infrastructure/snapshot [2 …
internal/application/service.go
Notifier (Interface)
Notifier 定义通知发送接口。 实现该接口即可扩展自定义通知方式。 使用场景:备份完成后调用 Notify() 发送汇总结果, 当前内置 HTTPNotifier 实现。 [1 implementers]
internal/infrastructure/notify/types.go
StorageRegistry (Interface)
StorageRegistry 定义存储适配器注册表接口。 使用场景:根据配置中的 storage.type 字段(如 host/s3/webdav)选择对应的上传实现。 默认实现:internal/infrastructure/stora [2 …
internal/application/service.go
Adapter (Interface)
Adapter 定义数据库备份适配器接口。 所有数据库类型(mysql/dm/pg)必须实现此接口。 使用场景:通过接口多态实现不同数据库的统一备份调用。 扩展方式:实现此接口并通过 Registry 注册,即可支持新的数据库类型。
internal/infrastructure/snapshots/snapshots.go
SnapshotsRepository (Interface)
SnapshotsRepository 定义快照元数据持久化接口。 负责初始化数据库结构和插入备份记录。 使用场景:存储本地和远程备份的元数据信息,支持历史查询和审计。 默认实现:internal/infrastructure/persis [1 …
internal/application/service.go
Adapter (Interface)
Adapter 定义远端存储适配器接口。 所有存储类型(host/s3/webdav)必须实现此接口。 使用场景:通过接口多态实现不同存储后端的统一调用。 扩展方式:实现此接口并通过 Registry 注册,即可支持新的存储类型。
internal/infrastructure/storages/remote.go
IDGenerator (Interface)
IDGenerator 定义 ID 生成器接口。 使用场景:为本地备份记录和远端存储记录生成全局唯一的主键 ID。 默认实现:internal/id.Snowflake(雪花算法) [1 implementers]
internal/application/service.go
Logger (Interface)
Logger 定义结构化日志接口。 使用场景:在备份流程各阶段输出运行日志,支持 key-value 格式。 默认实现:internal/logging.Logger(JSON 格式输出到 info.log 和 error.log) [1 …
internal/application/service.go

Core symbols most depended-on inside this repo

Info
called by 23
internal/application/service.go
Close
called by 19
internal/logging/logger.go
Error
called by 14
internal/application/service.go
Get
called by 10
internal/application/service.go
Warn
called by 9
internal/application/service.go
Do
called by 6
internal/infrastructure/retry/retry.go
normalizeSlashPath
called by 5
internal/infrastructure/storages/remote.go
buildDingTalkBody
called by 5
internal/infrastructure/notify/dingtalk.go

Shape

Function 81
Method 80
Struct 44
Interface 9
TypeAlias 2

Languages

Go100%

Modules by API surface

internal/application/service.go37 symbols
internal/logging/logger.go21 symbols
internal/infrastructure/storages/remote.go14 symbols
internal/config/config.go13 symbols
internal/metrics/metrics.go8 symbols
internal/infrastructure/snapshots/snapshots.go8 symbols
internal/infrastructure/notify/dingtalk_test.go8 symbols
pkg/dbkeeper/runtime.go7 symbols
internal/infrastructure/snapshots/helpers.go7 symbols
internal/infrastructure/retention/retention.go7 symbols
internal/infrastructure/storages/webdav.go6 symbols
internal/infrastructure/storages/s3.go6 symbols

For agents

$ claude mcp add dbkeeper-core \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact