Skip to content

Commit

Permalink
fix: 解决同名网站备份恢复异常
Browse files Browse the repository at this point in the history
  • Loading branch information
zhengkunwang223 committed Dec 18, 2023
1 parent 9aaa387 commit 7825e3f
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 42 deletions.
5 changes: 5 additions & 0 deletions backend/app/repo/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type IBackupRepo interface {
Update(id uint, vars map[string]interface{}) error
Delete(opts ...DBOption) error
DeleteRecord(ctx context.Context, opts ...DBOption) error
UpdateRecord(record *model.BackupRecord) error
WithByDetailName(detailName string) DBOption
WithByFileName(fileName string) DBOption
WithByType(backupType string) DBOption
Expand Down Expand Up @@ -105,6 +106,10 @@ func (u *BackupRepo) CreateRecord(record *model.BackupRecord) error {
return global.DB.Create(record).Error
}

func (u *BackupRepo) UpdateRecord(record *model.BackupRecord) error {
return global.DB.Save(record).Error
}

func (u *BackupRepo) Update(id uint, vars map[string]interface{}) error {
return global.DB.Model(&model.BackupAccount{}).Where("id = ?", id).Updates(vars).Error
}
Expand Down
31 changes: 16 additions & 15 deletions backend/app/service/backup_website.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func (u *BackupService) WebsiteBackup(req dto.CommonBackup) error {
if err != nil {
return err
}
website, err := websiteRepo.GetFirst(websiteRepo.WithDomain(req.Name))
website, err := websiteRepo.GetFirst(websiteRepo.WithAlias(req.DetailName))
if err != nil {
return err
}
Expand All @@ -40,7 +40,7 @@ func (u *BackupService) WebsiteBackup(req dto.CommonBackup) error {
record := &model.BackupRecord{
Type: "website",
Name: website.PrimaryDomain,
DetailName: "",
DetailName: req.DetailName,
Source: "LOCAL",
BackupType: "LOCAL",
FileDir: backupDir,
Expand All @@ -54,14 +54,14 @@ func (u *BackupService) WebsiteBackup(req dto.CommonBackup) error {
}

func (u *BackupService) WebsiteRecover(req dto.CommonRecover) error {
website, err := websiteRepo.GetFirst(websiteRepo.WithDomain(req.Name))
if err != nil {
return err
}
fileOp := files.NewFileOp()
if !fileOp.Stat(req.File) {
return errors.New(fmt.Sprintf("%s file is not exist", req.File))
}
website, err := websiteRepo.GetFirst(websiteRepo.WithAlias(req.DetailName))
if err != nil {
return err
}
global.LOG.Infof("recover website %s from backup file %s", req.Name, req.File)
if err := handleWebsiteRecover(&website, req.File, false); err != nil {
return err
Expand All @@ -79,15 +79,6 @@ func handleWebsiteRecover(website *model.Website, recoverFile string, isRollback
_ = os.RemoveAll(tmpPath)
}()

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)
}
}
var oldWebsite model.Website
websiteJson, err := os.ReadFile(tmpPath + "/website.json")
if err != nil {
Expand All @@ -101,6 +92,16 @@ func handleWebsiteRecover(website *model.Website, recoverFile string, isRollback
return err
}

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)
}
}

isOk := false
if !isRollback {
rollbackFile := path.Join(global.CONF.System.TmpDir, fmt.Sprintf("website/%s_%s.tar.gz", website.Alias, time.Now().Format("20060102150405")))
Expand Down
18 changes: 0 additions & 18 deletions backend/app/service/firewall.go
Original file line number Diff line number Diff line change
Expand Up @@ -582,24 +582,6 @@ func (u *FirewallService) addAddressRecord(req dto.AddrRuleOperate) error {
return nil
}

func listIpRules(strategy string) ([]string, error) {
client, err := firewall.NewFirewallClient()
if err != nil {
return nil, err
}
addrs, err := client.ListAddress()
if err != nil {
return nil, err
}
var rules []string
for _, addr := range addrs {
if addr.Strategy == strategy {
rules = append(rules, addr.Address)
}
}
return rules, nil
}

func checkPortUsed(ports, proto string, apps []portOfApp) string {
var portList []int
if strings.Contains(ports, "-") || strings.Contains(ports, ",") {
Expand Down
3 changes: 3 additions & 0 deletions backend/app/service/website_ca.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ func (w WebsiteCAService) Create(create request.WebsiteCACreate) (*request.Websi
return nil, err
}
rootCert, err := x509.ParseCertificate(rootDer)
if err != nil {
return nil, err
}
certBlock := &pem.Block{
Type: "CERTIFICATE",
Bytes: rootCert.Raw,
Expand Down
1 change: 1 addition & 0 deletions backend/init/migration/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ func Init() {
migrations.AddDatabaseSSL,
migrations.AddDefaultCA,
migrations.AddSettingRecycleBin,
migrations.UpdateWebsiteBackupRecord,
})
if err := m.Migrate(); err != nil {
global.LOG.Error(err)
Expand Down
16 changes: 16 additions & 0 deletions backend/init/migration/migrations/v_1_9.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package migrations
import (
"github.com/1Panel-dev/1Panel/backend/app/dto/request"
"github.com/1Panel-dev/1Panel/backend/app/model"
"github.com/1Panel-dev/1Panel/backend/app/repo"
"github.com/1Panel-dev/1Panel/backend/app/service"
"github.com/go-gormigrate/gormigrate/v2"
"gorm.io/gorm"
Expand Down Expand Up @@ -87,3 +88,18 @@ var AddSettingRecycleBin = &gormigrate.Migration{
return nil
},
}

var UpdateWebsiteBackupRecord = &gormigrate.Migration{
ID: "20231218-update-backup-record-for-website",
Migrate: func(tx *gorm.DB) error {
backupRepo := repo.NewIBackupRepo()
websitesBackups, _ := backupRepo.ListRecord(repo.NewCommonRepo().WithByType("website"))
if len(websitesBackups) > 0 {
for _, backup := range websitesBackups {
backup.DetailName = backup.Name
_ = backupRepo.UpdateRecord(&backup)
}
}
return nil
},
}
7 changes: 0 additions & 7 deletions backend/utils/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,6 @@ func IsCrossVersion(version1, version2 string) bool {
return v2num > v1num
}

func min(a, b int) int {
if a < b {
return a
}
return b
}

func GetUuid() string {
b := make([]byte, 16)
_, _ = io.ReadFull(rand.Reader, b)
Expand Down
3 changes: 3 additions & 0 deletions frontend/src/components/upload/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,9 @@ const acceptParams = async (params: DialogProps): Promise<void> => {
} else {
baseDir.value = `${pathRes.data}/uploads/${dir}/${name.value}/`;
}
if (type.value === 'website') {
baseDir.value = `${pathRes.data}/uploads/database/${type.value}/${detailName.value}/`;
}
upVisible.value = true;
search();
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
<el-form-item :label="$t('website.primaryDomain')" prop="primaryDomain">
<el-input v-model="form.primaryDomain"></el-input>
</el-form-item>
<el-form-item :label="$t('website.alias')" prop="primaryDomain">
<el-input v-model="form.alias" disabled></el-input>
</el-form-item>
<el-form-item :label="$t('website.group')" prop="webSiteGroupID">
<el-select v-model="form.webSiteGroupId">
<el-option
Expand All @@ -21,6 +24,7 @@
<el-form-item prop="IPV6">
<el-checkbox v-model="form.IPV6" :label="$t('website.ipv6')" size="large" />
</el-form-item>

<el-form-item>
<el-button type="primary" @click="submit(websiteForm)" :disabled="loading">
{{ $t('commons.button.save') }}
Expand Down Expand Up @@ -58,6 +62,7 @@ const form = reactive({
remark: '',
webSiteGroupId: 0,
IPV6: false,
alias: '',
});
const rules = ref({
primaryDomain: [Rules.requiredInput],
Expand Down Expand Up @@ -91,6 +96,7 @@ const search = async () => {
form.remark = res.data.remark;
form.webSiteGroupId = res.data.webSiteGroupId;
form.IPV6 = res.data.IPV6;
form.alias = res.data.alias;
});
};
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/views/website/website/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ const buttons = [
let params = {
type: 'website',
name: row.primaryDomain,
detailName: '',
detailName: row.alias,
};
dialogBackupRef.value!.acceptParams(params);
},
Expand All @@ -390,7 +390,7 @@ const buttons = [
let params = {
type: 'website',
name: row.primaryDomain,
detailName: '',
detailName: row.alias,
};
uploadRef.value!.acceptParams(params);
},
Expand Down

0 comments on commit 7825e3f

Please sign in to comment.