mirror of
https://github.com/1Panel-dev/1Panel.git
synced 2025-11-08 18:52:32 +08:00
fix: Optimize filename generation for directory backup (#9669)
Refs #9433
This commit is contained in:
parent
ba03d26b2c
commit
b3d9cc156a
2 changed files with 17 additions and 1 deletions
|
|
@ -629,6 +629,7 @@ func (u *CronjobService) Update(id uint, req dto.CronjobOperate) error {
|
|||
upMap["db_type"] = req.DBType
|
||||
upMap["db_name"] = req.DBName
|
||||
upMap["url"] = req.URL
|
||||
upMap["is_dir"] = req.IsDir
|
||||
upMap["source_dir"] = req.SourceDir
|
||||
upMap["snapshot_rule"] = cronjob.SnapshotRule
|
||||
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ import (
|
|||
"fmt"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
|
@ -199,7 +200,10 @@ func (u *CronjobService) handleDirectory(cronjob model.Cronjob, startTime time.T
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fileName := fmt.Sprintf("%s.tar.gz", startTime.Format(constant.DateTimeSlimLayout)+common.RandStrAndNum(5))
|
||||
fileName := fmt.Sprintf("%s.tar.gz", startTime.Format(constant.DateTimeSlimLayout)+common.RandStrAndNum(2))
|
||||
if cronjob.IsDir || len(strings.Split(cronjob.SourceDir, ",")) == 1 {
|
||||
fileName = loadFileName(cronjob.SourceDir)
|
||||
}
|
||||
backupDir := path.Join(global.Dir.LocalBackupDir, fmt.Sprintf("tmp/%s/%s", cronjob.Type, cronjob.Name))
|
||||
|
||||
fileOp := files.NewFileOp()
|
||||
|
|
@ -548,3 +552,14 @@ func loadSnapWithRule(cronjob model.Cronjob) (dto.SnapshotData, error) {
|
|||
}
|
||||
return itemData, nil
|
||||
}
|
||||
|
||||
func loadFileName(src string) string {
|
||||
dirs := strings.Split(filepath.ToSlash(src), "/")
|
||||
var keyPart string
|
||||
if len(dirs) >= 3 {
|
||||
keyPart = filepath.Join(dirs[len(dirs)-3], dirs[len(dirs)-2], dirs[len(dirs)-1])
|
||||
}
|
||||
cleanName := strings.ReplaceAll(keyPart, string(filepath.Separator), "_")
|
||||
timestamp := time.Now().Format(constant.DateTimeSlimLayout)
|
||||
return fmt.Sprintf("%s_%s_%s.tar.gz", cleanName, timestamp, common.RandStrAndNum(2))
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue