@@ -10,6 +10,7 @@ import (
1010 "sync"
1111 "time"
1212
13+ "github.com/1Panel-dev/1Panel/backend/app/dto"
1314 "github.com/1Panel-dev/1Panel/backend/app/model"
1415 "github.com/1Panel-dev/1Panel/backend/constant"
1516 "github.com/1Panel-dev/1Panel/backend/global"
@@ -38,6 +39,10 @@ func (u *CronjobService) HandleJob(cronjob *model.Cronjob) {
3839 message , err = u .handleShell (cronjob .Type , cronjob .Name , cronjob .Script )
3940 }
4041 u .HandleRmExpired ("LOCAL" , "" , "" , cronjob , nil )
42+ case "snapshot" :
43+ messageItem := ""
44+ messageItem , record .File , err = u .handleSnapshot (cronjob , record .StartTime )
45+ message = []byte (messageItem )
4146 case "curl" :
4247 if len (cronjob .URL ) == 0 {
4348 return
@@ -60,6 +65,7 @@ func (u *CronjobService) HandleJob(cronjob *model.Cronjob) {
6065 global .LOG .Errorf ("cut website log file failed, err: %v" , err )
6166 }
6267 }
68+
6369 if err != nil {
6470 cronjobRepo .EndRecords (record , constant .StatusFailed , err .Error (), string (message ))
6571 return
@@ -83,7 +89,7 @@ func (u *CronjobService) handleShell(cronType, cornName, script string) ([]byte,
8389 }
8490 stdout , err := cmd .ExecCronjobWithTimeOut (script , handleDir , 24 * time .Hour )
8591 if err != nil {
86- return nil , err
92+ return [] byte ( stdout ) , err
8793 }
8894 return []byte (stdout ), nil
8995}
@@ -187,6 +193,10 @@ func (u *CronjobService) HandleRmExpired(backType, backupPath, localDir string,
187193 fileItem = strings .TrimPrefix (file , localDir + "/" )
188194 }
189195 }
196+
197+ if cronjob .Type == "snapshot" {
198+ _ = snapshotRepo .Delete (commonRepo .WithByName (strings .TrimSuffix (path .Base (fileItem ), ".tar.gz" )))
199+ }
190200 _ , _ = backClient .Delete (fileItem )
191201 }
192202 }
@@ -222,7 +232,7 @@ func handleTar(sourceDir, targetDir, name, exclusionRules string) error {
222232 path = sourceDir
223233 }
224234
225- commands := fmt .Sprintf ("tar -zcf %s %s %s" , targetDir + "/" + name , excludeRules , path )
235+ commands := fmt .Sprintf ("tar -zcf --warning=no-file-changed --ignore-failed-read %s %s %s" , targetDir + "/" + name , excludeRules , path )
226236 global .LOG .Debug (commands )
227237 stdout , err := cmd .ExecWithTimeOut (commands , 24 * time .Hour )
228238 if err != nil {
@@ -550,3 +560,27 @@ func (u *CronjobService) handleWebsite(cronjob model.Cronjob, backup model.Backu
550560 u .HandleRmExpired (backup .Type , backup .BackupPath , localDir , & cronjob , client )
551561 return paths , nil
552562}
563+
564+ func (u * CronjobService ) handleSnapshot (cronjob * model.Cronjob , startTime time.Time ) (string , string , error ) {
565+ backup , err := backupRepo .Get (commonRepo .WithByID (uint (cronjob .TargetDirID )))
566+ if err != nil {
567+ return "" , "" , err
568+ }
569+ client , err := NewIBackupService ().NewClient (& backup )
570+ if err != nil {
571+ return "" , "" , err
572+ }
573+
574+ req := dto.SnapshotCreate {
575+ From : backup .Type ,
576+ }
577+ message , name , err := NewISnapshotService ().HandleSnapshot (true , req , startTime .Format ("20060102150405" ))
578+ if err != nil {
579+ return message , "" , err
580+ }
581+
582+ path := path .Join (strings .TrimPrefix (backup .BackupPath , "/" ), "system_snapshot" , name + ".tar.gz" )
583+
584+ u .HandleRmExpired (backup .Type , backup .BackupPath , "" , cronjob , client )
585+ return message , path , nil
586+ }
0 commit comments