From 51f11b228452b6b6b7763f7d4bb0e3ff697139f2 Mon Sep 17 00:00:00 2001 From: simonzhangsz Date: Thu, 17 Aug 2023 14:08:10 +0800 Subject: [PATCH] log backup --- features/lm/logbak.go | 28 ++++++++++++++++------------ lib/routes/routes.go | 6 ++++++ restagent/etc/restconf.yaml | 2 +- restagent/restagent.go | 7 +++++++ 4 files changed, 30 insertions(+), 13 deletions(-) diff --git a/features/lm/logbak.go b/features/lm/logbak.go index 284299e9..729c058c 100644 --- a/features/lm/logbak.go +++ b/features/lm/logbak.go @@ -1,4 +1,4 @@ -package dbrest +package lm import ( "fmt" @@ -6,7 +6,7 @@ import ( "strings" "time" - "ems.agt/lib/dborm" + "ems.agt/lib/global" "ems.agt/lib/log" "ems.agt/lib/services" "ems.agt/restagent/config" @@ -44,6 +44,12 @@ type DatabaseClient struct { var DbClient DatabaseClient +// func init() { +// conf := config.GetYamlConfig() +// InitDbClient(conf.Database.Type, conf.Database.User, conf.Database.Password, +// conf.Database.Host, conf.Database.Port, conf.Database.Name) +// } + func InitDbClient(dbType, dbUser, dbPassword, dbHost, dbPort, dbName string) error { DbClient.dbUrl = fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8&parseTime=true&loc=Local", dbUser, dbPassword, dbHost, dbPort, dbName) @@ -103,15 +109,14 @@ func ExtDatabaseBackupData(w http.ResponseWriter, r *http.Request) { var sql string switch tbname { case "operation_log": - filePath := fmt.Sprintf("%s/%s-%s.csv", GetYamlConfig().Database.Backup, tableName, time.Now().Local().Format(global.DateData)) - sql = fmt.Sprintf("select * into outfile '%s' fields terminated by ',' escaped by '' optionally enclosed by '' lines terminated by '\n' from (select 'op_id','account_name','op_ip','subsys_tag','op_type','op_content','op_result','begin_time','end_time','vnf_flag','log_time' union select op_id,account_name,op_ip,subsys_tag,op_type,op_content,op_result,begin_time,end_time,vnf_flag,log_time from operation_log) b -", filePath) - case "secrurity_log": - filePath := fmt.Sprintf("%s/%s-%s.csv", GetYamlConfig().Database.Backup, tableName, time.Now().Local().Format(global.DateData)) + filePath := fmt.Sprintf("%s/%s-%s.csv", config.GetYamlConfig().Database.Backup, tbname, time.Now().Local().Format(global.DateData)) + sql = fmt.Sprintf("select * into outfile '%s' fields terminated by ',' escaped by '' optionally enclosed by '' lines terminated by '\n' from (select 'op_id','account_name','op_ip','subsys_tag','op_type','op_content','op_result','begin_time','end_time','vnf_flag','log_time' union select op_id,account_name,op_ip,subsys_tag,op_type,op_content,op_result,begin_time,end_time,vnf_flag,log_time from operation_log) b", filePath) + case "security_log": + filePath := fmt.Sprintf("%s/%s-%s.csv", config.GetYamlConfig().Database.Backup, tbname, time.Now().Local().Format(global.DateData)) sql = fmt.Sprintf("select * into outfile '%s' fields terminated by ',' escaped by '' optionally enclosed by '' lines terminated by '\n' from (select 'id','account_name','account_type','op_ip','op_type','op_content','op_result','op_time' union select id,account_name,account_type,op_ip,op_type,op_content,op_result,op_time from security_log) b", filePath) case "alarm_log": - filePath := fmt.Sprintf("%s/%s-%s.csv", GetYamlConfig().Database.Backup, tableName, time.Now().Local().Format(global.DateData)) - sql = fmt.Sprintf("select * into outfile 'd:/local.git/be.ems/restagent/database/alarm-log-1.csv' fields terminated by ',' escaped by '' optionally enclosed by '' lines terminated by '\n' from (select 'id','ne_type','ne_id','alarm_seq','alarm_id','alarm_code','alarm_status','event_time','log_time' union select id,ne_type,ne_id,alarm_seq,alarm_id,alarm_code,alarm_status,event_time,log_time from alarm_log) b", filePath) + filePath := fmt.Sprintf("%s/%s-%s.csv", config.GetYamlConfig().Database.Backup, tbname, time.Now().Local().Format(global.DateData)) + sql = fmt.Sprintf("select * into outfile '%s' fields terminated by ',' escaped by '' optionally enclosed by '' lines terminated by '\n' from (select 'id','ne_type','ne_id','alarm_seq','alarm_id','alarm_code','alarm_status','event_time','log_time' union select id,ne_type,ne_id,alarm_seq,alarm_id,alarm_code,alarm_status,event_time,log_time from alarm_log) b", filePath) default: log.Error("error target tale") services.ResponseInternalServerError500DatabaseOperationFailed(w) @@ -124,11 +129,10 @@ func ExtDatabaseBackupData(w http.ResponseWriter, r *http.Request) { services.ResponseInternalServerError500DatabaseOperationFailed(w) return } - n, _ := res.RowsAffected() - affected = affected + n + affected, _ := res.RowsAffected() mapRow := make(map[string]interface{}) row := map[string]interface{}{"affectedRows": affected} - mapRow[tn] = row + mapRow[tbname] = row services.ResponseWithJson(w, http.StatusOK, mapRow) } diff --git a/lib/routes/routes.go b/lib/routes/routes.go index a9695f42..54ffa63c 100644 --- a/lib/routes/routes.go +++ b/lib/routes/routes.go @@ -10,6 +10,7 @@ import ( "ems.agt/features/dbrest" "ems.agt/features/file" "ems.agt/features/fm" + "ems.agt/features/lm" "ems.agt/features/mml" "ems.agt/features/monitor/monitor" "ems.agt/features/monitor/psnet" @@ -235,6 +236,11 @@ func init() { // 数据库连接情况 Register("GET", dbrest.UriDbConnection, dbrest.DbConnection, nil) Register("POST", dbrest.UriDbStop, dbrest.DbStop, nil) + + // 日志表备份 + Register("POST", lm.ExtBackupDataUri, lm.ExtDatabaseBackupData, nil) + Register("POST", lm.CustomExtBackupDataUri, lm.ExtDatabaseBackupData, nil) + } // To resolv rest POST/PUT/DELETE/PATCH cross domain diff --git a/restagent/etc/restconf.yaml b/restagent/etc/restconf.yaml index feab1fca..e4675b8b 100644 --- a/restagent/etc/restconf.yaml +++ b/restagent/etc/restconf.yaml @@ -25,7 +25,7 @@ database: host: 127.0.0.1 port: 33066 name: omc_db - backup: ./database + backup: d:/local.git/be.ems/restagent/database mml: port: 4100 diff --git a/restagent/restagent.go b/restagent/restagent.go index d87f1a50..a348dd91 100644 --- a/restagent/restagent.go +++ b/restagent/restagent.go @@ -14,6 +14,7 @@ import ( "ems.agt/features/dbrest" "ems.agt/features/fm" + "ems.agt/features/lm" "ems.agt/features/monitor/monitor" "ems.agt/features/pm" "ems.agt/restagent/config" @@ -100,6 +101,12 @@ func main() { fmt.Println("rests.initDbClient err:", err) os.Exit(4) } + err = lm.InitDbClient(conf.Database.Type, conf.Database.User, conf.Database.Password, + conf.Database.Host, conf.Database.Port, conf.Database.Name) + if err != nil { + fmt.Println("lm.initDbClient err:", err) + os.Exit(4) + } router := routes.NewRouter()