Skip to content

Commit

Permalink
Add cmd specifier and remove zlib compression
Browse files Browse the repository at this point in the history
Signed-off-by: Jason Cameron <[email protected]>
  • Loading branch information
JasonLovesDoggo committed Nov 3, 2024
1 parent 7fe2c6e commit 2b5e0e4
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 42 deletions.
96 changes: 55 additions & 41 deletions compression/compressor.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package compression

import (
"compress/gzip"
"compress/zlib"
"fmt"
"github.com/klauspost/compress/zstd"
"io"
Expand All @@ -11,8 +10,8 @@ import (

func Ext(format string) string {
switch format {
case "zlib":
return ".zlib"
//case "zlib":
// return ".zlib"
case "gzip":
return ".gz"
case "zstd":
Expand All @@ -24,61 +23,68 @@ func Ext(format string) string {

type Compressor interface {
Compress(src []string, dst string) error
Cmd() string
}

type ZlibCompressor struct {
level int // 1-9 see https://pkg.go.dev/compress/flate#pkg-constants
}
//type ZlibCompressor struct {
// level int // 1-9 see https://pkg.go.dev/compress/flate#pkg-constants (with -1 being the default and 0 being no compression)
//}

type ZstdCompressor struct {
level int // see https://pkg.go.dev/github.com/klauspost/compress/zstd#EncoderLevel
}
type GzipCompressor struct{}
type GzipCompressor struct {
level int // 1-9
}

func NewCompressor(format string, level int) (Compressor, error) {
switch format {
case "zlib":
return &ZlibCompressor{level: level}, nil
//case "zlib":
// return &ZlibCompressor{level: level}, nil
case "gzip":
return &GzipCompressor{}, nil
return &GzipCompressor{level: level}, nil
case "zstd":
return &ZstdCompressor{level: level}, nil
default:
return nil, fmt.Errorf("unsupported compression format: %s", format)
}
}

func (c *ZlibCompressor) Compress(src []string, dst string) error {
f, err := os.Create(dst)
if err != nil {
return err
}
defer f.Close()

zw, err := zlib.NewWriterLevel(f, c.level)
if err != nil {
return err
}
defer zw.Close()

for _, file := range src {
f, err := os.Open(file)
if err != nil {
return err
}
defer f.Close()

_, err = io.Copy(zw, f)
if err != nil {
return err
}
}
err = zw.Flush()
if err != nil {
return err
}
return nil
}
//func (c *ZlibCompressor) Compress(src []string, dst string) error {
// f, err := os.Create(dst)
// if err != nil {
// return err
// }
// defer f.Close()
//
// zw, err := zlib.NewWriterLevel(f, c.level)
// if err != nil {
// return err
// }
// defer zw.Close()
//
// for _, file := range src {
// f, err := os.Open(file)
// if err != nil {
// return err
// }
// defer f.Close()
//
// _, err = io.Copy(zw, f)
// if err != nil {
// return err
// }
// }
// err = zw.Flush()
// if err != nil {
// return err
// }
// return nil
//}
//
//func (c *ZlibCompressor) Cmd() string {
// return fmt.Sprintf("zlib -%d", c.level)
//}

func (c *GzipCompressor) Compress(src []string, dst string) error {
f, err := os.Create(dst)
Expand All @@ -105,6 +111,10 @@ func (c *GzipCompressor) Compress(src []string, dst string) error {
return nil
}

func (c *GzipCompressor) Cmd() string {
return fmt.Sprintf("gzip -%d", c.level)
}

func (c *ZstdCompressor) Compress(src []string, dst string) error {
f, err := os.Create(dst)
if err != nil {
Expand Down Expand Up @@ -132,3 +142,7 @@ func (c *ZstdCompressor) Compress(src []string, dst string) error {
}
return nil
}

func (c *ZstdCompressor) Cmd() string {
return fmt.Sprintf("zstd -%d", c.level)
}
3 changes: 2 additions & 1 deletion sentinel/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,9 @@ func performBackup(handlerList []handlers.BackupHandler, uploader *storage.S3Upl
filename := filepath.Join(config.Cfg.TempDir, fmt.Sprintf("backup.tar%s", compression.Ext(config.Cfg.Compression.Format)))
FileLocations := strings.Join(backupFiles, " ")
fmt.Printf("Compressing backups: %s\n", FileLocations)
compressor, _ := compression.NewCompressor(config.Cfg.Compression.Format, config.Cfg.Compression.Level)
cmd := exec.Command("tar",
"-I", config.Cfg.Compression.Format,
fmt.Sprintf("-I %s", compressor.Cmd()),
"-cf", filename, FileLocations)
fmt.Printf("Running command: %s\n", cmd.String())
if err := cmd.Run(); err != nil {
Expand Down

0 comments on commit 2b5e0e4

Please sign in to comment.