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

View File

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

View File

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

View File

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