Skip to content

Commit

Permalink
Persist empty directories by filling them with .keep file
Browse files Browse the repository at this point in the history
  • Loading branch information
Pavel Dmytrenko committed Sep 11, 2019
1 parent aa4c508 commit 19c6105
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
7 changes: 7 additions & 0 deletions internal/backup-vm/handlers/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
16 changes: 15 additions & 1 deletion pkg/s3sync/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 19c6105

Please sign in to comment.