fix: Fixed issue with tar file failed with exist 1 (#9261)

This commit is contained in:
CityFun 2025-06-24 16:57:27 +08:00 committed by GitHub
parent 669855de70
commit dd44fdd1f2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 65 additions and 30 deletions

View file

@ -271,6 +271,10 @@ func (u *CronjobService) uploadCronjobBackFile(cronjob model.Cronjob, task *task
cloudSrc := strings.TrimPrefix(file, global.Dir.TmpDir+"/")
for _, account := range accounts {
if len(account) != 0 {
task.LogStart(i18n.GetMsgWithMap("UploadFile", map[string]interface{}{
"file": pathUtils.Join(accountMap[account].backupPath, cloudSrc),
"backup": accountMap[account].name,
}))
_, err := accountMap[account].client.Upload(file, pathUtils.Join(accountMap[account].backupPath, cloudSrc))
task.LogWithStatus(
i18n.GetMsgWithMap("UploadFile", map[string]interface{}{

View file

@ -9,6 +9,7 @@ import (
"os"
"os/exec"
"strings"
"syscall"
"time"
"github.com/1Panel-dev/1Panel/agent/app/task"
@ -17,12 +18,13 @@ import (
)
type CommandHelper struct {
workDir string
outputFile string
scriptPath string
timeout time.Duration
taskItem *task.Task
logger *log.Logger
workDir string
outputFile string
scriptPath string
timeout time.Duration
taskItem *task.Task
logger *log.Logger
IgnoreExist1 bool
}
type Option func(*CommandHelper)
@ -65,6 +67,7 @@ func (c *CommandHelper) RunBashCWithArgs(arg ...string) error {
_, err := c.run("bash", arg...)
return err
}
func (c *CommandHelper) RunBashC(command string) error {
std, err := c.run("bash", "-c", command)
if err != nil {
@ -142,14 +145,15 @@ func (c *CommandHelper) run(name string, arg ...string) (string, error) {
return "", buserr.New("ErrCmdTimeout")
}
if err != nil {
return handleErr(stdout, stderr, err)
return handleErr(stdout, stderr, c.IgnoreExist1, err)
}
return stdout.String(), nil
}
err := cmd.Run()
if err != nil {
return handleErr(stdout, stderr, err)
return handleErr(stdout, stderr, c.IgnoreExist1, err)
}
return stdout.String(), nil
}
@ -184,6 +188,11 @@ func WithScriptPath(scriptPath string) Option {
s.scriptPath = scriptPath
}
}
func WithIgnoreExist1() Option {
return func(s *CommandHelper) {
s.IgnoreExist1 = true
}
}
type CustomWriter struct {
taskItem *task.Task
@ -209,7 +218,15 @@ func (cw *CustomWriter) Flush() {
}
}
func handleErr(stdout, stderr bytes.Buffer, err error) (string, error) {
func handleErr(stdout, stderr bytes.Buffer, ignoreExist1 bool, err error) (string, error) {
var exitError *exec.ExitError
if ignoreExist1 && errors.As(err, &exitError) {
if status, ok := exitError.Sys().(syscall.WaitStatus); ok {
if status.ExitStatus() == 1 {
return "", nil
}
}
}
errMsg := ""
if len(stderr.String()) != 0 {
errMsg = fmt.Sprintf("stderr: %s", stderr.String())

View file

@ -822,14 +822,13 @@ func (f FileOp) TarGzCompressPro(withDir bool, src, dst, secret, exclusionRules
itemPrefix = ""
}
if len(secret) != 0 {
commands = fmt.Sprintf("tar --warning=no-file-changed --ignore-failed-read %s --exclude-from=<(find %s -type s -printf '%s' | sed 's|^|%s/|') -zcf - %s | openssl enc -aes-256-cbc -salt -k '%s' -out %s", exStr, src, "%P\n", itemPrefix, srcItem, secret, dst)
commands = fmt.Sprintf("tar %s -zcf - %s | openssl enc -aes-256-cbc -salt -k '%s' -out %s", exStr, srcItem, secret, dst)
global.LOG.Debug(strings.ReplaceAll(commands, fmt.Sprintf(" %s ", secret), "******"))
} else {
commands = fmt.Sprintf("tar --warning=no-file-changed --ignore-failed-read --exclude-from=<(find %s -type s -printf '%s' | sed 's|^|%s/|') -zcf %s %s %s", src, "%P\n", itemPrefix, dst, exStr, srcItem)
commands = fmt.Sprintf("tar -zcf %s %s %s", dst, exStr, srcItem)
global.LOG.Debug(commands)
}
cmdMgr := cmd.NewCommandMgr(cmd.WithWorkDir(workdir))
cmdMgr := cmd.NewCommandMgr(cmd.WithWorkDir(workdir), cmd.WithIgnoreExist1())
return cmdMgr.RunBashC(commands)
}
@ -846,13 +845,14 @@ func (f FileOp) TarGzFilesWithCompressPro(list []string, dst, secret string) err
}
commands := ""
if len(secret) != 0 {
commands = fmt.Sprintf("tar --warning=no-file-changed --ignore-failed-read -zcf - %s | openssl enc -aes-256-cbc -salt -k '%s' -out %s", strings.Join(filelist, " "), secret, dst)
commands = fmt.Sprintf("tar -zcf - %s | openssl enc -aes-256-cbc -salt -k '%s' -out %s", strings.Join(filelist, " "), secret, dst)
global.LOG.Debug(strings.ReplaceAll(commands, fmt.Sprintf(" %s ", secret), "******"))
} else {
commands = fmt.Sprintf("tar --warning=no-file-changed --ignore-failed-read -zcf %s %s", dst, strings.Join(filelist, " "))
commands = fmt.Sprintf("tar -zcf %s %s", dst, strings.Join(filelist, " "))
global.LOG.Debug(commands)
}
return cmd.RunDefaultBashC(commands)
cmdMgr := cmd.NewCommandMgr(cmd.WithIgnoreExist1())
return cmdMgr.RunBashC(commands)
}
func (f FileOp) TarGzExtractPro(src, dst string, secret string) error {
@ -870,7 +870,7 @@ func (f FileOp) TarGzExtractPro(src, dst string, secret string) error {
commands = fmt.Sprintf("tar zxvf %s", src)
global.LOG.Debug(commands)
}
cmdMgr := cmd.NewCommandMgr(cmd.WithWorkDir(dst))
cmdMgr := cmd.NewCommandMgr(cmd.WithWorkDir(dst), cmd.WithIgnoreExist1())
return cmdMgr.RunBashC(commands)
}
func CopyCustomAppFile(srcPath, dstPath string) error {

View file

@ -9,6 +9,7 @@ import (
"os"
"os/exec"
"strings"
"syscall"
"time"
"github.com/1Panel-dev/1Panel/core/app/task"
@ -17,12 +18,13 @@ import (
)
type CommandHelper struct {
workDir string
outputFile string
scriptPath string
timeout time.Duration
taskItem *task.Task
logger *log.Logger
workDir string
outputFile string
scriptPath string
timeout time.Duration
taskItem *task.Task
logger *log.Logger
IgnoreExist1 bool
}
type Option func(*CommandHelper)
@ -132,14 +134,14 @@ func (c *CommandHelper) run(name string, arg ...string) (string, error) {
return "", buserr.New("ErrCmdTimeout")
}
if err != nil {
return handleErr(stdout, stderr, err)
return handleErr(stdout, stderr, c.IgnoreExist1, err)
}
return stdout.String(), nil
}
err := cmd.Run()
if err != nil {
return handleErr(stdout, stderr, err)
return handleErr(stdout, stderr, c.IgnoreExist1, err)
}
return stdout.String(), nil
}
@ -174,6 +176,11 @@ func WithScriptPath(scriptPath string) Option {
s.scriptPath = scriptPath
}
}
func WithIgnoreExist1() Option {
return func(s *CommandHelper) {
s.IgnoreExist1 = true
}
}
type CustomWriter struct {
taskItem *task.Task
@ -199,7 +206,15 @@ func (cw *CustomWriter) Flush() {
}
}
func handleErr(stdout, stderr bytes.Buffer, err error) (string, error) {
func handleErr(stdout, stderr bytes.Buffer, ignoreExist1 bool, err error) (string, error) {
var exitError *exec.ExitError
if ignoreExist1 && errors.As(err, &exitError) {
if status, ok := exitError.Sys().(syscall.WaitStatus); ok {
if status.ExitStatus() == 1 {
return "", nil
}
}
}
errMsg := ""
if len(stderr.String()) != 0 {
errMsg = fmt.Sprintf("stderr: %s", stderr.String())

View file

@ -83,7 +83,6 @@ func HandleTar(sourceDir, targetDir, name, exclusionRules string, secret string)
exMap := make(map[string]struct{})
excludes := strings.Split(exclusionRules, ",")
excludeRules := ""
excludes = append(excludes, "*.sock")
for _, exclude := range excludes {
if len(exclude) == 0 {
continue
@ -110,13 +109,13 @@ func HandleTar(sourceDir, targetDir, name, exclusionRules string, secret string)
if len(secret) != 0 {
extraCmd := "| openssl enc -aes-256-cbc -salt -k '" + secret + "' -out"
commands = fmt.Sprintf("tar --warning=no-file-changed --ignore-failed-read -zcf %s %s %s %s", " -"+excludeRules, path, extraCmd, targetDir+"/"+name)
commands = fmt.Sprintf("tar -zcf %s %s %s %s", " -"+excludeRules, path, extraCmd, targetDir+"/"+name)
global.LOG.Debug(strings.ReplaceAll(commands, fmt.Sprintf(" %s ", secret), "******"))
} else {
commands = fmt.Sprintf("tar --warning=no-file-changed --ignore-failed-read -zcf %s %s %s", targetDir+"/"+name, excludeRules, path)
commands = fmt.Sprintf("tar -zcf %s %s %s", targetDir+"/"+name, excludeRules, path)
global.LOG.Debug(commands)
}
cmdMgr := cmd.NewCommandMgr(cmd.WithTimeout(24 * time.Hour))
cmdMgr := cmd.NewCommandMgr(cmd.WithTimeout(24*time.Hour), cmd.WithIgnoreExist1())
stdout, err := cmdMgr.RunWithStdoutBashC(commands)
if err != nil {
if len(stdout) != 0 {