@@ -12,43 +12,44 @@ import (
1212
1313// DeleteOldBackup for client
1414func DeleteOldBackup () {
15- // sleep 30 minutes
16- time .Sleep (30 * time .Minute )
1715 for {
16+ delay := util .GetDelaySeconds (2 )
17+ log .Printf ("删除过期的备份文件将在 %.1f 小时后运行\n " , delay .Hours ())
18+ time .Sleep (delay )
19+
1820 conf , err := entity .GetConfigCache ()
19- if err == nil {
20- for _ , backupConf := range conf .BackupConfig {
21- // read from current path
22- backupFiles , err := ioutil .ReadDir (backupConf .GetProjectPath ())
23- if err != nil {
24- log .Println ("Read dir with error :" , err )
25- continue
26- }
21+ if err != nil {
22+ return
23+ }
2724
28- // delete client files
29- ago := time .Now ()
30- for _ , conf := range conf .BackupConfig {
31- lastDay , _ := time .ParseDuration ("-" + strconv .Itoa (conf .SaveDays * 24 ) + "h" )
32- ago = ago .Add (lastDay )
25+ for _ , backupConf := range conf .BackupConfig {
26+ if ! backupConf .NotEmptyProject () {
27+ continue
28+ }
29+ // read from current path
30+ backupFiles , err := ioutil .ReadDir (backupConf .GetProjectPath ())
31+ if err != nil {
32+ log .Printf ("读取项目 %s 目录失败! ERR: %s\n " , backupConf .ProjectName , err )
33+ continue
34+ }
35+
36+ // delete client files
37+ subDuration , _ := time .ParseDuration ("-" + strconv .Itoa (backupConf .SaveDays * 24 ) + "h" )
38+ before := time .Now ().Add (subDuration )
3339
34- // delete older file when file numbers gt MaxSaveDays
35- for _ , backupFile := range backupFiles {
36- if backupFile .ModTime ().Before (ago ) {
37- filepath := backupConf .GetProjectPath () + "/" + backupFile .Name ()
38- err := os .Remove (filepath )
39- if err != nil {
40- log .Printf ("删除过期的文件 %s 失败" , filepath )
41- } else {
42- log .Printf ("删除过期的文件 %s 成功" , filepath )
43- }
44- }
40+ // delete older file when file numbers gt MaxSaveDays
41+ for _ , backupFile := range backupFiles {
42+ if backupFile .ModTime ().Before (before ) {
43+ filepath := backupConf .GetProjectPath () + string (os .PathSeparator ) + backupFile .Name ()
44+ err := os .Remove (filepath )
45+ if err != nil {
46+ log .Printf ("删除过期的文件 %s 失败" , filepath )
47+ } else {
48+ log .Printf ("删除过期的文件 %s 成功" , filepath )
4549 }
4650 }
4751 }
48-
4952 }
50- // sleep
51- util .SleepForFileDelete ()
5253 }
5354
5455}
0 commit comments