From 645e6bbb790af0e1994600f0e5e76a43d2f1d05b Mon Sep 17 00:00:00 2001 From: ssongliu <73214554+ssongliu@users.noreply.github.com> Date: Fri, 8 Sep 2023 23:12:13 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=BF=AB=E7=85=A7=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E5=A4=A7=E5=B0=8F=E6=98=BE=E7=A4=BA=20(#2227?= =?UTF-8?q?)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/app/dto/setting.go | 1 + backend/app/model/snapshot.go | 1 + backend/app/service/snapshot.go | 9 +++++---- backend/app/service/snapshot_create.go | 11 ++++++++++- backend/init/migration/migrations/init.go | 2 +- backend/utils/common/common.go | 9 ++++++--- frontend/src/api/interface/setting.ts | 1 + .../src/views/setting/snapshot/snap_status/index.vue | 6 +++++- 8 files changed, 30 insertions(+), 10 deletions(-) diff --git a/backend/app/dto/setting.go b/backend/app/dto/setting.go index 6db7c01bb..49dbf992b 100644 --- a/backend/app/dto/setting.go +++ b/backend/app/dto/setting.go @@ -84,6 +84,7 @@ type SnapshotStatus struct { BackupData string `json:"backupData"` Compress string `json:"compress"` + Size string `json:"size"` Upload string `json:"upload"` } diff --git a/backend/app/model/snapshot.go b/backend/app/model/snapshot.go index 006bf3207..7aaa7e134 100644 --- a/backend/app/model/snapshot.go +++ b/backend/app/model/snapshot.go @@ -29,5 +29,6 @@ type SnapshotStatus struct { BackupData string `json:"backupData" gorm:"type:varchar(64);default:Running"` Compress string `json:"compress" gorm:"type:varchar(64);default:Waiting"` + Size string `json:"size" gorm:"type:varchar(64)"` Upload string `json:"upload" gorm:"type:varchar(64);default:Waiting"` } diff --git a/backend/app/service/snapshot.go b/backend/app/service/snapshot.go index cf05d337f..074ad9bce 100644 --- a/backend/app/service/snapshot.go +++ b/backend/app/service/snapshot.go @@ -764,7 +764,7 @@ func (u *SnapshotService) handleBackupDatas(fileOp files.FileOp, operation strin func (u *SnapshotService) handlePanelDatas(snapID uint, fileOp files.FileOp, operation string, source, target, backupDir, dockerDir string) error { switch operation { case "snapshot": - exclusionRules := "./tmp;./log;./cache;./db/1Panel.db-*;" + exclusionRules := "./tmp;./log;./cache;" if strings.Contains(backupDir, source) { exclusionRules += ("." + strings.ReplaceAll(backupDir, source, "") + ";") } @@ -776,7 +776,7 @@ func (u *SnapshotService) handlePanelDatas(snapID uint, fileOp files.FileOp, ope return fmt.Errorf("backup panel data failed, err: %v", err) } case "recover": - exclusionRules := "./tmp;./log;./cache;./db/1Panel.db-*;" + exclusionRules := "./tmp;./log;./cache;" if strings.Contains(backupDir, target) { exclusionRules += ("." + strings.ReplaceAll(backupDir, target, "") + ";") } @@ -1038,8 +1038,9 @@ func loadLogByStatus(status model.SnapshotStatus) string { logs += fmt.Sprintf("Backup installed apps from 1Panel: %s \n", status.AppData) logs += fmt.Sprintf("Backup 1Panel data directory: %s \n", status.PanelData) logs += fmt.Sprintf("Backup local backup directory for 1Panel: %s \n", status.BackupData) - logs += fmt.Sprintf("Create snapshot file: %s \n", status.BackupData) - logs += fmt.Sprintf("Upload snapshot file: %s \n", status.BackupData) + logs += fmt.Sprintf("Create snapshot file: %s \n", status.Compress) + logs += fmt.Sprintf("Snapshot size: %s \n", status.Size) + logs += fmt.Sprintf("Upload snapshot file: %s \n", status.Upload) return logs } diff --git a/backend/app/service/snapshot_create.go b/backend/app/service/snapshot_create.go index 8c3d87e96..f1ee11aa0 100644 --- a/backend/app/service/snapshot_create.go +++ b/backend/app/service/snapshot_create.go @@ -15,6 +15,7 @@ import ( "github.com/1Panel-dev/1Panel/backend/constant" "github.com/1Panel-dev/1Panel/backend/global" "github.com/1Panel-dev/1Panel/backend/utils/cmd" + "github.com/1Panel-dev/1Panel/backend/utils/common" "github.com/1Panel-dev/1Panel/backend/utils/files" ) @@ -161,8 +162,16 @@ func snapCompress(snap snapHelper, rootDir string) { return } + stat, err := os.Stat(path.Join(tmpDir, fileName)) + if err != nil { + snap.Status.Compress = err.Error() + _ = snapshotRepo.UpdateStatus(snap.Status.ID, map[string]interface{}{"compress": err.Error()}) + return + } + size := common.LoadSizeUnit(float64(stat.Size())) + global.LOG.Debugf("compress successful! size of file: %s", size) snap.Status.Compress = constant.StatusDone - _ = snapshotRepo.UpdateStatus(snap.Status.ID, map[string]interface{}{"compress": constant.StatusDone}) + _ = snapshotRepo.UpdateStatus(snap.Status.ID, map[string]interface{}{"compress": constant.StatusDone, "size": size}) } func snapUpload(snap snapHelper, account string, file string) { diff --git a/backend/init/migration/migrations/init.go b/backend/init/migration/migrations/init.go index b6b3e7518..cd4630108 100644 --- a/backend/init/migration/migrations/init.go +++ b/backend/init/migration/migrations/init.go @@ -573,7 +573,7 @@ var UpdateCronjobWithDb = &gormigrate.Migration{ } var AddTableFirewall = &gormigrate.Migration{ - ID: "20230828-add-table-firewall", + ID: "20230908-add-table-firewall", Migrate: func(tx *gorm.DB) error { if err := tx.AutoMigrate(&model.Firewall{}, model.SnapshotStatus{}, &model.Cronjob{}); err != nil { return err diff --git a/backend/utils/common/common.go b/backend/utils/common/common.go index c648b053e..ea425dd88 100644 --- a/backend/utils/common/common.go +++ b/backend/utils/common/common.go @@ -147,13 +147,16 @@ func RemoveRepeatElement(a interface{}) (ret []interface{}) { } func LoadSizeUnit(value float64) string { + if value > 1073741824 { + return fmt.Sprintf("%.2fM", value/1073741824) + } if value > 1048576 { - return fmt.Sprintf("%vM", value/1048576) + return fmt.Sprintf("%.2fM", value/1048576) } if value > 1024 { - return fmt.Sprintf("%vK", value/1024) + return fmt.Sprintf("%.2fK", value/1024) } - return fmt.Sprintf("%v", value) + return fmt.Sprintf("%.2f", value) } func LoadTimeZone() string { diff --git a/frontend/src/api/interface/setting.ts b/frontend/src/api/interface/setting.ts index e295ee868..73d2c8c5c 100644 --- a/frontend/src/api/interface/setting.ts +++ b/frontend/src/api/interface/setting.ts @@ -116,6 +116,7 @@ export namespace Setting { backupData: string; compress: string; + size: string; upload: string; } export interface UpgradeInfo { diff --git a/frontend/src/views/setting/snapshot/snap_status/index.vue b/frontend/src/views/setting/snapshot/snap_status/index.vue index e9087f966..e59238399 100644 --- a/frontend/src/views/setting/snapshot/snap_status/index.vue +++ b/frontend/src/views/setting/snapshot/snap_status/index.vue @@ -70,7 +70,9 @@