Browse Source

fix: 网站备份增加 website.json 文件

pull/55/head
ssongliu 2 years ago committed by ssongliu
parent
commit
c11c3be0de
  1. 60
      backend/app/service/website.go

60
backend/app/service/website.go

@ -1,8 +1,10 @@
package service package service
import ( import (
"bufio"
"context" "context"
"crypto/x509" "crypto/x509"
"encoding/json"
"encoding/pem" "encoding/pem"
"fmt" "fmt"
"io" "io"
@ -522,6 +524,11 @@ func (w WebsiteService) OpWebsiteHTTPS(req dto.WebsiteHTTPSOp) (dto.WebsiteHTTPS
return res, nil return res, nil
} }
type WebSiteInfo struct {
WebsiteName string `json:"websiteName"`
WebsiteType string `json:"websiteType"`
}
func handleWebsiteBackup(backupType, baseDir, backupDir, domain, backupName string) error { func handleWebsiteBackup(backupType, baseDir, backupDir, domain, backupName string) error {
website, err := websiteRepo.GetFirst(websiteRepo.WithByDomain(domain)) website, err := websiteRepo.GetFirst(websiteRepo.WithByDomain(domain))
if err != nil { if err != nil {
@ -556,18 +563,12 @@ func handleWebsiteBackup(backupType, baseDir, backupDir, domain, backupName stri
} }
} }
} }
nginxConfFile := fmt.Sprintf("%s/nginx/%s/conf/conf.d/%s.conf", constant.AppInstallDir, nginxInfo.Name, website.PrimaryDomain) if err := saveNginxConf(nginxInfo.Name, website.PrimaryDomain, tmpDir); err != nil {
src, err := os.OpenFile(nginxConfFile, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0775)
if err != nil {
return err return err
} }
defer src.Close() if err := saveWebsiteJson(&website, tmpDir); err != nil {
out, err := os.Create(fmt.Sprintf("%s/%s.conf", tmpDir, website.PrimaryDomain))
if err != nil {
return err return err
} }
defer out.Close()
_, _ = io.Copy(out, src)
if website.Type == "deployment" { if website.Type == "deployment" {
dbFile := fmt.Sprintf("%s/%s.sql", tmpDir, website.PrimaryDomain) dbFile := fmt.Sprintf("%s/%s.sql", tmpDir, website.PrimaryDomain)
@ -587,13 +588,8 @@ func handleWebsiteBackup(backupType, baseDir, backupDir, domain, backupName stri
return err return err
} }
} }
if err := handleTar(tmpDir, fmt.Sprintf("%s/%s", baseDir, backupDir), backupName+".tar.gz", ""); err != nil {
itemDir := strings.ReplaceAll(tmpDir[strings.LastIndex(tmpDir, "/"):], "/", "") return err
aheadDir := strings.ReplaceAll(tmpDir, itemDir, "")
tarcmd := exec.Command("tar", "zcvf", tmpDir+".tar.gz", "-C", aheadDir, itemDir)
stdout, err := tarcmd.CombinedOutput()
if err != nil {
return errors.New(string(stdout))
} }
_ = os.RemoveAll(tmpDir) _ = os.RemoveAll(tmpDir)
@ -615,3 +611,37 @@ func handleWebsiteBackup(backupType, baseDir, backupDir, domain, backupName stri
} }
return nil return nil
} }
func saveWebsiteJson(website *model.WebSite, tmpDir string) error {
var WebSiteInfo WebSiteInfo
WebSiteInfo.WebsiteType = website.Type
WebSiteInfo.WebsiteName = website.PrimaryDomain
remarkInfo, _ := json.Marshal(WebSiteInfo)
path := fmt.Sprintf("%s/website.json", tmpDir)
file, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE, 0666)
if err != nil {
return err
}
defer file.Close()
write := bufio.NewWriter(file)
_, _ = write.WriteString(string(remarkInfo))
write.Flush()
return nil
}
func saveNginxConf(nginxName, websiteDomain, tmpDir string) error {
nginxConfFile := fmt.Sprintf("%s/nginx/%s/conf/conf.d/%s.conf", constant.AppInstallDir, nginxName, websiteDomain)
src, err := os.OpenFile(nginxConfFile, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0775)
if err != nil {
return err
}
defer src.Close()
out, err := os.Create(fmt.Sprintf("%s/%s.conf", tmpDir, websiteDomain))
if err != nil {
return err
}
defer out.Close()
_, _ = io.Copy(out, src)
return nil
}

Loading…
Cancel
Save