mirror of
https://github.com/1Panel-dev/1Panel.git
synced 2025-10-06 05:24:33 +08:00
fix: Delete the data generated by the current snapshot (#8245)
This commit is contained in:
parent
0425cdcbb3
commit
b3cd10b743
6 changed files with 34 additions and 17 deletions
|
@ -36,7 +36,7 @@ func (b *BaseApi) CreateSnapshot(c *gin.Context) {
|
|||
return
|
||||
}
|
||||
|
||||
if err := snapshotService.SnapshotCreate(req, false); err != nil {
|
||||
if err := snapshotService.SnapshotCreate(req, 0); err != nil {
|
||||
helper.InternalServer(c, err)
|
||||
return
|
||||
}
|
||||
|
|
|
@ -215,7 +215,7 @@ func (u *CronjobService) handleSystemLog(cronjob model.Cronjob, startTime time.T
|
|||
return nil
|
||||
}
|
||||
|
||||
func (u *CronjobService) handleSnapshot(cronjob model.Cronjob, startTime time.Time, taskID string) error {
|
||||
func (u *CronjobService) handleSnapshot(cronjob model.Cronjob, jobRecord model.JobRecords) error {
|
||||
accountMap, err := NewBackupClientMap(strings.Split(cronjob.SourceAccountIDs, ","))
|
||||
if err != nil {
|
||||
return err
|
||||
|
@ -239,9 +239,9 @@ func (u *CronjobService) handleSnapshot(cronjob model.Cronjob, startTime time.Ti
|
|||
scope = "agent"
|
||||
}
|
||||
req := dto.SnapshotCreate{
|
||||
Name: fmt.Sprintf("snapshot-1panel-%s-%s-linux-%s-%s", scope, versionItem.Value, loadOs(), startTime.Format(constant.DateTimeSlimLayout)+common.RandStrAndNum(5)),
|
||||
Name: fmt.Sprintf("snapshot-1panel-%s-%s-linux-%s-%s", scope, versionItem.Value, loadOs(), jobRecord.StartTime.Format(constant.DateTimeSlimLayout)+common.RandStrAndNum(5)),
|
||||
Secret: cronjob.Secret,
|
||||
TaskID: taskID,
|
||||
TaskID: jobRecord.TaskID,
|
||||
|
||||
SourceAccountIDs: record.SourceAccountIDs,
|
||||
DownloadAccountID: cronjob.DownloadAccountID,
|
||||
|
@ -255,7 +255,7 @@ func (u *CronjobService) handleSnapshot(cronjob model.Cronjob, startTime time.Ti
|
|||
WithTaskLog: true,
|
||||
}
|
||||
|
||||
if err := NewISnapshotService().SnapshotCreate(req, true); err != nil {
|
||||
if err := NewISnapshotService().SnapshotCreate(req, jobRecord.ID); err != nil {
|
||||
return err
|
||||
}
|
||||
record.FileName = req.Name + ".tar.gz"
|
||||
|
|
|
@ -63,7 +63,7 @@ func (u *CronjobService) HandleJob(cronjob *model.Cronjob) {
|
|||
err = u.handleSystemLog(*cronjob, record.StartTime)
|
||||
case "snapshot":
|
||||
_ = cronjobRepo.UpdateRecords(record.ID, map[string]interface{}{"records": record.Records})
|
||||
err = u.handleSnapshot(*cronjob, record.StartTime, record.TaskID)
|
||||
err = u.handleSnapshot(*cronjob, record)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
|
|
|
@ -619,6 +619,14 @@ func (u *FirewallService) addPortsBeforeStart(client firewall.FirewallClient) er
|
|||
if err := client.Port(fireClient.FireInfo{Port: global.CONF.Base.Port, Protocol: "tcp", Strategy: "accept"}, "add"); err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
var portSetting model.Setting
|
||||
_ = global.CoreDB.Where("key = ?", "ServerPort").First(&portSetting).Error
|
||||
if len(portSetting.Value) != 0 {
|
||||
if err := client.Port(fireClient.FireInfo{Port: portSetting.Value, Protocol: "tcp", Strategy: "accept"}, "add"); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
if err := client.Port(fireClient.FireInfo{Port: "22", Protocol: "tcp", Strategy: "accept"}, "add"); err != nil {
|
||||
return err
|
||||
|
|
|
@ -30,7 +30,7 @@ type SnapshotService struct {
|
|||
type ISnapshotService interface {
|
||||
SearchWithPage(req dto.PageSnapshot) (int64, interface{}, error)
|
||||
LoadSnapshotData() (dto.SnapshotData, error)
|
||||
SnapshotCreate(req dto.SnapshotCreate, isCron bool) error
|
||||
SnapshotCreate(req dto.SnapshotCreate, jobID uint) error
|
||||
SnapshotReCreate(id uint) error
|
||||
SnapshotRecover(req dto.SnapshotRecover) error
|
||||
SnapshotRollback(req dto.SnapshotRecover) error
|
||||
|
@ -151,6 +151,7 @@ func (u *SnapshotService) Delete(req dto.SnapshotBatchDelete) error {
|
|||
global.LOG.Debugf("remove snapshot file %s.tar.gz from %s", snap.Name, item.name)
|
||||
_, _ = item.client.Delete(path.Join(item.backupPath, "system_snapshot", snap.Name+".tar.gz"))
|
||||
}
|
||||
_ = backupRepo.DeleteRecord(context.Background(), repo.WithByType("snapshot"), backupRepo.WithByFileName(snap.Name+".tar.gz"))
|
||||
}
|
||||
|
||||
if err := snapshotRepo.Delete(repo.WithByID(snap.ID)); err != nil {
|
||||
|
|
|
@ -26,14 +26,14 @@ import (
|
|||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func (u *SnapshotService) SnapshotCreate(req dto.SnapshotCreate, isCron bool) error {
|
||||
func (u *SnapshotService) SnapshotCreate(req dto.SnapshotCreate, jobID uint) error {
|
||||
versionItem, _ := settingRepo.Get(settingRepo.WithByKey("SystemVersion"))
|
||||
|
||||
scope := "core"
|
||||
if !global.IsMaster {
|
||||
scope = "agent"
|
||||
}
|
||||
if !isCron {
|
||||
if jobID == 0 {
|
||||
req.Name = fmt.Sprintf("1panel-%s-%s-linux-%s-%s", versionItem.Value, scope, loadOs(), time.Now().Format(constant.DateTimeSlimLayout))
|
||||
}
|
||||
appItem, _ := json.Marshal(req.AppData)
|
||||
|
@ -70,14 +70,14 @@ func (u *SnapshotService) SnapshotCreate(req dto.SnapshotCreate, isCron bool) er
|
|||
global.LOG.Errorf("new task for create snapshot failed, err: %v", err)
|
||||
return err
|
||||
}
|
||||
if !isCron {
|
||||
if jobID == 0 {
|
||||
go func() {
|
||||
_ = handleSnapshot(req, taskItem)
|
||||
_ = handleSnapshot(req, taskItem, jobID)
|
||||
}()
|
||||
return nil
|
||||
}
|
||||
|
||||
return handleSnapshot(req, taskItem)
|
||||
return handleSnapshot(req, taskItem, jobID)
|
||||
}
|
||||
|
||||
func (u *SnapshotService) SnapshotReCreate(id uint) error {
|
||||
|
@ -108,13 +108,13 @@ func (u *SnapshotService) SnapshotReCreate(id uint) error {
|
|||
return err
|
||||
}
|
||||
go func() {
|
||||
_ = handleSnapshot(req, taskItem)
|
||||
_ = handleSnapshot(req, taskItem, 0)
|
||||
}()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func handleSnapshot(req dto.SnapshotCreate, taskItem *task.Task) error {
|
||||
func handleSnapshot(req dto.SnapshotCreate, taskItem *task.Task, jobID uint) error {
|
||||
rootDir := path.Join(global.Dir.TmpDir, "system", req.Name)
|
||||
itemHelper := snapHelper{SnapID: req.ID, Task: *taskItem, FileOp: files.NewFileOp(), Ctx: context.Background()}
|
||||
baseDir := path.Join(rootDir, "base")
|
||||
|
@ -122,7 +122,16 @@ func handleSnapshot(req dto.SnapshotCreate, taskItem *task.Task) error {
|
|||
|
||||
taskItem.AddSubTaskWithAlias(
|
||||
"SnapDBInfo",
|
||||
func(t *task.Task) error { return loadDbConn(&itemHelper, rootDir, req) },
|
||||
func(t *task.Task) error {
|
||||
if err := loadDbConn(&itemHelper, rootDir, req); err != nil {
|
||||
return err
|
||||
}
|
||||
_ = itemHelper.snapAgentDB.Where("id = ?", req.ID).Delete(&model.Snapshot{}).Error
|
||||
if jobID != 0 {
|
||||
_ = itemHelper.snapAgentDB.Where("id = ?", jobID).Delete(&model.JobRecords{}).Error
|
||||
}
|
||||
return nil
|
||||
},
|
||||
nil,
|
||||
)
|
||||
|
||||
|
@ -198,6 +207,7 @@ func handleSnapshot(req dto.SnapshotCreate, taskItem *task.Task) error {
|
|||
|
||||
type snapHelper struct {
|
||||
SnapID uint
|
||||
SnapName string
|
||||
snapAgentDB *gorm.DB
|
||||
snapCoreDB *gorm.DB
|
||||
Ctx context.Context
|
||||
|
@ -251,8 +261,6 @@ func loadDbConn(snap *snapHelper, targetDir string, req dto.SnapshotCreate) erro
|
|||
return err
|
||||
}
|
||||
}
|
||||
|
||||
_ = snap.snapAgentDB.Where("id = ?", snap.SnapID).Delete(&model.Snapshot{}).Error
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue