Skip to content

Commit

Permalink
fix(#34): Skip backup rotation for week and month backups
Browse files Browse the repository at this point in the history
(cherry picked from commit 23943ff)
  • Loading branch information
randreev1321 committed Apr 4, 2024
1 parent 1ae41f7 commit 0c6906f
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 64 deletions.
30 changes: 18 additions & 12 deletions modules/storage/ftp/ftp.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,30 +171,36 @@ func (f *FTP) deleteDescBackup(logCh chan logger.LogRecord, job, ofsPart string,
var retentionDate time.Time
retentionCount := 0

bakDir := path.Join(f.backupPath, ofsPart, period)
files, err := f.conn.List(bakDir)
if err != nil {
var protoErr *textproto.Error
errors.As(err, &protoErr)
if protoErr.Code == 550 {
continue
}
logCh <- logger.Log(job, f.name).Errorf("Failed to read files in remote directory '%s' with next error: %s", bakDir, err)
return err
}

switch period {
case "daily":
retentionCount = f.Retention.Days
retentionDate = curDate.AddDate(0, 0, -f.Retention.Days)
case "weekly":
if misc.GetDateTimeNow("dow") != misc.WeeklyBackupDay {
continue
}
retentionCount = f.Retention.Weeks
retentionDate = curDate.AddDate(0, 0, -f.Retention.Weeks*7)
case "monthly":
if misc.GetDateTimeNow("dom") != misc.MonthlyBackupDay {
continue
}
retentionCount = f.Retention.Months
retentionDate = curDate.AddDate(0, -f.Retention.Months, 0)
}

bakDir := path.Join(f.backupPath, ofsPart, period)
files, err := f.conn.List(bakDir)
if err != nil {
var protoErr *textproto.Error
errors.As(err, &protoErr)
if protoErr.Code == 550 {
continue
}
logCh <- logger.Log(job, f.name).Errorf("Failed to read files in remote directory '%s' with next error: %s", bakDir, err)
return err
}

if f.Retention.UseCount {
sort.Slice(files, func(i, j int) bool {
return files[i].Time.Before(files[j].Time)
Expand Down
30 changes: 18 additions & 12 deletions modules/storage/local/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,24 @@ func (l *Local) deleteDescBackup(logCh chan logger.LogRecord, jobName, ofsPart s
var retentionDate time.Time
retentionCount := 0

switch period {
case "daily":
retentionCount = l.Retention.Days
retentionDate = curDate.AddDate(0, 0, -l.Retention.Days)
case "weekly":
if misc.GetDateTimeNow("dow") != misc.WeeklyBackupDay {
continue
}
retentionCount = l.Retention.Weeks
retentionDate = curDate.AddDate(0, 0, -l.Retention.Weeks*7)
case "monthly":
if misc.GetDateTimeNow("dom") != misc.MonthlyBackupDay {
continue
}
retentionCount = l.Retention.Months
retentionDate = curDate.AddDate(0, -l.Retention.Months, 0)
}

bakDir := path.Join(l.backupPath, ofsPart, period)

dir, err := os.Open(bakDir)
Expand All @@ -182,18 +200,6 @@ func (l *Local) deleteDescBackup(logCh chan logger.LogRecord, jobName, ofsPart s
return err
}

switch period {
case "daily":
retentionCount = l.Retention.Days
retentionDate = curDate.AddDate(0, 0, -l.Retention.Days)
case "weekly":
retentionCount = l.Retention.Weeks
retentionDate = curDate.AddDate(0, 0, -l.Retention.Weeks*7)
case "monthly":
retentionCount = l.Retention.Months
retentionDate = curDate.AddDate(0, -l.Retention.Months, 0)
}

if l.Retention.UseCount {
sort.Slice(files, func(i, j int) bool {
iInfo, _ := files[i].Info()
Expand Down
26 changes: 16 additions & 10 deletions modules/storage/nfs/nfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,28 +167,34 @@ func (n *NFS) deleteDescBackup(logCh chan logger.LogRecord, jobName, ofsPart str
var retentionDate time.Time
retentionCount := 0

bakDir := path.Join(n.backupPath, ofsPart, period)
files, err := n.target.ReadDirPlus(bakDir)
if err != nil {
if os.IsNotExist(err) {
continue
}
logCh <- logger.Log(jobName, n.name).Errorf("Failed to read files in remote directory '%s' with next error: %s", bakDir, err)
return err
}

switch period {
case "daily":
retentionCount = n.Retention.Days
retentionDate = curDate.AddDate(0, 0, -n.Retention.Days)
case "weekly":
if misc.GetDateTimeNow("dow") != misc.WeeklyBackupDay {
continue
}
retentionCount = n.Retention.Weeks
retentionDate = curDate.AddDate(0, 0, -n.Retention.Weeks*7)
case "monthly":
if misc.GetDateTimeNow("dom") != misc.MonthlyBackupDay {
continue
}
retentionCount = n.Retention.Months
retentionDate = curDate.AddDate(0, -n.Retention.Months, 0)
}

bakDir := path.Join(n.backupPath, ofsPart, period)
files, err := n.target.ReadDirPlus(bakDir)
if err != nil {
if os.IsNotExist(err) {
continue
}
logCh <- logger.Log(jobName, n.name).Errorf("Failed to read files in remote directory '%s' with next error: %s", bakDir, err)
return err
}

if n.Retention.UseCount {
sort.Slice(files, func(i, j int) bool {
return files[i].ModTime().Before(files[j].ModTime())
Expand Down
6 changes: 6 additions & 0 deletions modules/storage/s3/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,11 +179,17 @@ func (s *s3) DeleteOldBackups(logCh chan logger.LogRecord, ofs string, job inter
}
}
if strings.Contains(object.Key, "weekly") {
if misc.GetDateTimeNow("dow") != misc.WeeklyBackupDay {
continue
}
if s.Retention.UseCount || object.LastModified.Before(curDate.AddDate(0, 0, -s.Retention.Weeks*7)) {
filesList["weekly"] = append(filesList["weekly"], object)
}
}
if strings.Contains(object.Key, "monthly") {
if misc.GetDateTimeNow("dom") != misc.MonthlyBackupDay {
continue
}
if s.Retention.UseCount || object.LastModified.Before(curDate.AddDate(0, -s.Retention.Months, 0)) {
filesList["monthly"] = append(filesList["monthly"], object)
}
Expand Down
26 changes: 16 additions & 10 deletions modules/storage/sftp/sftp.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,28 +212,34 @@ func (s *SFTP) deleteDescBackup(logCh chan logger.LogRecord, jobName, ofsPart st
var retentionDate time.Time
retentionCount := 0

bakDir := path.Join(s.backupPath, ofsPart, period)
files, err := s.client.ReadDir(bakDir)
if err != nil {
if errors.Is(err, fs.ErrNotExist) {
continue
}
logCh <- logger.Log(jobName, s.name).Errorf("Failed to read files in remote directory '%s' with next error: %s", bakDir, err)
return err
}

switch period {
case "daily":
retentionCount = s.Retention.Days
retentionDate = curDate.AddDate(0, 0, -s.Retention.Days)
case "weekly":
if misc.GetDateTimeNow("dow") != misc.WeeklyBackupDay {
continue
}
retentionCount = s.Retention.Weeks
retentionDate = curDate.AddDate(0, 0, -s.Retention.Weeks*7)
case "monthly":
if misc.GetDateTimeNow("dom") != misc.MonthlyBackupDay {
continue
}
retentionCount = s.Retention.Months
retentionDate = curDate.AddDate(0, -s.Retention.Months, 0)
}

bakDir := path.Join(s.backupPath, ofsPart, period)
files, err := s.client.ReadDir(bakDir)
if err != nil {
if errors.Is(err, fs.ErrNotExist) {
continue
}
logCh <- logger.Log(jobName, s.name).Errorf("Failed to read files in remote directory '%s' with next error: %s", bakDir, err)
return err
}

if s.Retention.UseCount {
sort.Slice(files, func(i, j int) bool {
return files[i].ModTime().Before(files[j].ModTime())
Expand Down
26 changes: 16 additions & 10 deletions modules/storage/smb/smb.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,28 +181,34 @@ func (s *SMB) deleteDescBackup(logCh chan logger.LogRecord, jobName, ofsPart str
var retentionDate time.Time
retentionCount := 0

bakDir := path.Join(s.backupPath, ofsPart, period)
files, err := s.share.ReadDir(bakDir)
if err != nil {
if os.IsNotExist(err) {
continue
}
logCh <- logger.Log(jobName, s.name).Errorf("Failed to read files in remote directory '%s' with next error: %s", bakDir, err)
return err
}

switch period {
case "daily":
retentionCount = s.Retention.Days
retentionDate = curDate.AddDate(0, 0, -s.Retention.Days)
case "weekly":
if misc.GetDateTimeNow("dow") != misc.WeeklyBackupDay {
continue
}
retentionCount = s.Retention.Weeks
retentionDate = curDate.AddDate(0, 0, -s.Retention.Weeks*7)
case "monthly":
if misc.GetDateTimeNow("dom") != misc.MonthlyBackupDay {
continue
}
retentionCount = s.Retention.Months
retentionDate = curDate.AddDate(0, -s.Retention.Months, 0)
}

bakDir := path.Join(s.backupPath, ofsPart, period)
files, err := s.share.ReadDir(bakDir)
if err != nil {
if os.IsNotExist(err) {
continue
}
logCh <- logger.Log(jobName, s.name).Errorf("Failed to read files in remote directory '%s' with next error: %s", bakDir, err)
return err
}

if s.Retention.UseCount {
sort.Slice(files, func(i, j int) bool {
return files[i].ModTime().Before(files[j].ModTime())
Expand Down
26 changes: 16 additions & 10 deletions modules/storage/webdav/wedav.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,28 +157,34 @@ func (wd *webDav) deleteDescBackup(logCh chan logger.LogRecord, jobName, ofsPart
var retentionDate time.Time
retentionCount := 0

bakDir := path.Join(wd.backupPath, ofsPart, period)
files, err := wd.client.Ls(bakDir)
if err != nil {
if errors.Is(err, fs.ErrNotExist) {
continue
}
logCh <- logger.Log(jobName, wd.name).Errorf("Failed to read files in remote directory '%s' with next error: %s", bakDir, err)
return err
}

switch period {
case "daily":
retentionCount = wd.Retention.Days
retentionDate = curDate.AddDate(0, 0, -wd.Retention.Days)
case "weekly":
if misc.GetDateTimeNow("dow") != misc.WeeklyBackupDay {
continue
}
retentionCount = wd.Retention.Weeks
retentionDate = curDate.AddDate(0, 0, -wd.Retention.Weeks*7)
case "monthly":
if misc.GetDateTimeNow("dom") != misc.MonthlyBackupDay {
continue
}
retentionCount = wd.Retention.Months
retentionDate = curDate.AddDate(0, -wd.Retention.Months, 0)
}

bakDir := path.Join(wd.backupPath, ofsPart, period)
files, err := wd.client.Ls(bakDir)
if err != nil {
if errors.Is(err, fs.ErrNotExist) {
continue
}
logCh <- logger.Log(jobName, wd.name).Errorf("Failed to read files in remote directory '%s' with next error: %s", bakDir, err)
return err
}

if wd.Retention.UseCount {
sort.Slice(files, func(i, j int) bool {
return files[i].ModTime().Before(files[j].ModTime())
Expand Down

0 comments on commit 0c6906f

Please sign in to comment.