fix: 解决应用备份失败的问题 (#1828)

pull/1829/head
ssongliu 2023-08-03 18:40:23 +08:00 committed by GitHub
parent e30546102e
commit 43e1ec0244
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 43 additions and 38 deletions

View File

@ -104,14 +104,16 @@ func handleAppBackup(install *model.AppInstall, backupDir, fileName string) erro
return err return err
} }
resource, _ := appInstallResourceRepo.GetFirst(appInstallResourceRepo.WithAppInstallId(install.ID)) resources, _ := appInstallResourceRepo.GetBy(appInstallResourceRepo.WithAppInstallId(install.ID))
if resource.ID != 0 && resource.ResourceId != 0 { for _, resource := range resources {
db, err := mysqlRepo.Get(commonRepo.WithByID(resource.ResourceId)) if resource.Key == "mysql" {
if err != nil { db, err := mysqlRepo.Get(commonRepo.WithByID(resource.ResourceId))
return err if err != nil {
} return err
if err := handleMysqlBackup(db.MysqlName, db.Name, tmpDir, fmt.Sprintf("%s.sql.gz", install.Name)); err != nil { }
return err if err := handleMysqlBackup(db.MysqlName, db.Name, tmpDir, fmt.Sprintf("%s.sql.gz", install.Name)); err != nil {
return err
}
} }
} }
@ -169,38 +171,40 @@ func handleAppRecover(install *model.AppInstall, recoverFile string, isRollback
} }
newEnvFile := "" newEnvFile := ""
resource, _ := appInstallResourceRepo.GetFirst(appInstallResourceRepo.WithAppInstallId(install.ID)) resources, _ := appInstallResourceRepo.GetBy(appInstallResourceRepo.WithAppInstallId(install.ID))
if resource.ID != 0 && install.App.Key != "mysql" { for _, resource := range resources {
mysqlInfo, err := appInstallRepo.LoadBaseInfo(resource.Key, "") if resource.Key == "mysql" {
if err != nil { mysqlInfo, err := appInstallRepo.LoadBaseInfo(resource.Key, "")
return err if err != nil {
} return err
db, err := mysqlRepo.Get(commonRepo.WithByID(resource.ResourceId)) }
if err != nil { db, err := mysqlRepo.Get(commonRepo.WithByID(resource.ResourceId))
return err if err != nil {
} return err
}
newDB, envMap, err := reCreateDB(db.ID, oldInstall.Env) newDB, envMap, err := reCreateDB(db.ID, oldInstall.Env)
if err != nil { if err != nil {
return err return err
} }
oldHost := fmt.Sprintf("\"PANEL_DB_HOST\":\"%v\"", envMap["PANEL_DB_HOST"].(string)) oldHost := fmt.Sprintf("\"PANEL_DB_HOST\":\"%v\"", envMap["PANEL_DB_HOST"].(string))
newHost := fmt.Sprintf("\"PANEL_DB_HOST\":\"%v\"", mysqlInfo.ServiceName) newHost := fmt.Sprintf("\"PANEL_DB_HOST\":\"%v\"", mysqlInfo.ServiceName)
oldInstall.Env = strings.ReplaceAll(oldInstall.Env, oldHost, newHost) oldInstall.Env = strings.ReplaceAll(oldInstall.Env, oldHost, newHost)
envMap["PANEL_DB_HOST"] = mysqlInfo.ServiceName envMap["PANEL_DB_HOST"] = mysqlInfo.ServiceName
newEnvFile, err = coverEnvJsonToStr(oldInstall.Env) newEnvFile, err = coverEnvJsonToStr(oldInstall.Env)
if err != nil { if err != nil {
return err return err
} }
_ = appInstallResourceRepo.BatchUpdateBy(map[string]interface{}{"resource_id": newDB.ID}, commonRepo.WithByID(resource.ID)) _ = appInstallResourceRepo.BatchUpdateBy(map[string]interface{}{"resource_id": newDB.ID}, commonRepo.WithByID(resource.ID))
if err := handleMysqlRecover(dto.CommonRecover{ if err := handleMysqlRecover(dto.CommonRecover{
Name: newDB.MysqlName, Name: newDB.MysqlName,
DetailName: newDB.Name, DetailName: newDB.Name,
File: tmpPath + "/" + fmt.Sprintf("%s/%s.sql.gz", tmpPath, install.Name), File: fmt.Sprintf("%s/%s.sql.gz", tmpPath, install.Name),
}, true); err != nil { }, true); err != nil {
global.LOG.Errorf("handle recover from sql.gz failed, err: %v", err) global.LOG.Errorf("handle recover from sql.gz failed, err: %v", err)
return err return err
}
} }
} }
@ -246,6 +250,7 @@ func reCreateDB(dbID uint, oldEnv string) (*model.DatabaseMysql, map[string]inte
oldPassword, _ := envMap["PANEL_DB_USER_PASSWORD"].(string) oldPassword, _ := envMap["PANEL_DB_USER_PASSWORD"].(string)
createDB, err := mysqlService.Create(context.Background(), dto.MysqlDBCreate{ createDB, err := mysqlService.Create(context.Background(), dto.MysqlDBCreate{
Name: oldName, Name: oldName,
From: "local",
Format: "utf8mb4", Format: "utf8mb4",
Username: oldUser, Username: oldUser,
Password: oldPassword, Password: oldPassword,