Skip to content

Commit

Permalink
feat(dbm-services): 清理dump出来的表结构文件 TencentBlueKing#8281
Browse files Browse the repository at this point in the history
  • Loading branch information
ymakedaq committed Nov 29, 2024
1 parent af60ce0 commit 27f30a2
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,14 @@ type SemanticDumpSchemaComp struct {

// DumpSchemaParam TODO
type DumpSchemaParam struct {
Host string `json:"host" validate:"required,ip"` // 当前实例的主机地址
Port int `json:"port" validate:"required,lt=65536,gte=3306"` // 当前实例的端口
CharSet string `json:"charset" validate:"required,checkCharset"` // 字符集参数
CompressPkgName string `json:"com"`
// 当前实例的主机地址
Host string `json:"host" validate:"required,ip"`
// 当前实例的端口
Port int `json:"port" validate:"required,lt=65536,gte=3306"`
// 字符集参数
CharSet string `json:"charset" validate:"required,checkCharset"`
// 备份文件名后缀,清理相关文件
BackupFileNameSuffix string `json:"backup_file_name_suffix" validate:"required"`
UploadBkRepoParam
}

Expand Down Expand Up @@ -88,12 +92,29 @@ func (c *SemanticDumpSchemaComp) Example() interface{} {
}
return comp
}
func (c *SemanticDumpSchemaComp) cleanHistorySchemaFile() {
if c.Params.BackupFileNameSuffix == "" || c.Params.BackupDir == "" {
return
}
cleanCmd := fmt.Sprintf(`find %s -name "*%s.sql*" -type f -mtime +7 -delete `, c.Params.BackupDir,
c.Params.BackupFileNameSuffix)
logger.Warn("delete before 7 days dump schema file")
logger.Warn("will execute: %s", cleanCmd)
out, err := osutil.StandardShellCommand(false, cleanCmd)
if err != nil {
logger.Error("clean schema file failed:%s,out:%s", err.Error(), out)
return
}
logger.Warn("clean schema file success")
}

// Init init
//
// @receiver c
// @return err
func (c *SemanticDumpSchemaComp) Init() (err error) {
// 1. clean history schema file
c.cleanHistorySchemaFile()
conn, err := native.InsObject{
Host: c.Params.Host,
Port: c.Params.Port,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ def __init__(self, root_id: str, data: Optional[Dict]):
self.uid = self.data["uid"]

# 定义好每次语义检测的库表备份文件名称
self.semantic_dump_schema_file_name = f"{self.root_id}_semantic_dump_schema.sql"
self.semantic_dump_schema_file_name_suffix = "_semantic_dump_schema"
self.semantic_dump_schema_file_name = f"{self.root_id}{self.semantic_dump_schema_file_name_suffix}.sql"

# 定义SQL文件的下发位置
self.sql_path = os.path.join(consts.BK_PKG_INSTALL_PATH, f"sqlfile_{self.uid}") + "/"
Expand Down Expand Up @@ -188,7 +189,7 @@ def sql_semantic_check_flow(self):
)
),
)

template_cluster["semantic_dump_schema_file_name_suffix"] = self.semantic_dump_schema_file_name_suffix
semantic_check_pipeline.add_act(
act_name=_("备份测试库表结构"),
act_component_code=ExecuteDBActuatorScriptComponent.code,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ def __init__(self, root_id: str, data: Optional[Dict]):
self.uid = self.data["uid"]

# 定义好每次语义检测的库表备份文件名称
self.semantic_dump_schema_file_name = f"{self.root_id}_semantic_dump_schema.sql"

self.semantic_dump_schema_file_name_suffix = "_semantic_dump_schema"
self.semantic_dump_schema_file_name = f"{self.root_id}{self.semantic_dump_schema_file_name_suffix}.sql"
# 定义SQL文件的下发位置
self.sql_path = os.path.join(consts.BK_PKG_INSTALL_PATH, f"sqlfile_{self.uid}") + "/"
self.data["sql_path"] = self.sql_path
Expand Down Expand Up @@ -174,7 +174,7 @@ def sql_semantic_check_flow(self):
)
),
)

cluster["semantic_dump_schema_file_name_suffix"] = self.semantic_dump_schema_file_name_suffix
semantic_check_pipeline.add_act(
act_name=_("备份测试库表结构"),
act_component_code=ExecuteDBActuatorScriptComponent.code,
Expand Down
1 change: 1 addition & 0 deletions dbm-ui/backend/flow/utils/mysql/mysql_act_playload.py
Original file line number Diff line number Diff line change
Expand Up @@ -890,6 +890,7 @@ def get_semantic_dump_schema_payload(self, **kwargs):
"port": self.cluster["port"],
"charset": self.ticket_data["charset"],
"backup_file_name": f"{self.cluster['semantic_dump_schema_file_name']}",
"backup_file_name_suffix": f"{self.cluster['semantic_dump_schema_file_name_suffix']}",
"backup_dir": BK_PKG_INSTALL_PATH,
"fileserver": {
"url": get_bk_repo_url(bk_cloud_id),
Expand Down

0 comments on commit 27f30a2

Please sign in to comment.