diff --git a/internal/backup-vm/handlers/handlers.go b/internal/backup-vm/handlers/handlers.go index f451461..2b5d4b5 100644 --- a/internal/backup-vm/handlers/handlers.go +++ b/internal/backup-vm/handlers/handlers.go @@ -79,6 +79,13 @@ func BackupHandler(w http.ResponseWriter, r *http.Request) { delete := true follow := true + // Persist empty directories by filling them with .keep file + if _, err := s3sync.KeepEmptyDirs(snapPath); err != nil { + log.Errorw("error filling empty directories", "error", err) + http.Error(w, "failed to persist empty directories", http.StatusInternalServerError) + return + } + fmt.Fprintf(w, "Sync snapshot %s into %s\n", createResp.SnapName, bucketPath) syncer := s3sync.New(bucketPath, snapPath, delete, follow) out, err := syncer.Run() diff --git a/pkg/s3sync/sync.go b/pkg/s3sync/sync.go index ed29903..96df855 100644 --- a/pkg/s3sync/sync.go +++ b/pkg/s3sync/sync.go @@ -42,10 +42,24 @@ func (sc SyncCmd) ComposeCmd() string { return strings.TrimSpace(cmd) } +// Run executes composed `aws s3 sync` command. func (sc SyncCmd) Run() ([]byte, error) { + return runCmd(sc.ComposeCmd()) +} + +// KeepEmptyDirs fills empty directories with .keep file. +// This is useful for syncing empty directories in S3. +// S3 is an object storage which does not have a concept +// of the empty directory. +// https://docs.aws.amazon.com/AmazonS3/latest/dev/UsingMetadata.html +func KeepEmptyDirs(path string) ([]byte, error) { + cmd := fmt.Sprintf("find %s -type d -empty -exec touch {}/.keep \\;", path) + return runCmd(cmd) +} + +func runCmd(cmd string) ([]byte, error) { var out []byte - cmd := sc.ComposeCmd() tokens, err := shlex.Split(cmd) if err != nil { return out, err