This is an automated email from the ASF dual-hosted git repository.

tuhaihe pushed a commit to branch REL_2_STABLE
in repository https://gitbox.apache.org/repos/asf/cloudberry-backup.git

commit abeb027ec32f91f112b87f1cd46419128305c06e
Author: woblerr <[email protected]>
AuthorDate: Sun Mar 22 15:58:17 2026 +0300

    Refactor backup deletion commands to use direct SSH execution.
---
 go.mod                         |  2 +-
 go.sum                         |  2 --
 gpbackman/cmd/backup_delete.go | 77 +++++++-----------------------------------
 3 files changed, 13 insertions(+), 68 deletions(-)

diff --git a/go.mod b/go.mod
index 463ee9e4..3adaf23e 100644
--- a/go.mod
+++ b/go.mod
@@ -23,7 +23,6 @@ require (
        github.com/spf13/cobra v1.6.1
        github.com/spf13/pflag v1.0.5
        github.com/urfave/cli v1.22.13
-       golang.org/x/crypto v0.21.0
        golang.org/x/sys v0.18.0
        golang.org/x/tools v0.12.0
        gopkg.in/cheggaaa/pb.v1 v1.0.28
@@ -50,6 +49,7 @@ require (
        github.com/mattn/go-runewidth v0.0.13 // indirect
        github.com/rivo/uniseg v0.2.0 // indirect
        github.com/russross/blackfriday/v2 v2.1.0 // indirect
+       golang.org/x/crypto v0.21.0 // indirect
        golang.org/x/mod v0.12.0 // indirect
        golang.org/x/net v0.23.0 // indirect
        golang.org/x/text v0.14.0 // indirect
diff --git a/go.sum b/go.sum
index 7495cb68..ca12ca24 100644
--- a/go.sum
+++ b/go.sum
@@ -252,8 +252,6 @@ golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod 
h1:Nr5EML6q2oocZ2LXR
 golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod 
h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
 golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod 
h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
 golang.org/x/term v0.1.0/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
-golang.org/x/term v0.18.0 h1:FcHjZXDMxI8mM3nwhX9HlKop4C0YQvCVCdwYl2wOtE8=
-golang.org/x/term v0.18.0/go.mod 
h1:ILwASektA3OnRv7amZ1xhE/KTR+u50pbXfZ03+6Nx58=
 golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
 golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
 golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
diff --git a/gpbackman/cmd/backup_delete.go b/gpbackman/cmd/backup_delete.go
index bae6bdf3..34647d93 100644
--- a/gpbackman/cmd/backup_delete.go
+++ b/gpbackman/cmd/backup_delete.go
@@ -8,9 +8,6 @@ import (
        "os/exec"
        "strconv"
        "sync"
-       "time"
-
-       "golang.org/x/crypto/ssh"
 
        "github.com/apache/cloudberry-go-libs/gplog"
        "github.com/apache/cloudberry-go-libs/operating"
@@ -432,10 +429,8 @@ func executeDeleteBackupOnSegments(backupDir, 
backupDataBackupDir, backupName, s
        limit := make(chan bool, maxParallelProcesses)
        wg := &sync.WaitGroup{}
        errCh := make(chan error, len(configs))
-       sshClientConf, err := getSSHConfig()
-       if err != nil {
-               return err
-       }
+       currentUser, _ := operating.System.CurrentUser()
+       userName := currentUser.Username
        // Check that the directory exists on all segment hosts.
        for _, config := range configs {
                wg.Add(1)
@@ -447,7 +442,7 @@ func executeDeleteBackupOnSegments(backupDir, 
backupDataBackupDir, backupName, s
                go func(backupPath, host string) {
                        defer func() { <-limit }()
                        defer wg.Done()
-                       
checkBackupDirExistsOnSegments(gpbckpconfig.BackupDirPath(backupPath, 
backupName), host, sshClientConf, errCh)
+                       
checkBackupDirExistsOnSegments(gpbckpconfig.BackupDirPath(backupPath, 
backupName), host, userName, errCh)
                }(backupPath, config.Hostname)
        }
        // We should block the main function and wait for the WaitGroup to 
complete.
@@ -483,7 +478,7 @@ func executeDeleteBackupOnSegments(backupDir, 
backupDataBackupDir, backupName, s
                go func(backupPath, host string) {
                        defer func() { <-limit }()
                        defer wg.Done()
-                       
deleteBackupDirOnSegments(gpbckpconfig.BackupDirPath(backupPath, backupName), 
host, sshClientConf, errCh)
+                       
deleteBackupDirOnSegments(gpbckpconfig.BackupDirPath(backupPath, backupName), 
host, userName, errCh)
                }(backupPath, config.Hostname)
        }
        wg.Wait()
@@ -498,23 +493,15 @@ func executeDeleteBackupOnSegments(backupDir, 
backupDataBackupDir, backupName, s
        }
        return nil
 }
-func checkBackupDirExistsOnSegments(path, host string, sshConf 
*ssh.ClientConfig, errCh chan error) {
-       connection, err := ssh.Dial("tcp", host+":22", sshConf)
-       if err != nil {
-               errCh <- err
-               return
-       }
-       defer connection.Close()
+func runSSHCommand(remoteCmd, host, userName string) ([]byte, error) {
+       cmd := exec.Command("ssh", "-o", "StrictHostKeyChecking=no", 
fmt.Sprintf("%s@%s", userName, host), remoteCmd)
+       return cmd.CombinedOutput()
+}
 
-       session, err := connection.NewSession()
-       if err != nil {
-               errCh <- err
-               return
-       }
-       defer session.Close()
+func checkBackupDirExistsOnSegments(path, host, userName string, errCh chan 
error) {
        command := fmt.Sprintf("test -d %s", path)
        gplog.Debug("%s", textmsg.InfoTextCommandExecution(command, "on host", 
host))
-       if err := session.Run(command); err != nil {
+       if _, err := runSSHCommand(command, host, userName); err != nil {
                gplog.Error("%s", textmsg.ErrorTextCommandExecutionFailed(err, 
command, "on host", host))
                errCh <- textmsg.ErrorNotFoundBackupDirIn(fmt.Sprintf("%s on 
host %s", path, host))
                return
@@ -522,53 +509,13 @@ func checkBackupDirExistsOnSegments(path, host string, 
sshConf *ssh.ClientConfig
        gplog.Debug("%s", textmsg.InfoTextCommandExecutionSucceeded(command, 
"on host", host))
 }
 
-func deleteBackupDirOnSegments(path, host string, sshConf *ssh.ClientConfig, 
errCh chan error) {
-       connection, err := ssh.Dial("tcp", host+":22", sshConf)
-       if err != nil {
-               errCh <- err
-               return
-       }
-       defer connection.Close()
-
-       session, err := connection.NewSession()
-       if err != nil {
-               errCh <- err
-               return
-       }
-       defer session.Close()
+func deleteBackupDirOnSegments(path, host, userName string, errCh chan error) {
        command := fmt.Sprintf("rm -rf %s", path)
        gplog.Debug("%s", textmsg.InfoTextCommandExecution(command, "on host", 
host))
-       if err := session.Run(command); err != nil {
+       if _, err := runSSHCommand(command, host, userName); err != nil {
                gplog.Error("%s", textmsg.ErrorTextCommandExecutionFailed(err, 
command, "on host", host))
                errCh <- err
                return
        }
        gplog.Debug("%s", textmsg.InfoTextCommandExecutionSucceeded(command, 
"on host", host))
 }
-
-func getSSHConfig() (*ssh.ClientConfig, error) {
-       currentUser, _ := operating.System.CurrentUser()
-       key, err := os.ReadFile(currentUser.HomeDir + "/.ssh/id_rsa")
-       if err != nil {
-               return nil, err
-       }
-       signer, err := ssh.ParsePrivateKey(key)
-       if err != nil {
-               return nil, err
-       }
-       // sshConfig is a configuration object for establishing an SSH 
connection.
-       // It contains the user's username, authentication method using public 
keys,
-       // and a host key callback that ignores insecure host keys.
-       sshConfig := &ssh.ClientConfig{
-               User: currentUser.Username,
-               Auth: []ssh.AuthMethod{
-                       ssh.PublicKeys(signer),
-               },
-               // Disable known_hosts check.
-               // This check also disables in gpbackup utility.
-               // #nosec G106
-               HostKeyCallback: ssh.InsecureIgnoreHostKey(),
-               Timeout:         30 * time.Second,
-       }
-       return sshConfig, nil
-}


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to