2023-02-21 11:06:24 +00:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io/fs"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/dto"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/model"
|
2023-07-13 11:51:17 +00:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/buserr"
|
2023-02-21 11:06:24 +00:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/constant"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/global"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/cmd"
|
2024-02-01 10:35:57 +00:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/common"
|
2023-02-21 11:06:24 +00:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/compose"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/files"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (u *BackupService) WebsiteBackup(req dto.CommonBackup) error {
|
|
|
|
localDir, err := loadLocalDir()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-12-18 09:54:15 +00:00
|
|
|
website, err := websiteRepo.GetFirst(websiteRepo.WithAlias(req.DetailName))
|
2023-02-21 11:06:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
timeNow := time.Now().Format("20060102150405")
|
2024-01-24 09:41:56 +00:00
|
|
|
itemDir := fmt.Sprintf("website/%s", req.Name)
|
|
|
|
backupDir := path.Join(localDir, itemDir)
|
2024-02-01 10:35:57 +00:00
|
|
|
fileName := fmt.Sprintf("%s_%s.tar.gz", website.PrimaryDomain, timeNow+common.RandStrAndNum(5))
|
2024-06-04 08:48:31 +00:00
|
|
|
if err := handleWebsiteBackup(&website, backupDir, fileName, "", req.Secret); err != nil {
|
2023-02-21 11:06:24 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
record := &model.BackupRecord{
|
|
|
|
Type: "website",
|
|
|
|
Name: website.PrimaryDomain,
|
2023-12-18 09:54:15 +00:00
|
|
|
DetailName: req.DetailName,
|
2023-02-21 11:06:24 +00:00
|
|
|
Source: "LOCAL",
|
|
|
|
BackupType: "LOCAL",
|
2024-01-24 09:41:56 +00:00
|
|
|
FileDir: itemDir,
|
2023-02-21 11:06:24 +00:00
|
|
|
FileName: fileName,
|
|
|
|
}
|
|
|
|
if err := backupRepo.CreateRecord(record); err != nil {
|
|
|
|
global.LOG.Errorf("save backup record failed, err: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *BackupService) WebsiteRecover(req dto.CommonRecover) error {
|
|
|
|
fileOp := files.NewFileOp()
|
|
|
|
if !fileOp.Stat(req.File) {
|
2023-12-19 08:22:07 +00:00
|
|
|
return buserr.WithName("ErrFileNotFound", req.File)
|
2023-02-21 11:06:24 +00:00
|
|
|
}
|
2023-12-18 09:54:15 +00:00
|
|
|
website, err := websiteRepo.GetFirst(websiteRepo.WithAlias(req.DetailName))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-02-21 11:06:24 +00:00
|
|
|
global.LOG.Infof("recover website %s from backup file %s", req.Name, req.File)
|
2024-06-04 08:48:31 +00:00
|
|
|
if err := handleWebsiteRecover(&website, req.File, false, req.Secret); err != nil {
|
2023-02-21 11:06:24 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-06-04 08:48:31 +00:00
|
|
|
func handleWebsiteRecover(website *model.Website, recoverFile string, isRollback bool, secret string) error {
|
2023-02-21 11:06:24 +00:00
|
|
|
fileOp := files.NewFileOp()
|
2023-03-02 05:54:07 +00:00
|
|
|
tmpPath := strings.ReplaceAll(recoverFile, ".tar.gz", "")
|
2024-06-04 08:48:31 +00:00
|
|
|
if err := handleUnTar(recoverFile, path.Dir(recoverFile), secret); err != nil {
|
2023-02-21 11:06:24 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer func() {
|
2023-03-02 05:54:07 +00:00
|
|
|
_ = os.RemoveAll(tmpPath)
|
2023-02-21 11:06:24 +00:00
|
|
|
}()
|
|
|
|
|
2023-03-02 05:54:07 +00:00
|
|
|
var oldWebsite model.Website
|
|
|
|
websiteJson, err := os.ReadFile(tmpPath + "/website.json")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := json.Unmarshal(websiteJson, &oldWebsite); err != nil {
|
|
|
|
return fmt.Errorf("unmarshal app.json failed, err: %v", err)
|
|
|
|
}
|
2023-07-13 11:51:17 +00:00
|
|
|
|
|
|
|
if err := checkValidOfWebsite(&oldWebsite, website); err != nil {
|
|
|
|
return err
|
2023-03-02 05:54:07 +00:00
|
|
|
}
|
|
|
|
|
2023-12-18 09:54:15 +00:00
|
|
|
temPathWithName := tmpPath + "/" + website.Alias
|
|
|
|
if !fileOp.Stat(tmpPath+"/website.json") || !fileOp.Stat(temPathWithName+".conf") || !fileOp.Stat(temPathWithName+".web.tar.gz") {
|
|
|
|
return buserr.WithDetail(constant.ErrBackupExist, ".conf or .web.tar.gz", nil)
|
|
|
|
}
|
|
|
|
if website.Type == constant.Deployment {
|
|
|
|
if !fileOp.Stat(temPathWithName + ".app.tar.gz") {
|
|
|
|
return buserr.WithDetail(constant.ErrBackupExist, ".app.tar.gz", nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-01 03:38:08 +00:00
|
|
|
isOk := false
|
|
|
|
if !isRollback {
|
2023-08-01 14:05:14 +00:00
|
|
|
rollbackFile := path.Join(global.CONF.System.TmpDir, fmt.Sprintf("website/%s_%s.tar.gz", website.Alias, time.Now().Format("20060102150405")))
|
2024-06-04 08:48:31 +00:00
|
|
|
if err := handleWebsiteBackup(website, path.Dir(rollbackFile), path.Base(rollbackFile), "", ""); err != nil {
|
2023-03-02 10:35:08 +00:00
|
|
|
return fmt.Errorf("backup website %s for rollback before recover failed, err: %v", website.Alias, err)
|
2023-03-01 03:38:08 +00:00
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if !isOk {
|
2023-03-16 07:58:45 +00:00
|
|
|
global.LOG.Info("recover failed, start to rollback now")
|
2024-06-06 08:03:39 +00:00
|
|
|
if err := handleWebsiteRecover(website, rollbackFile, true, ""); err != nil {
|
2023-03-01 03:38:08 +00:00
|
|
|
global.LOG.Errorf("rollback website %s from %s failed, err: %v", website.Alias, rollbackFile, err)
|
2023-03-02 05:54:07 +00:00
|
|
|
return
|
2023-03-01 03:38:08 +00:00
|
|
|
}
|
|
|
|
global.LOG.Infof("rollback website %s from %s successful", website.Alias, rollbackFile)
|
|
|
|
_ = os.RemoveAll(rollbackFile)
|
|
|
|
} else {
|
|
|
|
_ = os.RemoveAll(rollbackFile)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
2023-02-21 11:06:24 +00:00
|
|
|
|
|
|
|
nginxInfo, err := appInstallRepo.LoadBaseInfo(constant.AppOpenresty, "")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
nginxConfPath := fmt.Sprintf("%s/openresty/%s/conf/conf.d", constant.AppInstallDir, nginxInfo.Name)
|
2023-03-02 05:54:07 +00:00
|
|
|
if err := fileOp.CopyFile(fmt.Sprintf("%s/%s.conf", tmpPath, website.Alias), nginxConfPath); err != nil {
|
2023-03-16 07:58:45 +00:00
|
|
|
global.LOG.Errorf("handle recover from conf.d failed, err: %v", err)
|
2023-02-21 11:06:24 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-10-07 12:30:45 +00:00
|
|
|
switch website.Type {
|
|
|
|
case constant.Deployment:
|
2023-02-21 11:06:24 +00:00
|
|
|
app, err := appInstallRepo.GetFirst(commonRepo.WithByID(website.AppInstallID))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-06-04 08:48:31 +00:00
|
|
|
if err := handleAppRecover(&app, fmt.Sprintf("%s/%s.app.tar.gz", tmpPath, website.Alias), true, ""); err != nil {
|
2023-03-16 07:58:45 +00:00
|
|
|
global.LOG.Errorf("handle recover from app.tar.gz failed, err: %v", err)
|
2023-02-21 11:06:24 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if _, err := compose.Restart(fmt.Sprintf("%s/%s/%s/docker-compose.yml", constant.AppInstallDir, app.App.Key, app.Name)); err != nil {
|
2023-03-16 07:58:45 +00:00
|
|
|
global.LOG.Errorf("docker-compose restart failed, err: %v", err)
|
2023-02-21 11:06:24 +00:00
|
|
|
return err
|
|
|
|
}
|
2023-10-07 12:30:45 +00:00
|
|
|
case constant.Runtime:
|
|
|
|
runtime, err := runtimeRepo.GetFirst(commonRepo.WithByID(website.RuntimeID))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-06-26 14:39:25 +00:00
|
|
|
if runtime.Type == constant.RuntimeNode || runtime.Type == constant.RuntimeJava {
|
2024-06-04 08:48:31 +00:00
|
|
|
if err := handleRuntimeRecover(runtime, fmt.Sprintf("%s/%s.runtime.tar.gz", tmpPath, website.Alias), true, ""); err != nil {
|
2023-10-07 12:30:45 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
global.LOG.Info("put runtime.tar.gz into tmp dir successful")
|
|
|
|
}
|
2023-02-21 11:06:24 +00:00
|
|
|
}
|
2023-10-07 12:30:45 +00:00
|
|
|
|
2023-02-21 11:06:24 +00:00
|
|
|
siteDir := fmt.Sprintf("%s/openresty/%s/www/sites", constant.AppInstallDir, nginxInfo.Name)
|
2024-06-04 08:48:31 +00:00
|
|
|
if err := handleUnTar(fmt.Sprintf("%s/%s.web.tar.gz", tmpPath, website.Alias), siteDir, ""); err != nil {
|
2023-03-16 07:58:45 +00:00
|
|
|
global.LOG.Errorf("handle recover from web.tar.gz failed, err: %v", err)
|
2023-02-21 11:06:24 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
stdout, err := cmd.Execf("docker exec -i %s nginx -s reload", nginxInfo.ContainerName)
|
|
|
|
if err != nil {
|
2023-03-16 07:58:45 +00:00
|
|
|
global.LOG.Errorf("nginx -s reload failed, err: %s", stdout)
|
2023-02-21 11:06:24 +00:00
|
|
|
return errors.New(string(stdout))
|
|
|
|
}
|
|
|
|
|
2023-07-13 11:51:17 +00:00
|
|
|
oldWebsite.ID = website.ID
|
2023-03-02 05:54:07 +00:00
|
|
|
if err := websiteRepo.SaveWithoutCtx(&oldWebsite); err != nil {
|
2023-03-16 07:58:45 +00:00
|
|
|
global.LOG.Errorf("handle save website data failed, err: %v", err)
|
2023-03-02 05:54:07 +00:00
|
|
|
return err
|
|
|
|
}
|
2023-03-07 07:51:48 +00:00
|
|
|
isOk = true
|
2023-02-21 11:06:24 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-06-04 08:48:31 +00:00
|
|
|
func handleWebsiteBackup(website *model.Website, backupDir, fileName string, excludes string, secret string) error {
|
2023-02-21 11:06:24 +00: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 05:54:07 +00:00
|
|
|
remarkInfo, _ := json.Marshal(website)
|
2023-02-21 11:06:24 +00:00
|
|
|
if err := fileOp.SaveFile(tmpDir+"/website.json", string(remarkInfo), fs.ModePerm); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-03-02 05:54:07 +00:00
|
|
|
global.LOG.Info("put website.json into tmp dir successful")
|
2023-02-21 11:06:24 +00:00
|
|
|
|
|
|
|
nginxInfo, err := appInstallRepo.LoadBaseInfo(constant.AppOpenresty, "")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
nginxConfFile := fmt.Sprintf("%s/openresty/%s/conf/conf.d/%s.conf", constant.AppInstallDir, nginxInfo.Name, website.Alias)
|
|
|
|
if err := fileOp.CopyFile(nginxConfFile, tmpDir); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
global.LOG.Info("put openresty conf into tmp dir successful")
|
|
|
|
|
2023-10-07 12:30:45 +00:00
|
|
|
switch website.Type {
|
|
|
|
case constant.Deployment:
|
2023-02-21 11:06:24 +00:00
|
|
|
app, err := appInstallRepo.GetFirst(commonRepo.WithByID(website.AppInstallID))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-06-04 08:48:31 +00:00
|
|
|
if err := handleAppBackup(&app, tmpDir, fmt.Sprintf("%s.app.tar.gz", website.Alias), excludes, ""); err != nil {
|
2023-02-21 11:06:24 +00:00
|
|
|
return err
|
|
|
|
}
|
2023-03-02 05:54:07 +00:00
|
|
|
global.LOG.Info("put app.tar.gz into tmp dir successful")
|
2023-10-07 12:30:45 +00:00
|
|
|
case constant.Runtime:
|
|
|
|
runtime, err := runtimeRepo.GetFirst(commonRepo.WithByID(website.RuntimeID))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-06-26 14:39:25 +00:00
|
|
|
if runtime.Type == constant.RuntimeNode || runtime.Type == constant.RuntimeJava {
|
2024-06-04 08:48:31 +00:00
|
|
|
if err := handleRuntimeBackup(runtime, tmpDir, fmt.Sprintf("%s.runtime.tar.gz", website.Alias), excludes, ""); err != nil {
|
2023-10-07 12:30:45 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
global.LOG.Info("put runtime.tar.gz into tmp dir successful")
|
|
|
|
}
|
2023-02-21 11:06:24 +00:00
|
|
|
}
|
2023-10-07 12:30:45 +00:00
|
|
|
|
2023-02-21 11:06:24 +00:00
|
|
|
websiteDir := fmt.Sprintf("%s/openresty/%s/www/sites/%s", constant.AppInstallDir, nginxInfo.Name, website.Alias)
|
2024-06-04 08:48:31 +00:00
|
|
|
if err := handleTar(websiteDir, tmpDir, fmt.Sprintf("%s.web.tar.gz", website.Alias), excludes, ""); err != nil {
|
2023-02-21 11:06:24 +00:00
|
|
|
return err
|
|
|
|
}
|
2023-03-02 05:54:07 +00:00
|
|
|
global.LOG.Info("put web.tar.gz into tmp dir successful, now start to tar tmp dir")
|
2024-06-04 08:48:31 +00:00
|
|
|
if err := handleTar(tmpDir, backupDir, fileName, "", secret); err != nil {
|
2023-02-21 11:06:24 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2023-07-13 11:51:17 +00:00
|
|
|
|
|
|
|
func checkValidOfWebsite(oldWebsite, website *model.Website) error {
|
|
|
|
if oldWebsite.Alias != website.Alias || oldWebsite.Type != website.Type {
|
|
|
|
return buserr.WithDetail(constant.ErrBackupMatch, fmt.Sprintf("oldName: %s, oldType: %v", oldWebsite.Alias, oldWebsite.Type), nil)
|
|
|
|
}
|
|
|
|
if oldWebsite.AppInstallID != 0 {
|
2024-05-08 10:44:24 +00:00
|
|
|
_, err := appInstallRepo.GetFirst(commonRepo.WithByID(oldWebsite.AppInstallID))
|
2023-07-13 11:51:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return buserr.WithDetail(constant.ErrBackupMatch, "app", nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if oldWebsite.RuntimeID != 0 {
|
2024-05-08 10:44:24 +00:00
|
|
|
if _, err := runtimeRepo.GetFirst(commonRepo.WithByID(oldWebsite.RuntimeID)); err != nil {
|
2023-07-13 11:51:17 +00:00
|
|
|
return buserr.WithDetail(constant.ErrBackupMatch, "runtime", nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if oldWebsite.WebsiteSSLID != 0 {
|
2024-05-08 10:44:24 +00:00
|
|
|
if _, err := websiteSSLRepo.GetFirst(commonRepo.WithByID(oldWebsite.WebsiteSSLID)); err != nil {
|
2023-07-13 11:51:17 +00:00
|
|
|
return buserr.WithDetail(constant.ErrBackupMatch, "ssl", nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|