Fixed bug for date counting
This commit is contained in:
BIN
backup-manager
BIN
backup-manager
Binary file not shown.
22
main.go
22
main.go
@@ -73,8 +73,7 @@ func runCommand(cmdline string) {
|
||||
|
||||
func getNDay(hour int, min int, offset int) time.Time {
|
||||
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, location)
|
||||
nextDate := time.Date(today.Year(), today.Month(), today.Day() + offset, hour, min, 0, 0, today.Location())
|
||||
return nextDate
|
||||
}
|
||||
|
||||
@@ -90,17 +89,22 @@ func getDir(path string, filter string) ([]string, []time.Time) {
|
||||
for _, file := range files {
|
||||
if strings.Contains(file.Name(), filter) {
|
||||
name := file.Name()
|
||||
timeNow := time.Now()
|
||||
workingDateStr := name[len(filter)+1:len(name)]
|
||||
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 {
|
||||
log.Println(err)
|
||||
} else {
|
||||
name = path + "/" + file.Name()
|
||||
names = append(names, name)
|
||||
dates = append(dates, workingDate)
|
||||
log.Printf("%v found\n", workingDate)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
log.Printf("%s: %v\n", path, 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 {
|
||||
for i:=0; i<searchDayNum; i++ {
|
||||
for i:=1; i<=searchDayNum; i++ {
|
||||
for _, date := range(dates) {
|
||||
if currDate.Add(-time.Hour * time.Duration(24 * i)) == date {
|
||||
log.Printf("Existing backup exists at %v\n", date)
|
||||
return true
|
||||
}
|
||||
}
|
||||
@@ -156,20 +161,22 @@ func main() {
|
||||
timeNow := time.Now()
|
||||
timeToday := time.Date(timeNow.Year(), timeNow.Month(), timeNow.Day(), 0, 0, 0, 0, timeNow.Location())
|
||||
log.Printf("New Date: %v", timeToday)
|
||||
var workingTime time.Time
|
||||
for _, host := range config.Hosts {
|
||||
log.Printf("New Host: %s", host.Name)
|
||||
// creating backup for today
|
||||
backupToday(timeToday, config, host)
|
||||
|
||||
// go through each interval for deletion
|
||||
workingTime := timeToday
|
||||
workingTime = timeToday
|
||||
filePaths, fileDates := getDir(config.WorkingDir, host.Name)
|
||||
for i, backupPeriod := range(config.BackupPeriods) {
|
||||
// get to the time
|
||||
if err != nil {
|
||||
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
|
||||
workingFileCount := findBackupDate(fileDates, workingTime)
|
||||
@@ -177,7 +184,7 @@ func main() {
|
||||
// file exist...
|
||||
if workingFileCount != 0 {
|
||||
// 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
|
||||
removeCommand := fmt.Sprintf("rm -rf \"%s\"", filePaths[workingFileCount])
|
||||
runCommand(removeCommand)
|
||||
@@ -185,9 +192,10 @@ func main() {
|
||||
}
|
||||
}
|
||||
|
||||
log.Printf("Final working time: %v", workingTime)
|
||||
// delete the rest
|
||||
for i, date := range(fileDates) {
|
||||
if date.After(workingTime) {
|
||||
if date.Before(workingTime) {
|
||||
removeCommand := fmt.Sprintf("rm -rf \"%s\"", filePaths[i])
|
||||
runCommand(removeCommand)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user