-
Notifications
You must be signed in to change notification settings - Fork 385
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move file system archive code into backup package
- Loading branch information
Showing
2 changed files
with
102 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package backup | ||
|
||
import ( | ||
"archive/tar" | ||
"compress/gzip" | ||
"context" | ||
"fmt" | ||
"io" | ||
"os" | ||
"path/filepath" | ||
) | ||
|
||
// ArchiveFS walks the filesystem starting from basedir writing files encountered | ||
// tarred and gzipped to the provided writer | ||
func ArchiveFS(ctx context.Context, basedir string, w io.Writer) error { | ||
gz := gzip.NewWriter(w) | ||
tarball := tar.NewWriter(gz) | ||
|
||
errChan := make(chan error, 1) | ||
walkFn := func(path string, info os.FileInfo, err error) error { | ||
if err != nil { | ||
return err | ||
} | ||
|
||
hdr, err := tar.FileInfoHeader(info, "") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
hdr.Name = path | ||
|
||
err = tarball.WriteHeader(hdr) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if !info.IsDir() { | ||
src, err := os.Open(path) | ||
if err != nil { | ||
return err | ||
} | ||
defer src.Close() | ||
|
||
_, err = io.Copy(tarball, src) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = tarball.Flush() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = gz.Flush() | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// stop processing if we get a cancellation signal | ||
err := filepath.Walk(basedir, func(path string, info os.FileInfo, err error) error { | ||
go func() { errChan <- walkFn(path, info, err) }() | ||
|
||
select { | ||
case <-ctx.Done(): | ||
if err := ctx.Err(); err != nil { | ||
return err | ||
} | ||
case err := <-errChan: | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
}) | ||
if err != nil { | ||
fmt.Println(err) | ||
return err | ||
} | ||
|
||
err = gz.Close() | ||
if err != nil { | ||
return err | ||
} | ||
err = tarball.Close() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |