|
|
4 månader sedan | |
|---|---|---|
| .. | ||
| README.md | 4 månader sedan | |
| build.sh | 4 månader sedan | |
| cleandb.go | 4 månader sedan | |
| config.json | 4 månader sedan | |
| main.go | 4 månader sedan | |
这是一个基于 Go 语言编写的命令行工具,专门用于通过 SSH 隧道 安全地连接并清理远程 MongoDB 和 Redis 数据库。
ssh.host,工具将尝试直接连接数据库。适用于本地或直接可达的网络环境。dev, test, prod),轻松切换。* 通配符进行批量模式匹配删除。cleandb.go: 核心连接逻辑(SSH 隧道与数据库拨号器)。main.go: 命令行程序入口,负责配置加载、交互逻辑及执行清理任务。config.json: 配置文件模版。golang.org/x/crypto/sshgo.mongodb.org/mongo-driver/v2github.com/go-redis/redis/v8工具内置了 build.sh 脚本,可以一键编译出适用于 Windows、Linux 和 macOS 的版本:
# 赋予执行权限
chmod +x tool/cleandb/build.sh
# 执行编译
./tool/cleandb/build.sh
编译成功后,可执行文件将生成在 tool/cleandb/publish 目录下。
在项目根目录下执行:
go build -o tool/cleandb/cleandb.exe tool/cleandb/*.go
config.json)配置文件采用环境名为 Key 的 JSON 结构。每个环境包含 ssh、mongo 和 redis 三大核心配置块。
| 配置路径 | 类型 | 必填 | 说明 |
|---|---|---|---|
[env].ssh.host |
string |
否 | SSH 跳板机的 IP 地址。若为空则直连数据库 |
[env].ssh.port |
int |
是 | SSH 服务端口 (通常为 22) |
[env].ssh.user |
string |
是 | SSH 登录用户名 |
[env].ssh.password |
string |
否 | SSH 登录密码 (暂未支持密钥登录,如需请留空并联系开发) |
[env].mongo.config.host |
string |
是 | 数据库在远程内网中的 IP 或 127.0.0.1 |
[env].mongo.config.port |
int |
是 | MongoDB 端口 (通常为 27017) |
[env].mongo.config.user |
string |
否 | MongoDB 用户名 |
[env].mongo.config.password |
string |
否 | MongoDB 密码 |
[env].mongo.config.auth_source |
string |
否 | 认证数据库,默认为 admin |
[env].mongo.drop_databases |
[]string |
否 | 待清理列表:包含在该数组内的数据库将被执行 Drop |
[env].redis.config.host |
string |
是 | Redis 在远程内网中的 IP 或 127.0.0.1 |
[env].redis.config.port |
int |
是 | Redis 端口 (通常为 6379) |
[env].redis.config.user |
string |
否 | Redis 6.0+ 用户名 (可选) |
[env].redis.config.password |
string |
否 | Redis 认证密码 |
[env].redis.delete_keys |
[]string |
否 | 待清理列表:支持具体 Key 名或带 * 的通配符模式 |
{
"dev": {
"ssh": {
"host": "47.115.229.244",
"port": 22,
"user": "root",
"password": "ssh_password"
},
"mongo": {
"config": {
"host": "172.25.236.27",
"port": 27017,
"user": "f1user",
"password": "mongo_password",
"auth_source": "admin"
},
"drop_databases": ["su-f1-game"]
},
"redis": {
"config": {
"host": "172.25.236.27",
"port": 46379,
"password": "redis_password"
},
"delete_keys": ["test-center:bind-uid:*"]
}
}
}
工具采用 位置参数 执行:
# 格式: ./cleandb <配置文件路径> <环境名称>
./tool/cleandb/cleandb.exe tool/cleandb/config.json dev
# 默认加载当前目录下的 config.json,仅需提供环境名称
./tool/cleandb/cleandb.exe dev
参数说明:
configPath: 配置文件路径。envName: 环境名称(对应配置文件中的顶级 Key)。[!CAUTION]
- 此工具执行的是物理删除操作,操作前请务必确认环境参数正确。
- 请勿将包含真实密码的
config.json提交至代码版本控制仓库。- SSH 隧道在连接不稳定时可能会导致 Handshake 失败,请确保网络状况良好。