log backup

This commit is contained in:
2023-08-17 14:08:10 +08:00
parent 96b399c31b
commit 51f11b2284
4 changed files with 30 additions and 13 deletions

View File

@@ -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)
}