From 7e00b46d5cfcb2dd8c9b82c13666d19402d7ab6e Mon Sep 17 00:00:00 2001 From: ssongliu <73214554+ssongliu@users.noreply.github.com> Date: Fri, 3 Nov 2023 11:18:45 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E8=A7=A3=E5=86=B3=E8=AE=A1=E5=88=92?= =?UTF-8?q?=E4=BB=BB=E5=8A=A1=E5=A4=87=E4=BB=BD=E6=97=A5=E5=BF=97=E6=8A=A5?= =?UTF-8?q?=E9=94=99=E7=9A=84=E9=97=AE=E9=A2=98=20(#2786)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/app/service/cronjob_helper.go | 60 +++++++++++++++------------ 1 file changed, 33 insertions(+), 27 deletions(-) diff --git a/backend/app/service/cronjob_helper.go b/backend/app/service/cronjob_helper.go index 66bc29e1c..eb1c9f4fb 100644 --- a/backend/app/service/cronjob_helper.go +++ b/backend/app/service/cronjob_helper.go @@ -598,46 +598,49 @@ func (u *CronjobService) handleSystemClean() (string, error) { } func (u *CronjobService) handleSystemLog(cronjob model.Cronjob, startTime time.Time) (string, error) { - websites, err := websiteRepo.List() - if err != nil { - return "", err - } backup, err := backupRepo.Get(commonRepo.WithByID(uint(cronjob.TargetDirID))) if err != nil { return "", err } pathItem := path.Join(global.CONF.System.BaseDir, "1panel/tmp/log", startTime.Format("20060102150405")) - nginxInstall, err := getAppInstallByKey(constant.AppOpenresty) + websites, err := websiteRepo.List() if err != nil { return "", err } - webItem := path.Join(nginxInstall.GetPath(), "www/sites") - for _, website := range websites { - dirItem := path.Join(pathItem, "website", website.Alias) - if _, err := os.Stat(dirItem); err != nil && os.IsNotExist(err) { - if err = os.MkdirAll(dirItem, os.ModePerm); err != nil { - return "", err - } + if len(websites) != 0 { + nginxInstall, err := getAppInstallByKey(constant.AppOpenresty) + if err != nil { + return "", err } - itemDir := path.Join(webItem, website.Alias, "log") - logFiles, _ := os.ReadDir(itemDir) - if len(logFiles) != 0 { - for i := 0; i < len(logFiles); i++ { - if !logFiles[i].IsDir() { - _ = cpBinary([]string{path.Join(itemDir, logFiles[i].Name())}, dirItem) - } - } - } - itemDir2 := path.Join(global.CONF.System.Backup, "log/website", website.Alias) - logFiles2, _ := os.ReadDir(itemDir2) - if len(logFiles2) != 0 { - for i := 0; i < len(logFiles2); i++ { - if !logFiles2[i].IsDir() { - _ = cpBinary([]string{path.Join(itemDir2, logFiles2[i].Name())}, dirItem) + webItem := path.Join(nginxInstall.GetPath(), "www/sites") + for _, website := range websites { + dirItem := path.Join(pathItem, "website", website.Alias) + if _, err := os.Stat(dirItem); err != nil && os.IsNotExist(err) { + if err = os.MkdirAll(dirItem, os.ModePerm); err != nil { + return "", err + } + } + itemDir := path.Join(webItem, website.Alias, "log") + logFiles, _ := os.ReadDir(itemDir) + if len(logFiles) != 0 { + for i := 0; i < len(logFiles); i++ { + if !logFiles[i].IsDir() { + _ = cpBinary([]string{path.Join(itemDir, logFiles[i].Name())}, dirItem) + } + } + } + itemDir2 := path.Join(global.CONF.System.Backup, "log/website", website.Alias) + logFiles2, _ := os.ReadDir(itemDir2) + if len(logFiles2) != 0 { + for i := 0; i < len(logFiles2); i++ { + if !logFiles2[i].IsDir() { + _ = cpBinary([]string{path.Join(itemDir2, logFiles2[i].Name())}, dirItem) + } } } } + global.LOG.Debug("backup website log successful!") } systemLogDir := path.Join(global.CONF.System.BaseDir, "1panel/log") @@ -655,6 +658,8 @@ func (u *CronjobService) handleSystemLog(cronjob model.Cronjob, startTime time.T } } } + global.LOG.Debug("backup system log successful!") + loginLogFiles, _ := os.ReadDir("/var/log") loginDir := path.Join(pathItem, "login") if _, err := os.Stat(loginDir); err != nil && os.IsNotExist(err) { @@ -669,6 +674,7 @@ func (u *CronjobService) handleSystemLog(cronjob model.Cronjob, startTime time.T } } } + global.LOG.Debug("backup ssh log successful!") fileName := fmt.Sprintf("system_log_%s.tar.gz", startTime.Format("20060102150405")) if err := handleTar(pathItem, pathItem, fileName, ""); err != nil {