Skip to content

Commit be6d3ea

Browse files
authored
fix: Update compression backup exclusion rules (#9323)
1 parent 64fd715 commit be6d3ea

File tree

14 files changed

+85
-94
lines changed

14 files changed

+85
-94
lines changed

agent/app/dto/setting.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ type SettingInfo struct {
2323
AppStoreSyncStatus string `json:"appStoreSyncStatus"`
2424

2525
FileRecycleBin string `json:"fileRecycleBin"`
26-
27-
SnapshotIgnore string `json:"snapshotIgnore"`
2826
}
2927

3028
type SettingUpdate struct {

agent/app/dto/snapshot.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,21 @@ type SnapshotCreate struct {
2828
WithOperationLog bool `json:"withOperationLog"`
2929
WithSystemLog bool `json:"withSystemLog"`
3030
WithTaskLog bool `json:"withTaskLog"`
31+
32+
IgnoreFiles []string `json:"ignoreFiles"`
3133
}
3234

3335
type SnapshotData struct {
3436
AppData []DataTree `json:"appData"`
3537
BackupData []DataTree `json:"backupData"`
3638
PanelData []DataTree `json:"panelData"`
3739

38-
WithMonitorData bool `json:"withMonitorData"`
39-
WithLoginLog bool `json:"withLoginLog"`
40-
WithOperationLog bool `json:"withOperationLog"`
41-
WithSystemLog bool `json:"withSystemLog"`
42-
WithTaskLog bool `json:"withTaskLog"`
40+
WithMonitorData bool `json:"withMonitorData"`
41+
WithLoginLog bool `json:"withLoginLog"`
42+
WithOperationLog bool `json:"withOperationLog"`
43+
WithSystemLog bool `json:"withSystemLog"`
44+
WithTaskLog bool `json:"withTaskLog"`
45+
IgnoreFiles []string `json:"ignoreFiles"`
4346
}
4447
type DataTree struct {
4548
ID string `json:"id"`

agent/app/model/snapshot.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ type Snapshot struct {
2323
WithOperationLog bool `json:"withOperationLog"`
2424
WithSystemLog bool `json:"withSystemLog"`
2525
WithTaskLog bool `json:"withTaskLog"`
26+
IgnoreFiles string `json:"ignoreFiles"`
2627

2728
InterruptStep string `json:"interruptStep"`
2829
RecoverStatus string `json:"recoverStatus"`

agent/app/service/cronjob_backup.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,7 @@ func (u *CronjobService) handleSnapshot(cronjob model.Cronjob, jobRecord model.J
285285
WithOperationLog: true,
286286
WithSystemLog: true,
287287
WithTaskLog: true,
288+
IgnoreFiles: strings.Split(cronjob.ExclusionRules, ","),
288289
}
289290

290291
if err := NewISnapshotService().SnapshotCreate(taskItem, req, jobRecord.ID, cronjob.RetryTimes, cronjob.Timeout); err != nil {

agent/app/service/snapshot_create.go

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"fmt"
77
"os"
88
"path"
9+
"path/filepath"
910
"strings"
1011
"sync"
1112
"time"
@@ -55,6 +56,7 @@ func (u *SnapshotService) SnapshotCreate(parentTask *task.Task, req dto.Snapshot
5556
WithOperationLog: req.WithOperationLog,
5657
WithTaskLog: req.WithTaskLog,
5758
WithSystemLog: req.WithSystemLog,
59+
IgnoreFiles: strings.Join(req.IgnoreFiles, ","),
5860

5961
Version: versionItem.Value,
6062
Status: constant.StatusWaiting,
@@ -398,6 +400,7 @@ func snapBackupData(snap snapHelper, req dto.SnapshotCreate, targetDir string) e
398400
snap.Task.LogStart(i18n.GetMsgByKey("SnapLocalBackup"))
399401

400402
excludes := loadBackupExcludes(snap, req.BackupData)
403+
excludes = append(excludes, req.IgnoreFiles...)
401404
excludes = append(excludes, "./system_snapshot")
402405
for _, item := range req.AppData {
403406
for _, itemApp := range item.Children {
@@ -421,7 +424,8 @@ func loadBackupExcludes(snap snapHelper, req []dto.DataTree) []string {
421424
if err := snap.snapAgentDB.Where("file_dir = ? AND file_name = ?", strings.TrimPrefix(path.Dir(item.Path), global.Dir.LocalBackupDir+"/"), path.Base(item.Path)).Delete(&model.BackupRecord{}).Error; err != nil {
422425
snap.Task.LogWithStatus("delete backup file from database", err)
423426
}
424-
excludes = append(excludes, "."+strings.TrimPrefix(item.Path, global.Dir.LocalBackupDir))
427+
itemDir, _ := filepath.Rel(item.Path, global.Dir.LocalBackupDir)
428+
excludes = append(excludes, itemDir)
425429
} else {
426430
excludes = append(excludes, loadBackupExcludes(snap, item.Children)...)
427431
}
@@ -433,7 +437,8 @@ func loadAppBackupExcludes(req []dto.DataTree) []string {
433437
for _, item := range req {
434438
if len(item.Children) == 0 {
435439
if !item.IsCheck {
436-
excludes = append(excludes, "."+strings.TrimPrefix(item.Path, path.Join(global.Dir.LocalBackupDir)))
440+
itemDir, _ := filepath.Rel(item.Path, global.Dir.LocalBackupDir)
441+
excludes = append(excludes, itemDir)
437442
}
438443
} else {
439444
excludes = append(excludes, loadAppBackupExcludes(item.Children)...)
@@ -466,26 +471,21 @@ func snapPanelData(snap snapHelper, req dto.SnapshotCreate, targetDir string) er
466471

467472
rootDir := global.Dir.DataDir
468473
if strings.Contains(global.Dir.LocalBackupDir, rootDir) {
469-
excludes = append(excludes, "."+strings.ReplaceAll(global.Dir.LocalBackupDir, rootDir, ""))
474+
itemDir, _ := filepath.Rel(rootDir, global.Dir.LocalBackupDir)
475+
excludes = append(excludes, itemDir)
470476
}
471477
if len(snap.OpenrestyDir) != 0 && strings.Contains(snap.OpenrestyDir, rootDir) {
472-
excludes = append(excludes, "."+strings.ReplaceAll(snap.OpenrestyDir, rootDir, ""))
473-
}
474-
ignoreVal, _ := settingRepo.Get(settingRepo.WithByKey("SnapshotIgnore"))
475-
rules := strings.Split(ignoreVal.Value, ",")
476-
for _, ignore := range rules {
477-
if len(ignore) == 0 || cmd.CheckIllegal(ignore) {
478-
continue
479-
}
480-
excludes = append(excludes, "."+strings.ReplaceAll(ignore, rootDir, ""))
478+
itemDir, _ := filepath.Rel(rootDir, snap.OpenrestyDir)
479+
excludes = append(excludes, itemDir)
481480
}
481+
excludes = append(excludes, req.IgnoreFiles...)
482482
err := snap.FileOp.TarGzCompressPro(false, rootDir, path.Join(targetDir, "1panel_data.tar.gz"), "", strings.Join(excludes, ","))
483483
snap.Task.LogWithStatus(i18n.GetMsgByKey("SnapCompressPanel"), err)
484484
if err != nil {
485485
return err
486486
}
487487
if len(snap.OpenrestyDir) != 0 {
488-
err := snap.FileOp.TarGzCompressPro(false, snap.OpenrestyDir, path.Join(targetDir, "website.tar.gz"), "", "")
488+
err := snap.FileOp.TarGzCompressPro(false, snap.OpenrestyDir, path.Join(targetDir, "website.tar.gz"), "", strings.Join(req.IgnoreFiles, ","))
489489
snap.Task.LogWithStatus(i18n.GetMsgByKey("SnapWebsite"), err)
490490
if err != nil {
491491
return err
@@ -499,7 +499,8 @@ func loadPanelExcludes(req []dto.DataTree) []string {
499499
for _, item := range req {
500500
if len(item.Children) == 0 {
501501
if !item.IsCheck {
502-
excludes = append(excludes, "."+strings.TrimPrefix(item.Path, path.Join(global.Dir.BaseDir, "1panel")))
502+
itemDir, _ := filepath.Rel(item.Path, path.Join(global.Dir.BaseDir, "1panel"))
503+
excludes = append(excludes, itemDir)
503504
}
504505
} else {
505506
excludes = append(excludes, loadPanelExcludes(item.Children)...)

agent/init/migration/migrate.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ func InitAgentDB() {
2727
migrations.UpdateRuntime,
2828
migrations.AddSnapshotRule,
2929
migrations.UpdatePHPRuntime,
30+
migrations.AddSnapshotIgnore,
3031
})
3132
if err := m.Migrate(); err != nil {
3233
global.LOG.Error(err)

agent/init/migration/migrations/init.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -185,9 +185,6 @@ var InitSetting = &gormigrate.Migration{
185185
if err := tx.Create(&model.Setting{Key: "FileRecycleBin", Value: constant.StatusEnable}).Error; err != nil {
186186
return err
187187
}
188-
if err := tx.Create(&model.Setting{Key: "SnapshotIgnore", Value: "*.sock"}).Error; err != nil {
189-
return err
190-
}
191188

192189
if err := tx.Create(&model.Setting{Key: "LocalSSHConn", Value: ""}).Error; err != nil {
193190
return err
@@ -332,11 +329,18 @@ var AddSnapshotRule = &gormigrate.Migration{
332329
)
333330
},
334331
}
335-
336332
var UpdatePHPRuntime = &gormigrate.Migration{
337333
ID: "20250624-update-php-runtime",
338334
Migrate: func(tx *gorm.DB) error {
339335
service.HandleOldPHPRuntime()
340336
return nil
341337
},
342338
}
339+
var AddSnapshotIgnore = &gormigrate.Migration{
340+
ID: "20250627-add-snapshot-ignore",
341+
Migrate: func(tx *gorm.DB) error {
342+
return tx.AutoMigrate(
343+
&model.Snapshot{},
344+
)
345+
},
346+
}

agent/utils/files/file_op.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -815,17 +815,16 @@ func (f FileOp) TarGzCompressPro(withDir bool, src, dst, secret, exclusionRules
815815
if len(exclude) == 0 {
816816
continue
817817
}
818+
if strings.HasPrefix(exclude, "/") {
819+
exclude, _ = filepath.Rel(src, exclude)
820+
}
818821
if _, ok := exMap[exclude]; ok {
819822
continue
820823
}
821824
exStr += fmt.Sprintf(" --exclude '%s'", exclude)
822825
exMap[exclude] = struct{}{}
823826
}
824827

825-
itemPrefix := filepath.Base(src)
826-
if itemPrefix == "/" {
827-
itemPrefix = ""
828-
}
829828
if len(secret) != 0 {
830829
commands = fmt.Sprintf("tar %s -zcf - %s | openssl enc -aes-256-cbc -salt -k '%s' -out %s", exStr, srcItem, secret, dst)
831830
global.LOG.Debug(strings.ReplaceAll(commands, fmt.Sprintf(" %s ", secret), "******"))

frontend/src/api/interface/cronjob.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export namespace Cronjob {
2323
appID: string;
2424
website: string;
2525
exclusionRules: string;
26+
ignoreFiles: Array<string>;
2627
dbType: string;
2728
dbName: string;
2829
url: string;
Lines changed: 25 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<template>
2-
<DrawerPro v-model="drawerVisible" :header="$t('setting.ignoreRule')" @close="handleClose" size="small">
3-
<el-alert :closable="false" type="warning">{{ $t('setting.ignoreHelper') }}</el-alert>
2+
<div>
43
<el-form ref="formRef" :model="form" :rules="rules" v-loading="loading" class="mt-2">
54
<el-form-item prop="tmpRule">
65
<div class="w-full">
@@ -13,6 +12,7 @@
1312
/>
1413
<FileList @choose="loadDir" :path="baseDir" :isAll="true"></FileList>
1514
</div>
15+
<span class="input-help">{{ $t('cronjob.exclusionRulesHelper') }}</span>
1616
</el-form-item>
1717
</el-form>
1818

@@ -30,26 +30,26 @@
3030
</template>
3131
</el-table-column>
3232
</el-table>
33-
<template #footer>
34-
<el-button @click="drawerVisible = false">{{ $t('commons.button.cancel') }}</el-button>
35-
<el-button :disabled="loading" type="primary" @click="onSave()">
36-
{{ $t('commons.button.save') }}
37-
</el-button>
38-
</template>
39-
</DrawerPro>
33+
</div>
4034
</template>
4135
<script lang="ts" setup>
4236
import { reactive, ref } from 'vue';
4337
import i18n from '@/lang';
44-
import { MsgSuccess } from '@/utils/message';
4538
import FileList from '@/components/file-list/index.vue';
4639
import { FormInstance } from 'element-plus';
47-
import { getAgentSettingInfo, loadBaseDir, updateAgentSetting } from '@/api/modules/setting';
40+
import { loadBaseDir } from '@/api/modules/setting';
4841
4942
const loading = ref();
5043
const baseDir = ref();
51-
const drawerVisible = ref(false);
5244
const tableList = ref();
45+
const em = defineEmits(['update:files']);
46+
47+
const props = defineProps({
48+
files: {
49+
type: Array<String>,
50+
default: [],
51+
},
52+
});
5353
5454
const form = reactive({
5555
tmpRule: '',
@@ -76,17 +76,6 @@ function checkData(rule: any, value: any, callback: any) {
7676
callback();
7777
}
7878
79-
const acceptParams = async (): Promise<void> => {
80-
loadPath();
81-
const res = await getAgentSettingInfo();
82-
tableList.value = [];
83-
let items = res.data.snapshotIgnore.split(',');
84-
for (const item of items) {
85-
tableList.value.push({ value: item });
86-
}
87-
drawerVisible.value = true;
88-
};
89-
9079
const loadPath = async () => {
9180
const pathRes = await loadBaseDir();
9281
baseDir.value = pathRes.data;
@@ -106,35 +95,25 @@ const handleAdd = (formEl: FormInstance | undefined) => {
10695
tableList.value.push({ value: item });
10796
}
10897
}
98+
em(
99+
'update:files',
100+
tableList.value.map((item) => item.value),
101+
);
109102
form.tmpRule = '';
110103
});
111104
};
112105
const handleDelete = (index: number) => {
113106
tableList.value.splice(index, 1);
107+
em(
108+
'update:files',
109+
tableList.value.map((item) => item.value),
110+
);
114111
};
115112
116-
const onSave = async () => {
117-
let list = [];
118-
for (const item of tableList.value) {
119-
list.push(item.value);
120-
}
121-
await updateAgentSetting({ key: 'SnapshotIgnore', value: list.join(',') })
122-
.then(async () => {
123-
MsgSuccess(i18n.global.t('commons.msg.operationSuccess'));
124-
loading.value = false;
125-
drawerVisible.value = false;
126-
return;
127-
})
128-
.catch(() => {
129-
loading.value = false;
130-
});
131-
};
132-
133-
const handleClose = () => {
134-
drawerVisible.value = false;
135-
};
136-
137-
defineExpose({
138-
acceptParams,
113+
onMounted(() => {
114+
loadPath();
115+
tableList.value = props.files.map((item) => {
116+
return { value: item };
117+
});
139118
});
140119
</script>

0 commit comments

Comments
 (0)