Skip to content

Commit

Permalink
APPS-974 Fix backup size computation (#43)
Browse files Browse the repository at this point in the history
* APPS-974 Fix backup size computation

* update path for recursive calls

* add BackupDetails.Size for S3 full backup
  • Loading branch information
reugn authored Nov 5, 2023
1 parent 18edc53 commit 872df85
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 5 deletions.
31 changes: 27 additions & 4 deletions pkg/service/backup_backend_local.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"io/fs"
"os"
"path/filepath"

"log/slog"

Expand Down Expand Up @@ -91,7 +92,7 @@ func (local *BackupBackendLocal) FullBackupList() ([]model.BackupDetails, error)
backupDetails := make([]model.BackupDetails, 0)
for _, e := range entries {
if e.IsDir() {
backupDetails = append(backupDetails, toBackupDetails(e, backupFolder+"/"))
backupDetails = append(backupDetails, toBackupDetails(e, backupFolder))
}
}
return backupDetails, nil
Expand All @@ -108,7 +109,7 @@ func (local *BackupBackendLocal) IncrementalBackupList() ([]model.BackupDetails,
backupDetails := make([]model.BackupDetails, 0)
for _, e := range entries {
if !e.IsDir() {
backupDetails = append(backupDetails, toBackupDetails(e, backupFolder+"/"))
backupDetails = append(backupDetails, toBackupDetails(e, backupFolder))
}
}
return backupDetails, nil
Expand Down Expand Up @@ -138,12 +139,34 @@ func (local *BackupBackendLocal) BackupPolicyName() string {

func toBackupDetails(e fs.DirEntry, prefix string) model.BackupDetails {
details := model.BackupDetails{
Key: util.Ptr(prefix + e.Name()),
Key: util.Ptr(filepath.Join(prefix, e.Name())),
}
dirInfo, err := e.Info()
if err == nil {
details.LastModified = util.Ptr(dirInfo.ModTime())
details.Size = util.Ptr(dirInfo.Size())
details.Size = util.Ptr(dirEntrySize(prefix, e, dirInfo))
}
return details
}

func dirEntrySize(path string, e fs.DirEntry, info fs.FileInfo) int64 {
if e.IsDir() {
var totalSize int64
path = filepath.Join(path, e.Name())
entries, err := os.ReadDir(path)
if err == nil {
for _, dirEntry := range entries {
dirInfo, err := dirEntry.Info()
if err == nil {
if dirEntry.IsDir() {
totalSize += dirEntrySize(path, dirEntry, dirInfo)
} else {
totalSize += dirInfo.Size()
}
}
}
}
return totalSize
}
return info.Size()
}
17 changes: 17 additions & 0 deletions pkg/service/backup_backend_s3.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package service

import (
"log/slog"

"github.com/aerospike/backup/pkg/model"
"github.com/aws/smithy-go/ptr"
)
Expand Down Expand Up @@ -47,6 +49,7 @@ func (s *BackupBackendS3) FullBackupList() ([]model.BackupDetails, error) {
return []model.BackupDetails{{
Key: ptr.String(s3prefix + backupFolder),
LastModified: &s.readState().LastRun,
Size: ptr.Int64(s.dirSize(backupFolder)),
}}, nil
}
return []model.BackupDetails{}, nil
Expand All @@ -61,12 +64,26 @@ func (s *BackupBackendS3) FullBackupList() ([]model.BackupDetails, error) {
details := model.BackupDetails{
Key: ptr.String(s3prefix + "/" + *subfolder.Prefix),
LastModified: s.GetTime(subfolder),
Size: ptr.Int64(s.dirSize(*subfolder.Prefix)),
}
contents[i] = details
}
return contents, err
}

func (s *BackupBackendS3) dirSize(path string) int64 {
files, err := s.listFiles(path)
if err != nil {
slog.Warn("Failed to list files", "path", path)
return 0
}
var totalSize int64
for _, file := range files {
totalSize += file.Size
}
return totalSize
}

// IncrementalBackupList returns a list of available incremental backups.
func (s *BackupBackendS3) IncrementalBackupList() ([]model.BackupDetails, error) {
list, err := s.listFiles(s.Path + "/" + model.IncrementalBackupDirectory)
Expand Down
2 changes: 1 addition & 1 deletion pkg/shared/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func (b *BackupShared) BackupRun(backupPolicy *model.BackupPolicy, cluster *mode
backupStatus := C.backup_run(&backupConfig)

var success bool
if unsafe.Pointer(backupStatus) == C.RUN_BACKUP_SUCCESS {
if unsafe.Pointer(backupStatus) == C.RUN_BACKUP_SUCCESS { //nolint:gocritic
success = true
} else if unsafe.Pointer(backupStatus) != C.RUN_BACKUP_FAILURE {
C.backup_status_destroy(backupStatus)
Expand Down

0 comments on commit 872df85

Please sign in to comment.