From e9ccaac9621a3f87c8e497c389be0fdccbe4bb95 Mon Sep 17 00:00:00 2001 From: ostempel Date: Tue, 3 Dec 2024 13:51:58 +0100 Subject: [PATCH] fix decrompression of archives --- cmd/internal/compress/compress.go | 45 ++++++++++++++++++++++++++----- cmd/main.go | 36 +++++++++++++++---------- 2 files changed, 60 insertions(+), 21 deletions(-) diff --git a/cmd/internal/compress/compress.go b/cmd/internal/compress/compress.go index f85e965..c82a387 100644 --- a/cmd/internal/compress/compress.go +++ b/cmd/internal/compress/compress.go @@ -7,35 +7,54 @@ import ( "github.com/metal-stack/backup-restore-sidecar/pkg/constants" "github.com/mholt/archives" + "github.com/spf13/afero" ) type Compressor struct { + fs afero.Fs compressor *archives.CompressedArchive extension string } +type CompressorConfig struct { + Method string + FS afero.Fs +} + // New Returns a new Compressor -func New(method string) (*Compressor, error) { +func New(config *CompressorConfig) (*Compressor, error) { + + if config.FS == nil { + config.FS = afero.NewOsFs() + } + c := &archives.CompressedArchive{} - switch method { + switch config.Method { case "tar": c = &archives.CompressedArchive{ - Archival: archives.Tar{}, + Archival: archives.Tar{}, + Extraction: archives.Tar{}, } case "targz": c = &archives.CompressedArchive{ Compression: archives.Gz{}, + Extraction: archives.Tar{}, Archival: archives.Tar{}, } case "tarlz4": c = &archives.CompressedArchive{ Compression: archives.Lz4{}, + Extraction: archives.Tar{}, Archival: archives.Tar{}, } default: - return nil, fmt.Errorf("unsupported compression method: %s", method) + return nil, fmt.Errorf("unsupported compression method: %s", config.Method) } - return &Compressor{compressor: c, extension: "." + method}, nil + return &Compressor{ + compressor: c, + extension: "." + config.Method, + fs: config.FS, + }, nil } // Compress the given backupFile and returns the full filename with the extension @@ -57,8 +76,20 @@ func (c *Compressor) Compress(ctx context.Context, backupFilePath string, output // Decompress the given backupFile func (c *Compressor) Decompress(ctx context.Context, inputReader io.Reader) error { err := c.compressor.Extract(ctx, inputReader, func(ctx context.Context, f archives.FileInfo) error { - // do something with the file here; or, if you only want a specific file or directory, - // just return until you come across the desired f.NameInArchive value(s) + // open archive file + file, err := f.Open() + if err != nil { + return err + } + defer file.Close() + // create file in restore directory + outputFile, err := c.fs.Create(constants.RestoreDir + "/" + f.Name()) + if err != nil { + return err + } + defer outputFile.Close() + // copy file content + io.Copy(outputFile, file) return nil }) return err diff --git a/cmd/main.go b/cmd/main.go index c1b8a89..eed9782 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -103,12 +103,13 @@ const ( ) var ( - cfgFile string - logger *slog.Logger - db database.Database - bp providers.BackupProvider - encrypter *encryption.Encrypter - stop context.Context + cfgFile string + logger *slog.Logger + db database.Database + bp providers.BackupProvider + encrypter *encryption.Encrypter + compressor *compress.Compressor + stop context.Context ) var rootCmd = &cobra.Command{ @@ -135,6 +136,9 @@ var startCmd = &cobra.Command{ if err := initEncrypter(); err != nil { return err } + if err := initCompressor(); err != nil { + return err + } return initBackupProvider() }, RunE: func(cmd *cobra.Command, args []string) error { @@ -153,12 +157,6 @@ var startCmd = &cobra.Command{ logger.Info("starting backup-restore-sidecar", "version", v.V, "bind-addr", addr) - comp, err := compress.New(viper.GetString(compressionMethod)) - logger.Info(comp.Extension()) - if err != nil { - return err - } - metrics := metrics.New() metrics.Start(logger.WithGroup("metrics")) @@ -168,11 +166,11 @@ var startCmd = &cobra.Command{ DatabaseProber: db, BackupProvider: bp, Metrics: metrics, - Compressor: comp, + Compressor: compressor, Encrypter: encrypter, }) - if err := initializer.New(logger.WithGroup("initializer"), addr, db, bp, comp, metrics, viper.GetString(databaseDatadirFlg), encrypter).Start(stop, backuper); err != nil { + if err := initializer.New(logger.WithGroup("initializer"), addr, db, bp, compressor, metrics, viper.GetString(databaseDatadirFlg), encrypter).Start(stop, backuper); err != nil { return err } @@ -554,6 +552,16 @@ func initDatabase() error { return nil } +func initCompressor() error { + var err error + method := viper.GetString(compressionMethod) + compressor, err = compress.New(&compress.CompressorConfig{Method: method}) + if err != nil { + return fmt.Errorf("unable to initialize compressor: %w", err) + } + return nil +} + func initEncrypter() error { var err error key := viper.GetString(encryptionKeyFlg)