2023-02-21 19:06:24 +08:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
2023-04-06 00:09:58 +08:00
|
|
|
"context"
|
2023-02-21 19:06:24 +08:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io/fs"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2024-01-09 12:43:33 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/buserr"
|
|
|
|
|
2023-02-21 19:06:24 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/dto"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/model"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/constant"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/global"
|
2024-02-01 18:35:57 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/common"
|
2023-02-21 19:06:24 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/compose"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/files"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
2024-04-24 17:34:10 +08:00
|
|
|
func (u *BackupService) AppBackup(req dto.CommonBackup) (*model.BackupRecord, error) {
|
2023-02-21 19:06:24 +08:00
|
|
|
localDir, err := loadLocalDir()
|
|
|
|
if err != nil {
|
2024-04-24 17:34:10 +08:00
|
|
|
return nil, err
|
2023-02-21 19:06:24 +08:00
|
|
|
}
|
|
|
|
app, err := appRepo.GetFirst(appRepo.WithKey(req.Name))
|
|
|
|
if err != nil {
|
2024-04-24 17:34:10 +08:00
|
|
|
return nil, err
|
2023-02-21 19:06:24 +08:00
|
|
|
}
|
|
|
|
install, err := appInstallRepo.GetFirst(commonRepo.WithByName(req.DetailName), appInstallRepo.WithAppId(app.ID))
|
|
|
|
if err != nil {
|
2024-04-24 17:34:10 +08:00
|
|
|
return nil, err
|
2023-02-21 19:06:24 +08:00
|
|
|
}
|
2024-06-28 14:04:08 +08:00
|
|
|
timeNow := time.Now().Format(constant.DateTimeSlimLayout)
|
2024-01-24 17:41:56 +08:00
|
|
|
itemDir := fmt.Sprintf("app/%s/%s", req.Name, req.DetailName)
|
|
|
|
backupDir := path.Join(localDir, itemDir)
|
2023-02-21 19:06:24 +08:00
|
|
|
|
2024-08-13 15:28:55 +08:00
|
|
|
fileName := req.FileName
|
|
|
|
if req.FileName == "" {
|
|
|
|
fileName = fmt.Sprintf("%s_%s.tar.gz", req.DetailName, timeNow+common.RandStrAndNum(5))
|
|
|
|
}
|
2024-06-04 16:48:31 +08:00
|
|
|
if err := handleAppBackup(&install, backupDir, fileName, "", req.Secret); err != nil {
|
2024-04-24 17:34:10 +08:00
|
|
|
return nil, err
|
2023-02-21 19:06:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
record := &model.BackupRecord{
|
|
|
|
Type: "app",
|
|
|
|
Name: req.Name,
|
|
|
|
DetailName: req.DetailName,
|
|
|
|
Source: "LOCAL",
|
|
|
|
BackupType: "LOCAL",
|
2024-01-24 17:41:56 +08:00
|
|
|
FileDir: itemDir,
|
2023-02-21 19:06:24 +08:00
|
|
|
FileName: fileName,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := backupRepo.CreateRecord(record); err != nil {
|
|
|
|
global.LOG.Errorf("save backup record failed, err: %v", err)
|
2024-04-24 17:34:10 +08:00
|
|
|
return nil, err
|
2023-02-21 19:06:24 +08:00
|
|
|
}
|
2024-04-24 17:34:10 +08:00
|
|
|
return record, nil
|
2023-02-21 19:06:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (u *BackupService) AppRecover(req dto.CommonRecover) error {
|
|
|
|
app, err := appRepo.GetFirst(appRepo.WithKey(req.Name))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
install, err := appInstallRepo.GetFirst(commonRepo.WithByName(req.DetailName), appInstallRepo.WithAppId(app.ID))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
fileOp := files.NewFileOp()
|
|
|
|
if !fileOp.Stat(req.File) {
|
2023-12-19 16:22:07 +08:00
|
|
|
return buserr.WithName("ErrFileNotFound", req.File)
|
2023-02-21 19:06:24 +08:00
|
|
|
}
|
|
|
|
if _, err := compose.Down(install.GetComposePath()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-06-04 16:48:31 +08:00
|
|
|
if err := handleAppRecover(&install, req.File, false, req.Secret); err != nil {
|
2023-02-21 19:06:24 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-06-04 16:48:31 +08:00
|
|
|
func handleAppBackup(install *model.AppInstall, backupDir, fileName string, excludes string, secret string) error {
|
2023-02-21 19:06:24 +08:00
|
|
|
fileOp := files.NewFileOp()
|
|
|
|
tmpDir := fmt.Sprintf("%s/%s", backupDir, strings.ReplaceAll(fileName, ".tar.gz", ""))
|
|
|
|
if !fileOp.Stat(tmpDir) {
|
|
|
|
if err := os.MkdirAll(tmpDir, os.ModePerm); err != nil {
|
|
|
|
return fmt.Errorf("mkdir %s failed, err: %v", backupDir, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
_ = os.RemoveAll(tmpDir)
|
|
|
|
}()
|
|
|
|
|
2023-03-02 13:54:07 +08:00
|
|
|
remarkInfo, _ := json.Marshal(install)
|
2023-02-21 19:06:24 +08:00
|
|
|
remarkInfoPath := fmt.Sprintf("%s/app.json", tmpDir)
|
|
|
|
if err := fileOp.SaveFile(remarkInfoPath, string(remarkInfo), fs.ModePerm); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-06-09 11:42:11 +08:00
|
|
|
appPath := install.GetPath()
|
2024-06-04 16:48:31 +08:00
|
|
|
if err := handleTar(appPath, tmpDir, "app.tar.gz", excludes, ""); err != nil {
|
2023-02-21 19:06:24 +08:00
|
|
|
return err
|
|
|
|
}
|
2023-03-02 13:54:07 +08:00
|
|
|
|
2023-08-03 18:40:23 +08:00
|
|
|
resources, _ := appInstallResourceRepo.GetBy(appInstallResourceRepo.WithAppInstallId(install.ID))
|
|
|
|
for _, resource := range resources {
|
2023-12-28 16:29:18 +08:00
|
|
|
switch resource.Key {
|
|
|
|
case constant.AppMysql, constant.AppMariaDB:
|
2023-08-03 18:40:23 +08:00
|
|
|
db, err := mysqlRepo.Get(commonRepo.WithByID(resource.ResourceId))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-03-04 16:35:05 +08:00
|
|
|
if err := handleMysqlBackup(db.MysqlName, resource.Key, db.Name, tmpDir, fmt.Sprintf("%s.sql.gz", install.Name)); err != nil {
|
2023-08-03 18:40:23 +08:00
|
|
|
return err
|
|
|
|
}
|
2023-12-28 16:29:18 +08:00
|
|
|
case constant.AppPostgresql:
|
|
|
|
db, err := postgresqlRepo.Get(commonRepo.WithByID(resource.ResourceId))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := handlePostgresqlBackup(db.PostgresqlName, db.Name, tmpDir, fmt.Sprintf("%s.sql.gz", install.Name)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-03-02 13:54:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-04 16:48:31 +08:00
|
|
|
if err := handleTar(tmpDir, backupDir, fileName, "", secret); err != nil {
|
2023-02-21 19:06:24 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-06-04 16:48:31 +08:00
|
|
|
func handleAppRecover(install *model.AppInstall, recoverFile string, isRollback bool, secret string) error {
|
2023-03-01 11:38:08 +08:00
|
|
|
isOk := false
|
2023-02-21 19:06:24 +08:00
|
|
|
fileOp := files.NewFileOp()
|
2024-06-04 16:48:31 +08:00
|
|
|
if err := handleUnTar(recoverFile, path.Dir(recoverFile), secret); err != nil {
|
2023-02-21 19:06:24 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
tmpPath := strings.ReplaceAll(recoverFile, ".tar.gz", "")
|
|
|
|
defer func() {
|
2023-03-02 13:54:07 +08:00
|
|
|
_, _ = compose.Up(install.GetComposePath())
|
2023-02-21 19:06:24 +08:00
|
|
|
_ = os.RemoveAll(strings.ReplaceAll(recoverFile, ".tar.gz", ""))
|
|
|
|
}()
|
|
|
|
|
|
|
|
if !fileOp.Stat(tmpPath+"/app.json") || !fileOp.Stat(tmpPath+"/app.tar.gz") {
|
|
|
|
return errors.New("the wrong recovery package does not have app.json or app.tar.gz files")
|
|
|
|
}
|
2023-03-02 13:54:07 +08:00
|
|
|
var oldInstall model.AppInstall
|
|
|
|
appjson, err := os.ReadFile(tmpPath + "/app.json")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := json.Unmarshal(appjson, &oldInstall); err != nil {
|
|
|
|
return fmt.Errorf("unmarshal app.json failed, err: %v", err)
|
|
|
|
}
|
2023-06-13 12:28:11 +08:00
|
|
|
if oldInstall.App.Key != install.App.Key || oldInstall.Name != install.Name {
|
2023-03-02 13:54:07 +08:00
|
|
|
return errors.New("the current backup file does not match the application")
|
|
|
|
}
|
|
|
|
|
2023-03-01 11:38:08 +08:00
|
|
|
if !isRollback {
|
2024-06-28 14:04:08 +08:00
|
|
|
rollbackFile := path.Join(global.CONF.System.TmpDir, fmt.Sprintf("app/%s_%s.tar.gz", install.Name, time.Now().Format(constant.DateTimeSlimLayout)))
|
2024-06-04 16:48:31 +08:00
|
|
|
if err := handleAppBackup(install, path.Dir(rollbackFile), path.Base(rollbackFile), "", ""); err != nil {
|
2023-03-02 18:35:08 +08:00
|
|
|
return fmt.Errorf("backup app %s for rollback before recover failed, err: %v", install.Name, err)
|
2023-03-01 11:38:08 +08:00
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if !isOk {
|
2023-03-08 23:58:42 +08:00
|
|
|
global.LOG.Info("recover failed, start to rollback now")
|
2024-06-04 16:48:31 +08:00
|
|
|
if err := handleAppRecover(install, rollbackFile, true, secret); err != nil {
|
2023-03-01 11:38:08 +08:00
|
|
|
global.LOG.Errorf("rollback app %s from %s failed, err: %v", install.Name, rollbackFile, err)
|
2023-03-02 13:54:07 +08:00
|
|
|
return
|
2023-03-01 11:38:08 +08:00
|
|
|
}
|
|
|
|
global.LOG.Infof("rollback app %s from %s successful", install.Name, rollbackFile)
|
|
|
|
_ = os.RemoveAll(rollbackFile)
|
|
|
|
} else {
|
|
|
|
_ = os.RemoveAll(rollbackFile)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2023-06-13 16:32:12 +08:00
|
|
|
newEnvFile := ""
|
2023-08-03 18:40:23 +08:00
|
|
|
resources, _ := appInstallResourceRepo.GetBy(appInstallResourceRepo.WithAppInstallId(install.ID))
|
|
|
|
for _, resource := range resources {
|
2023-10-07 16:02:44 +08:00
|
|
|
var database model.Database
|
|
|
|
switch resource.From {
|
|
|
|
case constant.AppResourceRemote:
|
|
|
|
database, err = databaseRepo.Get(commonRepo.WithByID(resource.LinkId))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
case constant.AppResourceLocal:
|
|
|
|
resourceApp, err := appInstallRepo.GetFirst(commonRepo.WithByID(resource.LinkId))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
database, err = databaseRepo.Get(databaseRepo.WithAppInstallID(resourceApp.ID), commonRepo.WithByType(resource.Key), databaseRepo.WithByFrom(constant.AppResourceLocal), commonRepo.WithByName(resourceApp.Name))
|
2023-08-03 18:40:23 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-10-07 16:02:44 +08:00
|
|
|
}
|
2024-01-09 22:31:34 +08:00
|
|
|
switch database.Type {
|
|
|
|
case constant.AppPostgresql:
|
2023-12-28 16:29:18 +08:00
|
|
|
db, err := postgresqlRepo.Get(commonRepo.WithByID(resource.ResourceId))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := handlePostgresqlRecover(dto.CommonRecover{
|
2024-01-09 22:31:34 +08:00
|
|
|
Name: database.Name,
|
|
|
|
DetailName: db.Name,
|
2023-12-28 16:29:18 +08:00
|
|
|
File: fmt.Sprintf("%s/%s.sql.gz", tmpPath, install.Name),
|
|
|
|
}, true); err != nil {
|
|
|
|
global.LOG.Errorf("handle recover from sql.gz failed, err: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
2024-01-09 22:31:34 +08:00
|
|
|
case constant.AppMysql, constant.AppMariaDB:
|
2023-08-03 18:40:23 +08:00
|
|
|
db, err := mysqlRepo.Get(commonRepo.WithByID(resource.ResourceId))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-10-07 16:02:44 +08:00
|
|
|
newDB, envMap, err := reCreateDB(db.ID, database, oldInstall.Env)
|
2023-08-03 18:40:23 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
oldHost := fmt.Sprintf("\"PANEL_DB_HOST\":\"%v\"", envMap["PANEL_DB_HOST"].(string))
|
2023-10-07 16:02:44 +08:00
|
|
|
newHost := fmt.Sprintf("\"PANEL_DB_HOST\":\"%v\"", database.Address)
|
2023-08-03 18:40:23 +08:00
|
|
|
oldInstall.Env = strings.ReplaceAll(oldInstall.Env, oldHost, newHost)
|
2023-10-07 16:02:44 +08:00
|
|
|
envMap["PANEL_DB_HOST"] = database.Address
|
2023-08-03 18:40:23 +08:00
|
|
|
newEnvFile, err = coverEnvJsonToStr(oldInstall.Env)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_ = appInstallResourceRepo.BatchUpdateBy(map[string]interface{}{"resource_id": newDB.ID}, commonRepo.WithByID(resource.ID))
|
2023-06-12 17:48:11 +08:00
|
|
|
|
2023-08-03 18:40:23 +08:00
|
|
|
if err := handleMysqlRecover(dto.CommonRecover{
|
2023-09-16 13:16:15 +08:00
|
|
|
Name: newDB.MysqlName,
|
2023-08-03 18:40:23 +08:00
|
|
|
DetailName: newDB.Name,
|
|
|
|
File: fmt.Sprintf("%s/%s.sql.gz", tmpPath, install.Name),
|
|
|
|
}, true); err != nil {
|
|
|
|
global.LOG.Errorf("handle recover from sql.gz failed, err: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
2023-03-02 13:54:07 +08:00
|
|
|
}
|
2023-02-21 19:06:24 +08:00
|
|
|
}
|
|
|
|
|
2023-08-08 17:06:12 +08:00
|
|
|
appDir := install.GetPath()
|
|
|
|
backPath := fmt.Sprintf("%s_bak", appDir)
|
|
|
|
_ = fileOp.Rename(appDir, backPath)
|
|
|
|
_ = fileOp.CreateDir(appDir, 0755)
|
|
|
|
|
2024-06-04 16:48:31 +08:00
|
|
|
if err := handleUnTar(tmpPath+"/app.tar.gz", install.GetAppPath(), ""); err != nil {
|
2023-03-16 15:58:45 +08:00
|
|
|
global.LOG.Errorf("handle recover from app.tar.gz failed, err: %v", err)
|
2023-08-08 17:06:12 +08:00
|
|
|
_ = fileOp.DeleteDir(appDir)
|
|
|
|
_ = fileOp.Rename(backPath, appDir)
|
2023-02-21 19:06:24 +08:00
|
|
|
return err
|
|
|
|
}
|
2023-08-08 17:06:12 +08:00
|
|
|
_ = fileOp.DeleteDir(backPath)
|
2023-02-21 19:06:24 +08:00
|
|
|
|
2023-06-13 16:32:12 +08:00
|
|
|
if len(newEnvFile) != 0 {
|
2023-12-28 16:29:18 +08:00
|
|
|
envPath := fmt.Sprintf("%s/%s/.env", install.GetAppPath(), install.Name)
|
2023-06-13 16:32:12 +08:00
|
|
|
file, err := os.OpenFile(envPath, os.O_WRONLY|os.O_TRUNC, 0640)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
_, _ = file.WriteString(newEnvFile)
|
|
|
|
}
|
|
|
|
|
2023-06-12 17:48:11 +08:00
|
|
|
oldInstall.ID = install.ID
|
|
|
|
oldInstall.Status = constant.StatusRunning
|
|
|
|
oldInstall.AppId = install.AppId
|
|
|
|
oldInstall.AppDetailId = install.AppDetailId
|
2023-08-03 14:45:27 +08:00
|
|
|
oldInstall.App.ID = install.AppId
|
2023-06-12 17:48:11 +08:00
|
|
|
if err := appInstallRepo.Save(context.Background(), &oldInstall); err != nil {
|
2023-03-16 15:58:45 +08:00
|
|
|
global.LOG.Errorf("save db app install failed, err: %v", err)
|
2023-02-21 19:06:24 +08:00
|
|
|
return err
|
|
|
|
}
|
2023-03-01 11:38:08 +08:00
|
|
|
isOk = true
|
2023-08-08 17:06:12 +08:00
|
|
|
|
2023-02-21 19:06:24 +08:00
|
|
|
return nil
|
|
|
|
}
|
2023-06-12 17:48:11 +08:00
|
|
|
|
2023-10-07 16:02:44 +08:00
|
|
|
func reCreateDB(dbID uint, database model.Database, oldEnv string) (*model.DatabaseMysql, map[string]interface{}, error) {
|
2023-06-12 17:48:11 +08:00
|
|
|
mysqlService := NewIMysqlService()
|
|
|
|
ctx := context.Background()
|
2023-10-07 16:02:44 +08:00
|
|
|
_ = mysqlService.Delete(ctx, dto.MysqlDBDelete{ID: dbID, Database: database.Name, Type: database.Type, DeleteBackup: false, ForceDelete: true})
|
2023-06-12 17:48:11 +08:00
|
|
|
|
|
|
|
envMap := make(map[string]interface{})
|
|
|
|
if err := json.Unmarshal([]byte(oldEnv), &envMap); err != nil {
|
2023-06-13 16:32:12 +08:00
|
|
|
return nil, envMap, err
|
2023-06-12 17:48:11 +08:00
|
|
|
}
|
|
|
|
oldName, _ := envMap["PANEL_DB_NAME"].(string)
|
|
|
|
oldUser, _ := envMap["PANEL_DB_USER"].(string)
|
|
|
|
oldPassword, _ := envMap["PANEL_DB_USER_PASSWORD"].(string)
|
|
|
|
createDB, err := mysqlService.Create(context.Background(), dto.MysqlDBCreate{
|
|
|
|
Name: oldName,
|
2023-10-07 16:02:44 +08:00
|
|
|
From: database.From,
|
|
|
|
Database: database.Name,
|
2023-06-12 17:48:11 +08:00
|
|
|
Format: "utf8mb4",
|
|
|
|
Username: oldUser,
|
|
|
|
Password: oldPassword,
|
|
|
|
Permission: "%",
|
|
|
|
})
|
2024-01-31 18:47:21 +08:00
|
|
|
cronjobs, _ := cronjobRepo.List(cronjobRepo.WithByDbName(fmt.Sprintf("%v", dbID)))
|
|
|
|
for _, job := range cronjobs {
|
|
|
|
_ = cronjobRepo.Update(job.ID, map[string]interface{}{"db_name": fmt.Sprintf("%v", createDB.ID)})
|
|
|
|
}
|
2023-06-12 17:48:11 +08:00
|
|
|
if err != nil {
|
2023-06-13 16:32:12 +08:00
|
|
|
return nil, envMap, err
|
2023-06-12 17:48:11 +08:00
|
|
|
}
|
2023-06-13 16:32:12 +08:00
|
|
|
return createDB, envMap, nil
|
2023-06-12 17:48:11 +08:00
|
|
|
}
|