From fdf9046430fa5557c8422f11621cf1f7e4d90334 Mon Sep 17 00:00:00 2001 From: ssongliu <73214554+ssongliu@users.noreply.github.com> Date: Fri, 2 Feb 2024 10:47:24 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=85=A8=E5=B1=80=E5=A4=87=E4=BB=BD?= =?UTF-8?q?=E7=9B=AE=E5=BD=95=E5=8F=98=E9=87=8F=E7=BB=99=E5=AE=9A=E5=88=9D?= =?UTF-8?q?=E5=A7=8B=E5=80=BC=20(#3787)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/init/hook/hook.go | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/backend/init/hook/hook.go b/backend/init/hook/hook.go index cea498a95..e3b720471 100644 --- a/backend/init/hook/hook.go +++ b/backend/init/hook/hook.go @@ -2,6 +2,8 @@ package hook import ( "encoding/base64" + "encoding/json" + "os" "github.com/1Panel-dev/1Panel/backend/app/model" "github.com/1Panel-dev/1Panel/backend/app/repo" @@ -73,6 +75,7 @@ func Init() { handleCronjobStatus() handleSnapStatus() + loadLocalDir() } func handleSnapStatus() { @@ -140,3 +143,33 @@ func handleCronjobStatus() { "message": "the task was interrupted due to the restart of the 1panel service", }).Error } + +func loadLocalDir() { + var backup model.BackupAccount + _ = global.DB.Where("type = ?", "LOCAL").First(&backup).Error + if backup.ID == 0 { + global.LOG.Errorf("no such backup account `%s` in db", "LOCAL") + return + } + varMap := make(map[string]interface{}) + if err := json.Unmarshal([]byte(backup.Vars), &varMap); err != nil { + global.LOG.Errorf("json unmarshal backup.Vars: %v failed, err: %v", backup.Vars, err) + return + } + if _, ok := varMap["dir"]; !ok { + global.LOG.Error("load local backup dir failed") + return + } + baseDir, ok := varMap["dir"].(string) + if ok { + if _, err := os.Stat(baseDir); err != nil && os.IsNotExist(err) { + if err = os.MkdirAll(baseDir, os.ModePerm); err != nil { + global.LOG.Errorf("mkdir %s failed, err: %v", baseDir, err) + return + } + } + global.CONF.System.Backup = baseDir + return + } + global.LOG.Errorf("error type dir: %T", varMap["dir"]) +}