Fixed bug for date counting

This commit is contained in:
Samuel Pua
2019-08-26 16:25:31 +08:00
parent de307873b2
commit 447f8ee5b7
2 changed files with 15 additions and 7 deletions

Binary file not shown.

22
main.go
View File

@@ -73,8 +73,7 @@ func runCommand(cmdline string) {
func getNDay(hour int, min int, offset int) time.Time { func getNDay(hour int, min int, offset int) time.Time {
today := time.Now() today := time.Now()
location := time.FixedZone("UTC+8", 8*60*60) nextDate := time.Date(today.Year(), today.Month(), today.Day() + offset, hour, min, 0, 0, today.Location())
nextDate := time.Date(today.Year(), today.Month(), today.Day() + offset, hour, min, 0, 0, location)
return nextDate return nextDate
} }
@@ -90,17 +89,22 @@ func getDir(path string, filter string) ([]string, []time.Time) {
for _, file := range files { for _, file := range files {
if strings.Contains(file.Name(), filter) { if strings.Contains(file.Name(), filter) {
name := file.Name() name := file.Name()
timeNow := time.Now()
workingDateStr := name[len(filter)+1:len(name)] workingDateStr := name[len(filter)+1:len(name)]
workingDate, err := time.Parse("2006-01-02", workingDateStr) workingDate, err := time.Parse("2006-01-02", workingDateStr)
workingDate = time.Date(workingDate.Year(), workingDate.Month(), workingDate.Day(), 0, 0, 0, 0, timeNow.Location())
if err!= nil { if err!= nil {
log.Println(err) log.Println(err)
} else { } else {
name = path + "/" + file.Name()
names = append(names, name) names = append(names, name)
dates = append(dates, workingDate) dates = append(dates, workingDate)
log.Printf("%v found\n", workingDate)
} }
} }
} }
log.Printf("%s: %v\n", path, dates)
return names, dates return names, dates
} }
@@ -128,9 +132,10 @@ func findBackupDate(dates []time.Time, findDate time.Time) int {
} }
func enumerateDates(dates []time.Time, currDate time.Time, searchDayNum int) bool { func enumerateDates(dates []time.Time, currDate time.Time, searchDayNum int) bool {
for i:=0; i<searchDayNum; i++ { for i:=1; i<=searchDayNum; i++ {
for _, date := range(dates) { for _, date := range(dates) {
if currDate.Add(-time.Hour * time.Duration(24 * i)) == date { if currDate.Add(-time.Hour * time.Duration(24 * i)) == date {
log.Printf("Existing backup exists at %v\n", date)
return true return true
} }
} }
@@ -156,20 +161,22 @@ func main() {
timeNow := time.Now() timeNow := time.Now()
timeToday := time.Date(timeNow.Year(), timeNow.Month(), timeNow.Day(), 0, 0, 0, 0, timeNow.Location()) timeToday := time.Date(timeNow.Year(), timeNow.Month(), timeNow.Day(), 0, 0, 0, 0, timeNow.Location())
log.Printf("New Date: %v", timeToday) log.Printf("New Date: %v", timeToday)
var workingTime time.Time
for _, host := range config.Hosts { for _, host := range config.Hosts {
log.Printf("New Host: %s", host.Name) log.Printf("New Host: %s", host.Name)
// creating backup for today // creating backup for today
backupToday(timeToday, config, host) backupToday(timeToday, config, host)
// go through each interval for deletion // go through each interval for deletion
workingTime := timeToday workingTime = timeToday
filePaths, fileDates := getDir(config.WorkingDir, host.Name) filePaths, fileDates := getDir(config.WorkingDir, host.Name)
for i, backupPeriod := range(config.BackupPeriods) { for i, backupPeriod := range(config.BackupPeriods) {
// get to the time // get to the time
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
} }
workingTime = workingTime.Add(-time.Duration(backupPeriod.Interval) * 24 * time.Hour) workingTime = workingTime.Add(-time.Duration(backupPeriod.Interval * backupPeriod.Count) * 24 * time.Hour)
log.Printf("Working time: %v\n", workingTime)
// check if file exists // check if file exists
workingFileCount := findBackupDate(fileDates, workingTime) workingFileCount := findBackupDate(fileDates, workingTime)
@@ -177,7 +184,7 @@ func main() {
// file exist... // file exist...
if workingFileCount != 0 { if workingFileCount != 0 {
// if there's a copy out there within interval... // if there's a copy out there within interval...
if i == len(config.BackupPeriods) || enumerateDates(fileDates, workingTime, config.BackupPeriods[i+1].Interval) { if i+1 == len(config.BackupPeriods) || enumerateDates(fileDates, workingTime, config.BackupPeriods[i+1].Interval) {
// delete it // delete it
removeCommand := fmt.Sprintf("rm -rf \"%s\"", filePaths[workingFileCount]) removeCommand := fmt.Sprintf("rm -rf \"%s\"", filePaths[workingFileCount])
runCommand(removeCommand) runCommand(removeCommand)
@@ -185,9 +192,10 @@ func main() {
} }
} }
log.Printf("Final working time: %v", workingTime)
// delete the rest // delete the rest
for i, date := range(fileDates) { for i, date := range(fileDates) {
if date.After(workingTime) { if date.Before(workingTime) {
removeCommand := fmt.Sprintf("rm -rf \"%s\"", filePaths[i]) removeCommand := fmt.Sprintf("rm -rf \"%s\"", filePaths[i])
runCommand(removeCommand) runCommand(removeCommand)
} }