feat:核心数据每次重启进行自动备份

This commit is contained in:
samwaf
2024-01-15 10:27:18 +08:00
parent 0c8be72cdd
commit 919a791754
2 changed files with 41 additions and 7 deletions
+1 -7
View File
@@ -670,13 +670,7 @@
let postdata = {
...that.formEditData
}
if(postdata.remote_host.length==0){
postdata.remote_host = "http://"+postdata.host
}else{
if(postdata.remote_host.startsWith("http://")==false || postdata.remote_host.startsWith("https://")==false ){
postdata.remote_host = "http://"+postdata.remote_host
}
}
postdata['ssl'] = Number(postdata['ssl'])
console.log(postdata)
this.$request
+40
View File
@@ -10,7 +10,10 @@ import (
"SamWaf/utils/zlog"
"fmt"
uuid "github.com/satori/go.uuid"
"io"
"net/url"
"os"
"path/filepath"
"time"
//"github.com/kangarooxin/gorm-plugin-crypto"
@@ -25,6 +28,43 @@ func InitCoreDb(currentDir string) {
}
if global.GWAF_LOCAL_DB == nil {
path := currentDir + "/data/local.db"
// 检查文件是否存在
if _, err := os.Stat(path); !os.IsNotExist(err) {
// 文件存在的逻辑
backupDir := currentDir + "/data/backups"
// 判断备份目录是否存在,不存在则创建
if _, err := os.Stat(backupDir); os.IsNotExist(err) {
if err := os.MkdirAll(backupDir, os.ModePerm); err != nil {
zlog.Error("创建备份目录失败:", err)
return
}
}
// 创建备份文件
backupFilePath := filepath.Join(backupDir, fmt.Sprintf("local_backup_%s.db", time.Now().Format("20060102150405")))
backupFile, err := os.Create(backupFilePath)
if err != nil {
zlog.Error("创建备份文件失败:", err)
return
}
defer backupFile.Close()
// 打开原始文件
originalFile, err := os.Open(path)
if err != nil {
zlog.Error("打开原始文件失败:", err)
return
}
defer originalFile.Close()
// 复制文件内容到备份文件
_, err = io.Copy(backupFile, originalFile)
if err != nil {
zlog.Error("文件复制失败:", err)
return
}
zlog.Info("文件备份成功,备份文件路径:", backupFilePath)
}
key := url.QueryEscape(global.GWAF_PWD_COREDB)
dns := fmt.Sprintf("%s?_db_key=%s", path, key)
db, err := gorm.Open(sqlite.Open(dns), &gorm.Config{})