|
@@ -12,43 +12,44 @@ import (
|
|
|
|
|
|
// DeleteOldBackup for client
|
|
|
func DeleteOldBackup() {
|
|
|
- // sleep 30 minutes
|
|
|
- time.Sleep(30 * time.Minute)
|
|
|
for {
|
|
|
+ delay := util.GetDelaySeconds(2)
|
|
|
+ log.Printf("删除过期的备份文件将在 %.1f 小时后运行\n", delay.Hours())
|
|
|
+ time.Sleep(delay)
|
|
|
+
|
|
|
conf, err := entity.GetConfigCache()
|
|
|
- if err == nil {
|
|
|
- for _, backupConf := range conf.BackupConfig {
|
|
|
- // read from current path
|
|
|
- backupFiles, err := ioutil.ReadDir(backupConf.GetProjectPath())
|
|
|
- if err != nil {
|
|
|
- log.Println("Read dir with error :", err)
|
|
|
- continue
|
|
|
- }
|
|
|
+ if err != nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
|
|
|
- // delete client files
|
|
|
- ago := time.Now()
|
|
|
- for _, conf := range conf.BackupConfig {
|
|
|
- lastDay, _ := time.ParseDuration("-" + strconv.Itoa(conf.SaveDays*24) + "h")
|
|
|
- ago = ago.Add(lastDay)
|
|
|
+ for _, backupConf := range conf.BackupConfig {
|
|
|
+ if !backupConf.NotEmptyProject() {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ // read from current path
|
|
|
+ backupFiles, err := ioutil.ReadDir(backupConf.GetProjectPath())
|
|
|
+ if err != nil {
|
|
|
+ log.Printf("读取项目 %s 目录失败! ERR: %s\n", backupConf.ProjectName, err)
|
|
|
+ continue
|
|
|
+ }
|
|
|
+
|
|
|
+ // delete client files
|
|
|
+ subDuration, _ := time.ParseDuration("-" + strconv.Itoa(backupConf.SaveDays*24) + "h")
|
|
|
+ before := time.Now().Add(subDuration)
|
|
|
|
|
|
- // delete older file when file numbers gt MaxSaveDays
|
|
|
- for _, backupFile := range backupFiles {
|
|
|
- if backupFile.ModTime().Before(ago) {
|
|
|
- filepath := backupConf.GetProjectPath() + "/" + backupFile.Name()
|
|
|
- err := os.Remove(filepath)
|
|
|
- if err != nil {
|
|
|
- log.Printf("删除过期的文件 %s 失败", filepath)
|
|
|
- } else {
|
|
|
- log.Printf("删除过期的文件 %s 成功", filepath)
|
|
|
- }
|
|
|
- }
|
|
|
+ // delete older file when file numbers gt MaxSaveDays
|
|
|
+ for _, backupFile := range backupFiles {
|
|
|
+ if backupFile.ModTime().Before(before) {
|
|
|
+ filepath := backupConf.GetProjectPath() + string(os.PathSeparator) + backupFile.Name()
|
|
|
+ err := os.Remove(filepath)
|
|
|
+ if err != nil {
|
|
|
+ log.Printf("删除过期的文件 %s 失败", filepath)
|
|
|
+ } else {
|
|
|
+ log.Printf("删除过期的文件 %s 成功", filepath)
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
}
|
|
|
- // sleep
|
|
|
- util.SleepForFileDelete()
|
|
|
}
|
|
|
|
|
|
}
|