-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduced `shouldCleanUp` and `maxBackups` configuration options with corresponding default values.
- Loading branch information
1 parent
ad8e769
commit 3364371
Showing
4 changed files
with
67 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package main | ||
|
||
import ( | ||
"log" | ||
"os" | ||
"path/filepath" | ||
"sort" | ||
) | ||
|
||
func cleanUp() { | ||
files, err := os.ReadDir(pathToBackups) | ||
if err != nil { | ||
log.Println("Failed to read backup directory for clean up") | ||
return | ||
} | ||
|
||
if len(files) > maxBackups { | ||
sort.Slice(files, func(i, j int) bool { | ||
info1, err1 := files[i].Info() | ||
info2, err2 := files[j].Info() | ||
if err1 != nil || err2 != nil { | ||
log.Printf("Error retrieving file info: %v, %v", err1, err2) | ||
return false | ||
} | ||
|
||
return info1.ModTime().Before(info2.ModTime()) | ||
}) | ||
|
||
// The oldest file will now be the first element in the sorted list | ||
oldestFile := files[0] | ||
oldestFilePath := filepath.Join(pathToBackups, oldestFile.Name()) | ||
|
||
// Delete the oldest file | ||
log.Printf("Deleting oldest file: %s\n", oldestFilePath) | ||
err = os.Remove(oldestFilePath) | ||
if err != nil { | ||
log.Fatalf("Failed to delete file %s: %v", oldestFilePath, err) | ||
} | ||
|
||
log.Printf("Oldest file %s has been deleted successfully.\n", oldestFile.Name()) | ||
return | ||
} | ||
|
||
log.Println("Not enough backups to require clean up") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters