Skip to content
This repository was archived by the owner on Feb 23, 2023. It is now read-only.

Commit 6286b25

Browse files
authored
Merge pull request #11 from jeessy2:oldBackup
fix: 删除过期文件
2 parents c110f07 + 61e7496 commit 6286b25

File tree

3 files changed

+67
-29
lines changed

3 files changed

+67
-29
lines changed

client/delete_old_file.go

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,43 +12,44 @@ import (
1212

1313
// DeleteOldBackup for client
1414
func 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
}

util/time_util.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package util
2+
3+
import (
4+
"time"
5+
)
6+
7+
// GetDelaySeconds 获取第一次启动的延时时间(秒)
8+
func GetDelaySeconds(startTime int) time.Duration {
9+
now := time.Now().Truncate(time.Second)
10+
midNightNow := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location())
11+
midNightTom := midNightNow.Add(24 * time.Hour)
12+
13+
var seconds int
14+
if now.Hour() >= startTime {
15+
// tomorrow
16+
seconds = int(midNightTom.Add(time.Hour * time.Duration(startTime)).Sub(now).Seconds())
17+
} else {
18+
seconds = int(midNightNow.Add(time.Hour * time.Duration(startTime)).Sub(now).Seconds())
19+
}
20+
21+
return time.Second * time.Duration(seconds)
22+
}

util/time_util_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package util
2+
3+
import (
4+
"testing"
5+
"time"
6+
)
7+
8+
// TestGetDelaySeconds
9+
func TestGetDelaySeconds(t *testing.T) {
10+
hour := 1
11+
runTime := time.Now().Add(GetDelaySeconds(hour))
12+
if runTime.Hour() != hour || runTime.Minute() != 0 {
13+
t.Error("GetDelaySeconds not correct")
14+
}
15+
}

0 commit comments

Comments
 (0)