From 47dda4ac9f1ca0aec05b3407478e2ccb400a3773 Mon Sep 17 00:00:00 2001 From: ssongliu <73214554+ssongliu@users.noreply.github.com> Date: Tue, 13 Jun 2023 16:32:12 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E6=94=B9=E5=BA=94=E7=94=A8?= =?UTF-8?q?=E5=AF=BC=E5=85=A5=E6=81=A2=E5=A4=8D=E6=97=B6=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E5=BA=93=E8=BF=9E=E6=8E=A5=E4=BF=A1=E6=81=AF=20(#1367)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/app/service/backup_app.go | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/backend/app/service/backup_app.go b/backend/app/service/backup_app.go index 19b92d625..56e8e1049 100644 --- a/backend/app/service/backup_app.go +++ b/backend/app/service/backup_app.go @@ -172,6 +172,7 @@ func handleAppRecover(install *model.AppInstall, recoverFile string, isRollback }() } + newEnvFile := "" resource, _ := appInstallResourceRepo.GetFirst(appInstallResourceRepo.WithAppInstallId(install.ID)) if resource.ID != 0 && install.App.Key != "mysql" { mysqlInfo, err := appInstallRepo.LoadBaseInfo(resource.Key, "") @@ -183,7 +184,15 @@ func handleAppRecover(install *model.AppInstall, recoverFile string, isRollback return err } - newDB, err := reCreateDB(db.ID, oldInstall.Env) + newDB, envMap, err := reCreateDB(db.ID, oldInstall.Env) + if err != nil { + return err + } + oldHost := fmt.Sprintf("\"PANEL_DB_HOST\":\"%v\"", envMap["PANEL_DB_HOST"].(string)) + newHost := fmt.Sprintf("\"PANEL_DB_HOST\":\"%v\"", mysqlInfo.ServiceName) + oldInstall.Env = strings.ReplaceAll(oldInstall.Env, oldHost, newHost) + envMap["PANEL_DB_HOST"] = mysqlInfo.ServiceName + newEnvFile, err = coverEnvJsonToStr(oldInstall.Env) if err != nil { return err } @@ -200,6 +209,16 @@ func handleAppRecover(install *model.AppInstall, recoverFile string, isRollback return err } + if len(newEnvFile) != 0 { + envPath := fmt.Sprintf("%s/%s/%s/.env", constant.AppInstallDir, install.App.Key, install.Name) + file, err := os.OpenFile(envPath, os.O_WRONLY|os.O_TRUNC, 0640) + if err != nil { + return err + } + defer file.Close() + _, _ = file.WriteString(newEnvFile) + } + oldInstall.ID = install.ID oldInstall.Status = constant.StatusRunning oldInstall.AppId = install.AppId @@ -212,14 +231,14 @@ func handleAppRecover(install *model.AppInstall, recoverFile string, isRollback return nil } -func reCreateDB(dbID uint, oldEnv string) (*model.DatabaseMysql, error) { +func reCreateDB(dbID uint, oldEnv string) (*model.DatabaseMysql, map[string]interface{}, error) { mysqlService := NewIMysqlService() ctx := context.Background() _ = mysqlService.Delete(ctx, dto.MysqlDBDelete{ID: dbID, DeleteBackup: true, ForceDelete: true}) envMap := make(map[string]interface{}) if err := json.Unmarshal([]byte(oldEnv), &envMap); err != nil { - return nil, err + return nil, envMap, err } oldName, _ := envMap["PANEL_DB_NAME"].(string) oldUser, _ := envMap["PANEL_DB_USER"].(string) @@ -232,7 +251,8 @@ func reCreateDB(dbID uint, oldEnv string) (*model.DatabaseMysql, error) { Permission: "%", }) if err != nil { - return nil, err + return nil, envMap, err } - return createDB, nil + + return createDB, envMap, nil }