From 872df8549d5ac9502942809c0469cffb29130b31 Mon Sep 17 00:00:00 2001 From: Eugene R Date: Sun, 5 Nov 2023 11:49:43 +0200 Subject: [PATCH] APPS-974 Fix backup size computation (#43) * APPS-974 Fix backup size computation * update path for recursive calls * add BackupDetails.Size for S3 full backup --- pkg/service/backup_backend_local.go | 31 +++++++++++++++++++++++++---- pkg/service/backup_backend_s3.go | 17 ++++++++++++++++ pkg/shared/backup.go | 2 +- 3 files changed, 45 insertions(+), 5 deletions(-) diff --git a/pkg/service/backup_backend_local.go b/pkg/service/backup_backend_local.go index 27e4a82f..f2ee09f8 100644 --- a/pkg/service/backup_backend_local.go +++ b/pkg/service/backup_backend_local.go @@ -4,6 +4,7 @@ import ( "fmt" "io/fs" "os" + "path/filepath" "log/slog" @@ -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 @@ -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 @@ -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() +} diff --git a/pkg/service/backup_backend_s3.go b/pkg/service/backup_backend_s3.go index 4a53892d..d4b22c53 100644 --- a/pkg/service/backup_backend_s3.go +++ b/pkg/service/backup_backend_s3.go @@ -1,6 +1,8 @@ package service import ( + "log/slog" + "github.com/aerospike/backup/pkg/model" "github.com/aws/smithy-go/ptr" ) @@ -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 @@ -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) diff --git a/pkg/shared/backup.go b/pkg/shared/backup.go index a68a0e06..fe1c9619 100644 --- a/pkg/shared/backup.go +++ b/pkg/shared/backup.go @@ -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)