fix: 解决应用、网站备份恢复失败的问题 (#2266)

This commit is contained in:
ssongliu 2023-09-12 17:48:11 +08:00 committed by GitHub
parent 3a0b3e952b
commit 327e160ea3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 20 additions and 9 deletions

View File

@ -5,7 +5,6 @@ import (
"encoding/base64" "encoding/base64"
"encoding/json" "encoding/json"
"fmt" "fmt"
"github.com/1Panel-dev/1Panel/backend/utils/cmd"
"math" "math"
"net/http" "net/http"
"os" "os"
@ -16,6 +15,8 @@ import (
"strconv" "strconv"
"strings" "strings"
"github.com/1Panel-dev/1Panel/backend/utils/cmd"
"github.com/1Panel-dev/1Panel/backend/app/api/v1/helper" "github.com/1Panel-dev/1Panel/backend/app/api/v1/helper"
"github.com/1Panel-dev/1Panel/backend/app/dto/request" "github.com/1Panel-dev/1Panel/backend/app/dto/request"
"github.com/1Panel-dev/1Panel/backend/i18n" "github.com/1Panel-dev/1Panel/backend/i18n"
@ -294,9 +295,11 @@ func deleteLink(ctx context.Context, install *model.AppInstall, deleteDB bool, f
continue continue
} }
if err := mysqlService.Delete(ctx, dto.MysqlDBDelete{ if err := mysqlService.Delete(ctx, dto.MysqlDBDelete{
ID: database.ID, ID: database.ID,
ForceDelete: forceDelete, ForceDelete: forceDelete,
Database: database.MysqlName, DeleteBackup: true,
Type: re.Key,
Database: database.MysqlName,
}); err != nil && !forceDelete { }); err != nil && !forceDelete {
return err return err
} }

View File

@ -173,8 +173,15 @@ func handleAppRecover(install *model.AppInstall, recoverFile string, isRollback
newEnvFile := "" newEnvFile := ""
resources, _ := appInstallResourceRepo.GetBy(appInstallResourceRepo.WithAppInstallId(install.ID)) resources, _ := appInstallResourceRepo.GetBy(appInstallResourceRepo.WithAppInstallId(install.ID))
for _, resource := range resources { for _, resource := range resources {
if resource.From != "local" {
continue
}
resourceApp, err := appInstallRepo.GetFirst(commonRepo.WithByID(resource.LinkId))
if err != nil {
return err
}
if resource.Key == "mysql" || resource.Key == "maraidb" { if resource.Key == "mysql" || resource.Key == "maraidb" {
mysqlInfo, err := appInstallRepo.LoadBaseInfo(resource.Key, "") mysqlInfo, err := appInstallRepo.LoadBaseInfo(resource.Key, resourceApp.Name)
if err != nil { if err != nil {
return err return err
} }
@ -183,7 +190,7 @@ func handleAppRecover(install *model.AppInstall, recoverFile string, isRollback
return err return err
} }
newDB, envMap, err := reCreateDB(db.ID, oldInstall.Env) newDB, envMap, err := reCreateDB(db.ID, resourceApp, oldInstall.Env)
if err != nil { if err != nil {
return err return err
} }
@ -245,10 +252,10 @@ func handleAppRecover(install *model.AppInstall, recoverFile string, isRollback
return nil return nil
} }
func reCreateDB(dbID uint, oldEnv string) (*model.DatabaseMysql, map[string]interface{}, error) { func reCreateDB(dbID uint, app model.AppInstall, oldEnv string) (*model.DatabaseMysql, map[string]interface{}, error) {
mysqlService := NewIMysqlService() mysqlService := NewIMysqlService()
ctx := context.Background() ctx := context.Background()
_ = mysqlService.Delete(ctx, dto.MysqlDBDelete{ID: dbID, DeleteBackup: true, ForceDelete: true}) _ = mysqlService.Delete(ctx, dto.MysqlDBDelete{ID: dbID, Database: app.Name, Type: app.App.Key, DeleteBackup: true, ForceDelete: true})
envMap := make(map[string]interface{}) envMap := make(map[string]interface{})
if err := json.Unmarshal([]byte(oldEnv), &envMap); err != nil { if err := json.Unmarshal([]byte(oldEnv), &envMap); err != nil {
@ -260,6 +267,7 @@ func reCreateDB(dbID uint, oldEnv string) (*model.DatabaseMysql, map[string]inte
createDB, err := mysqlService.Create(context.Background(), dto.MysqlDBCreate{ createDB, err := mysqlService.Create(context.Background(), dto.MysqlDBCreate{
Name: oldName, Name: oldName,
From: "local", From: "local",
Database: app.Name,
Format: "utf8mb4", Format: "utf8mb4",
Username: oldUser, Username: oldUser,
Password: oldPassword, Password: oldPassword,

View File

@ -232,7 +232,7 @@ func handleTar(sourceDir, targetDir, name, exclusionRules string) error {
path = sourceDir path = sourceDir
} }
commands := fmt.Sprintf("tar -zcf --warning=no-file-changed --ignore-failed-read %s %s %s", targetDir+"/"+name, excludeRules, path) commands := fmt.Sprintf("tar --warning=no-file-changed --ignore-failed-read -zcf %s %s %s", targetDir+"/"+name, excludeRules, path)
global.LOG.Debug(commands) global.LOG.Debug(commands)
stdout, err := cmd.ExecWithTimeOut(commands, 24*time.Hour) stdout, err := cmd.ExecWithTimeOut(commands, 24*time.Hour)
if err != nil { if err != nil {