1
0
mirror of https://github.com/1Panel-dev/1Panel.git synced 2025-02-28 19:14:13 +08:00

fix: 解决计划任务备份日志报错的问题 (#2786)

This commit is contained in:
ssongliu 2023-11-03 11:18:45 +08:00 committed by GitHub
parent 46320634f6
commit 7e00b46d5c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -598,46 +598,49 @@ func (u *CronjobService) handleSystemClean() (string, error) {
} }
func (u *CronjobService) handleSystemLog(cronjob model.Cronjob, startTime time.Time) (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))) backup, err := backupRepo.Get(commonRepo.WithByID(uint(cronjob.TargetDirID)))
if err != nil { if err != nil {
return "", err return "", err
} }
pathItem := path.Join(global.CONF.System.BaseDir, "1panel/tmp/log", startTime.Format("20060102150405")) 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 { if err != nil {
return "", err return "", err
} }
webItem := path.Join(nginxInstall.GetPath(), "www/sites") if len(websites) != 0 {
for _, website := range websites { nginxInstall, err := getAppInstallByKey(constant.AppOpenresty)
dirItem := path.Join(pathItem, "website", website.Alias) if err != nil {
if _, err := os.Stat(dirItem); err != nil && os.IsNotExist(err) { return "", err
if err = os.MkdirAll(dirItem, os.ModePerm); err != nil {
return "", err
}
} }
itemDir := path.Join(webItem, website.Alias, "log") webItem := path.Join(nginxInstall.GetPath(), "www/sites")
logFiles, _ := os.ReadDir(itemDir) for _, website := range websites {
if len(logFiles) != 0 { dirItem := path.Join(pathItem, "website", website.Alias)
for i := 0; i < len(logFiles); i++ { if _, err := os.Stat(dirItem); err != nil && os.IsNotExist(err) {
if !logFiles[i].IsDir() { if err = os.MkdirAll(dirItem, os.ModePerm); err != nil {
_ = cpBinary([]string{path.Join(itemDir, logFiles[i].Name())}, dirItem) return "", err
} }
} }
} itemDir := path.Join(webItem, website.Alias, "log")
itemDir2 := path.Join(global.CONF.System.Backup, "log/website", website.Alias) logFiles, _ := os.ReadDir(itemDir)
logFiles2, _ := os.ReadDir(itemDir2) if len(logFiles) != 0 {
if len(logFiles2) != 0 { for i := 0; i < len(logFiles); i++ {
for i := 0; i < len(logFiles2); i++ { if !logFiles[i].IsDir() {
if !logFiles2[i].IsDir() { _ = cpBinary([]string{path.Join(itemDir, logFiles[i].Name())}, dirItem)
_ = cpBinary([]string{path.Join(itemDir2, logFiles2[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") 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") loginLogFiles, _ := os.ReadDir("/var/log")
loginDir := path.Join(pathItem, "login") loginDir := path.Join(pathItem, "login")
if _, err := os.Stat(loginDir); err != nil && os.IsNotExist(err) { 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")) fileName := fmt.Sprintf("system_log_%s.tar.gz", startTime.Format("20060102150405"))
if err := handleTar(pathItem, pathItem, fileName, ""); err != nil { if err := handleTar(pathItem, pathItem, fileName, ""); err != nil {