From 1d43a0b07b1a9d80edf5a462111c26c31dafa565 Mon Sep 17 00:00:00 2001 From: Jason Cameron Date: Thu, 13 Feb 2025 00:46:15 -0500 Subject: [PATCH] Delete fs directory after compression Delete the `fs` directory after converting to a tarfile to reduce space costs. * Add a function `deleteDirectory` in `handlers/filesystem.go` to delete the `fs` directory after creating the tarfile. * Call `deleteDirectory` in the `Backup` function in `handlers/filesystem.go` after creating the tarfile. * Add a call to `deleteDirectory` in `performBackup` in `sentinel/main.go` after the tarfile is created. * Add a function `deleteDirectory` in `sentinel/main.go` to delete the `fs` directory. --- handlers/filesystem.go | 20 +++++++++++++++++++- sentinel/main.go | 13 +++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/handlers/filesystem.go b/handlers/filesystem.go index a242d8d..d1786cc 100644 --- a/handlers/filesystem.go +++ b/handlers/filesystem.go @@ -158,7 +158,17 @@ func (h *FileSystemHandler) Backup(ctx context.Context) (string, error) { } // Create archive - return h.createArchive() + archivePath, err := h.createArchive() + if err != nil { + return "", err + } + + // Delete the fs directory after creating the tarfile + if err := h.deleteDirectory(); err != nil { + return "", err + } + + return archivePath, nil } // Name returns the handler name @@ -175,3 +185,11 @@ func (h *FileSystemHandler) Cleanup() error { } return nil } + +// deleteDirectory deletes the fs directory +func (h *FileSystemHandler) deleteDirectory() error { + if err := os.RemoveAll(config.Cfg.Filesystem.BasePath); err != nil { + return &ErrFileSystem{Op: "delete directory", Err: err} + } + return nil +} diff --git a/sentinel/main.go b/sentinel/main.go index 178a9f0..224a76b 100644 --- a/sentinel/main.go +++ b/sentinel/main.go @@ -155,6 +155,11 @@ func performBackup(handlerList []handlers.BackupHandler, uploader *storage.S3Upl return err } + // Delete the fs directory after creating the tarfile + if err := deleteDirectory(); err != nil { + return err + } + // Cleanup for _, file := range backupFiles { err := os.Remove(file) @@ -169,3 +174,11 @@ func performBackup(handlerList []handlers.BackupHandler, uploader *storage.S3Upl return nil } + +// deleteDirectory deletes the fs directory +func deleteDirectory() error { + if err := os.RemoveAll(config.Cfg.Filesystem.BasePath); err != nil { + return fmt.Errorf("failed to delete directory: %v", err) + } + return nil +}