Skip to content

Commit

Permalink
Merge pull request #292 from ponzu-cms/ponzu-dev
Browse files Browse the repository at this point in the history
backup: follow symlinks when archiving
  • Loading branch information
nilslice authored Jan 1, 2019
2 parents 222b13a + f91b429 commit 584a064
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
3 changes: 3 additions & 0 deletions system/admin/upload/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ func Backup(ctx context.Context, res http.ResponseWriter) error {
}

err = backup.ArchiveFS(ctx, "uploads", f)
if err != nil {
return err
}

err = f.Close()
if err != nil {
Expand Down
21 changes: 20 additions & 1 deletion system/backup/archive.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,25 @@ func ArchiveFS(ctx context.Context, basedir string, w io.Writer) error {
gz := gzip.NewWriter(w)
tarball := tar.NewWriter(gz)

absPath, err := filepath.Abs(basedir)
if err != nil {
return err
}

info, err := os.Lstat(absPath)
if err != nil {
return err
}

if info.Mode()&os.ModeSymlink == os.ModeSymlink {
// This is a symlink - we need to follow it
bdir, err := os.Readlink(absPath)
if err != nil {
return err
}
basedir = bdir
}

errChan := make(chan error, 1)
walkFn := func(path string, info os.FileInfo, err error) error {
if err != nil {
Expand Down Expand Up @@ -61,7 +80,7 @@ func ArchiveFS(ctx context.Context, basedir string, w io.Writer) error {
}

// stop processing if we get a cancellation signal
err := filepath.Walk(basedir, func(path string, info os.FileInfo, err error) error {
err = filepath.Walk(basedir, func(path string, info os.FileInfo, err error) error {
go func() { errChan <- walkFn(path, info, err) }()

select {
Expand Down
5 changes: 4 additions & 1 deletion system/search/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ func Backup(ctx context.Context, res http.ResponseWriter) error {
return err
}

backup.ArchiveFS(ctx, "search", f)
err = backup.ArchiveFS(ctx, "search", f)
if err != nil {
return err
}

err = f.Close()
if err != nil {
Expand Down

0 comments on commit 584a064

Please sign in to comment.