fix: fix backup retention count not taking effect during script updates (#12896)

This commit is contained in:
ssongliu
2026-05-29 11:41:57 +08:00
committed by GitHub
parent 9a2b7a7775
commit 8693cc17df
5 changed files with 69 additions and 58 deletions

View File

@@ -7,7 +7,6 @@ import (
"net/http"
"os"
"path"
"sort"
"strconv"
"strings"
"syscall"
@@ -24,6 +23,7 @@ import (
"github.com/1Panel-dev/1Panel/core/utils/ctl_conf"
"github.com/1Panel-dev/1Panel/core/utils/files"
"github.com/1Panel-dev/1Panel/core/utils/req_helper"
upgradeUtil "github.com/1Panel-dev/1Panel/core/utils/upgrade"
"github.com/1Panel-dev/1Panel/core/utils/xpack"
)
@@ -574,29 +574,7 @@ func loadArch() (string, error) {
func dropBackupCopies() {
backupCopies, _ := settingRepo.GetValueByKey("UpgradeBackupCopies")
copies, _ := strconv.Atoi(backupCopies)
if copies == 0 {
return
}
backupDir := path.Join(global.CONF.Base.InstallDir, "1panel/tmp/upgrade")
upgradeDir, err := os.ReadDir(backupDir)
if err != nil {
if err := upgradeUtil.DropBackupCopies(global.CONF.Base.InstallDir, backupCopies); err != nil {
global.LOG.Errorf("read upgrade dir failed, err: %v", err)
return
}
var versions []string
for _, item := range upgradeDir {
if item.IsDir() && strings.HasPrefix(item.Name(), "v") {
versions = append(versions, item.Name())
}
}
if len(versions) <= copies {
return
}
sort.Slice(versions, func(i, j int) bool {
return common.ComparePanelVersion(versions[i], versions[j])
})
for i := copies; i < len(versions); i++ {
_ = os.RemoveAll(backupDir + "/" + versions[i])
}
}

View File

@@ -4,12 +4,11 @@ import (
"fmt"
"os/user"
"path"
"strings"
"time"
"github.com/1Panel-dev/1Panel/core/server"
"github.com/1Panel-dev/1Panel/core/utils/common"
"github.com/1Panel-dev/1Panel/core/utils/ctl_conf"
"github.com/glebarez/sqlite"
"github.com/spf13/cobra"
"gorm.io/gorm"
)
@@ -38,24 +37,29 @@ type setting struct {
}
func loadDBConn(dbName string) (*gorm.DB, error) {
baseDir, err := ctl_conf.LoadFromFile("/usr/local/bin/1pctl", "BASE_DIR")
baseDir, err := loadBaseDir()
if err != nil {
return nil, fmt.Errorf("handle load `BASE_DIR` failed, err: %v", err)
}
if len(baseDir) == 0 {
return nil, fmt.Errorf("error `BASE_DIR` find in /usr/local/bin/1pctl")
}
if strings.HasSuffix(baseDir, "/") {
baseDir = baseDir[:strings.LastIndex(baseDir, "/")]
return nil, err
}
db, err := gorm.Open(sqlite.Open(path.Join(baseDir, "1panel/db", dbName)), &gorm.Config{})
db, err := common.GetDBWithPath(path.Join(baseDir, "1panel/db", dbName))
if err != nil {
return nil, fmt.Errorf("init my db conn failed, err: %v", err)
}
return db, nil
}
func loadBaseDir() (string, error) {
baseDir, err := ctl_conf.LoadFromFile("/usr/local/bin/1pctl", "BASE_DIR")
if err != nil {
return "", fmt.Errorf("handle load `BASE_DIR` failed, err: %v", err)
}
if len(baseDir) == 0 {
return "", fmt.Errorf("error `BASE_DIR` find in /usr/local/bin/1pctl")
}
return baseDir, nil
}
func getSettingByKey(db *gorm.DB, key string) string {
var setting setting
_ = db.Where("key = ?", key).First(&setting).Error

View File

@@ -16,6 +16,7 @@ import (
"github.com/1Panel-dev/1Panel/core/utils/cmd"
"github.com/1Panel-dev/1Panel/core/utils/common"
"github.com/1Panel-dev/1Panel/core/utils/encrypt"
upgradeUtil "github.com/1Panel-dev/1Panel/core/utils/upgrade"
"github.com/spf13/cobra"
"golang.org/x/term"
"gopkg.in/yaml.v3"
@@ -164,6 +165,7 @@ var updateVersion = &cobra.Command{
fmt.Println(i18n.GetMsgWithMapForCmd("UpdateUserErr", map[string]interface{}{"err": err.Error()}))
return err
}
defer dropUpgradeBackupCopies(db)
xpackDB, err := loadDBConn("xpack.db")
if err != nil {
@@ -188,6 +190,17 @@ var updateVersion = &cobra.Command{
},
}
func dropUpgradeBackupCopies(db *gorm.DB) {
baseDir, err := loadBaseDir()
if err != nil {
fmt.Println(err.Error())
return
}
if err := upgradeUtil.DropBackupCopies(baseDir, getSettingByKey(db, "UpgradeBackupCopies")); err != nil {
fmt.Printf("read upgrade dir failed, err: %v\n", err)
}
}
func username() {
reader := bufio.NewReader(os.Stdin)
fmt.Print(i18n.GetMsgByKeyForCmd("UpdateUser") + ": ")

View File

@@ -1,23 +0,0 @@
package cmd
import "testing"
// TestWhich_ExistingBinary verifies that Which() returns true for a
// binary that is guaranteed to exist on every Unix-like build host
// (`sh`). Regression test for #12605: the previous implementation
// shelled out to `which`, which is not always available on minimal
// distributions like Arch Linux. The new implementation tries
// exec.LookPath first, so this assertion holds regardless of whether
// `which` itself is on PATH.
func TestWhich_ExistingBinary(t *testing.T) {
if !Which("sh") {
t.Errorf("Which(\"sh\") = false, want true")
}
}
func TestWhich_MissingBinary(t *testing.T) {
// A binary name that is extremely unlikely to exist on any host.
if Which("definitely-not-a-real-binary-xyzzy-1panel") {
t.Errorf("Which(\"definitely-not-a-real-binary-xyzzy-1panel\") = true, want false")
}
}

View File

@@ -0,0 +1,39 @@
package upgrade
import (
"os"
"path"
"sort"
"strconv"
"strings"
"github.com/1Panel-dev/1Panel/core/utils/common"
)
func DropBackupCopies(installDir, backupCopies string) error {
copies, _ := strconv.Atoi(backupCopies)
if copies == 0 {
return nil
}
backupDir := path.Join(installDir, "1panel/tmp/upgrade")
upgradeDir, err := os.ReadDir(backupDir)
if err != nil {
return err
}
var versions []string
for _, item := range upgradeDir {
if item.IsDir() && strings.HasPrefix(item.Name(), "v") {
versions = append(versions, item.Name())
}
}
if len(versions) <= copies {
return nil
}
sort.Slice(versions, func(i, j int) bool {
return common.ComparePanelVersion(versions[i], versions[j])
})
for i := copies; i < len(versions); i++ {
_ = os.RemoveAll(path.Join(backupDir, versions[i]))
}
return nil
}