fix: 解决计划任务升级导致数据异常问题 (#3744)

This commit is contained in:
ssongliu 2024-01-30 14:39:13 +08:00 committed by GitHub
parent 3846dcef86
commit 5c9315ff1c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 17 additions and 10 deletions

View file

@ -272,16 +272,16 @@ var UpdateCronjobSpec = &gormigrate.Migration{
jobs []model.Cronjob jobs []model.Cronjob
backupAccounts []model.BackupAccount backupAccounts []model.BackupAccount
) )
mapAccount := make(map[uint]string) mapAccount := make(map[uint]model.BackupAccount)
if err := tx.Find(&jobs).Error; err != nil { if err := tx.Find(&jobs).Error; err != nil {
return err return err
} }
_ = tx.Find(&backupAccounts).Error _ = tx.Find(&backupAccounts).Error
for _, item := range backupAccounts { for _, item := range backupAccounts {
mapAccount[item.ID] = item.Type mapAccount[item.ID] = item
} }
for _, job := range jobs { for _, job := range jobs {
if job.KeepLocal && mapAccount[uint(job.TargetDirID)] != constant.Local { if job.KeepLocal && mapAccount[uint(job.TargetDirID)].Type != constant.Local {
if err := tx.Model(&model.Cronjob{}). if err := tx.Model(&model.Cronjob{}).
Where("id = ?", job.ID). Where("id = ?", job.ID).
Updates(map[string]interface{}{ Updates(map[string]interface{}{
@ -316,6 +316,7 @@ var UpdateCronjobSpec = &gormigrate.Migration{
CronjobID: job.ID, CronjobID: job.ID,
Type: "snapshot", Type: "snapshot",
Name: job.Name, Name: job.Name,
FileDir: "system_snapshot",
FileName: snap.Name + ".tar.gz", FileName: snap.Name + ".tar.gz",
Source: snap.From, Source: snap.From,
BackupType: snap.From, BackupType: snap.From,
@ -324,16 +325,22 @@ var UpdateCronjobSpec = &gormigrate.Migration{
} }
continue continue
} }
itemPath := mapAccount[uint(job.TargetDirID)].BackupPath
if itemPath != "/" {
itemPath = strings.TrimPrefix(itemPath, "/") + "/"
} else {
itemPath = ""
}
if job.Type == "log" { if job.Type == "log" {
item := model.BackupRecord{ item := model.BackupRecord{
From: "cronjob", From: "cronjob",
CronjobID: job.ID, CronjobID: job.ID,
Type: "log", Type: "log",
Name: job.Name, Name: job.Name,
FileDir: path.Dir(record.File), FileDir: path.Dir(strings.TrimPrefix(record.File, itemPath)),
FileName: path.Base(record.File), FileName: path.Base(record.File),
Source: mapAccount[uint(job.TargetDirID)], Source: mapAccount[uint(job.TargetDirID)].Type,
BackupType: mapAccount[uint(job.TargetDirID)], BackupType: mapAccount[uint(job.TargetDirID)].Type,
} }
_ = tx.Create(&item).Error _ = tx.Create(&item).Error
continue continue
@ -344,14 +351,14 @@ var UpdateCronjobSpec = &gormigrate.Migration{
CronjobID: job.ID, CronjobID: job.ID,
Type: "directory", Type: "directory",
Name: job.Name, Name: job.Name,
FileDir: path.Dir(record.File), FileDir: path.Dir(strings.TrimPrefix(record.File, itemPath)),
FileName: path.Base(record.File), FileName: path.Base(record.File),
BackupType: mapAccount[uint(job.TargetDirID)], BackupType: mapAccount[uint(job.TargetDirID)].Type,
} }
if record.FromLocal { if record.FromLocal {
item.Source = constant.Local item.Source = constant.Local
} else { } else {
item.Source = mapAccount[uint(job.TargetDirID)] item.Source = mapAccount[uint(job.TargetDirID)].Type
} }
_ = tx.Create(&item).Error _ = tx.Create(&item).Error
continue continue

View file

@ -108,7 +108,7 @@ func (s sftpClient) Download(src, target string) (bool, error) {
} }
defer dstFile.Close() defer dstFile.Close()
if _, err = io.Copy(srcFile, dstFile); err != nil { if _, err = io.Copy(dstFile, srcFile); err != nil {
return false, err return false, err
} }
return true, err return true, err