-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackup_utils.go
68 lines (59 loc) · 1.44 KB
/
backup_utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package main
import (
"archive/tar"
"compress/gzip"
"fmt"
"io"
"os"
"path/filepath"
)
// tarDir creates a tar gz archive from a directory
func tarDir(writer io.Writer, dir string) error {
// create gzip compressed tar writer
gzipWriter := gzip.NewWriter(writer)
defer gzipWriter.Close()
tarWriter := tar.NewWriter(gzipWriter)
defer tarWriter.Close()
return filepath.Walk(dir, func(file string, info os.FileInfo, err error) error {
if err != nil {
return err
}
// handle symlinks
var symLinkTarget string
if info.Mode()&os.ModeSymlink != 0 {
symLinkTarget, err = os.Readlink(file)
if err != nil {
return fmt.Errorf("failed to get symlink target of %s: %w", file, err)
}
}
// generate tar header
header, err := tar.FileInfoHeader(info, symLinkTarget)
if err != nil {
return err
}
// make file path relative
fileRel, err := filepath.Rel(dir, file)
if err != nil {
return fmt.Errorf("failed to get relative path: %w", err)
}
header.Name = filepath.ToSlash(fileRel)
// write tar file entry header
err = tarWriter.WriteHeader(header)
if err != nil {
return err
}
// add content of files
if info.Mode().IsRegular() {
f, err := os.Open(file)
if err != nil {
return fmt.Errorf("failed to open %s: %w", file, err)
}
defer f.Close()
_, err = io.Copy(tarWriter, f)
if err != nil {
return fmt.Errorf("failed add %s to archive: %w", file, err)
}
}
return nil
})
}