From c660507b7d000948e66ff567fd8f5ce93cadb348 Mon Sep 17 00:00:00 2001 From: iyear Date: Mon, 4 Dec 2023 11:54:09 +0800 Subject: [PATCH] refactor(archive): rewrite based on new storage --- app/archive/backup.go | 34 ++++++++++++++++----------- app/archive/recover.go | 52 +++++++++++++++++++----------------------- cmd/archive.go | 6 ++--- test/archive_test.go | 14 ++++++------ 4 files changed, 54 insertions(+), 52 deletions(-) diff --git a/app/archive/backup.go b/app/archive/backup.go index 5a8fba5..26d362d 100644 --- a/app/archive/backup.go +++ b/app/archive/backup.go @@ -2,34 +2,42 @@ package archive import ( "context" + "encoding/json" "os" "github.com/fatih/color" "github.com/go-faster/errors" - "github.com/mholt/archiver/v4" + "github.com/klauspost/compress/zstd" + "go.uber.org/multierr" - "github.com/iyear/tdl/pkg/consts" + "github.com/iyear/tdl/pkg/kv" ) -func Backup(ctx context.Context, dst string) error { +func Backup(ctx context.Context, dst string) (rerr error) { + meta, err := kv.From(ctx).MigrateTo() + if err != nil { + return errors.Wrap(err, "read metadata") + } + f, err := os.Create(dst) if err != nil { return errors.Wrap(err, "create file") } - defer func(f *os.File) { - _ = f.Close() - }(f) + defer multierr.AppendInvoke(&rerr, multierr.Close(f)) - files, err := archiver.FilesFromDisk(nil, map[string]string{ - consts.KVPath: "", - }) + enc, err := zstd.NewWriter(f, zstd.WithEncoderLevel(zstd.SpeedBestCompression)) if err != nil { - return errors.Wrap(err, "walk files") + return errors.Wrap(err, "create zstd encoder") + } + defer multierr.AppendInvoke(&rerr, multierr.Close(enc)) + + metaB, err := json.Marshal(meta) + if err != nil { + return errors.Wrap(err, "marshal metadata") } - format := archiver.Zip{} - if err = format.Archive(ctx, f, files); err != nil { - return err + if _, err = enc.Write(metaB); err != nil { + return errors.Wrap(err, "write metadata") } color.Green("Backup successfully, file: %s", dst) diff --git a/app/archive/recover.go b/app/archive/recover.go index 5c2686e..03d0e78 100644 --- a/app/archive/recover.go +++ b/app/archive/recover.go @@ -1,52 +1,46 @@ package archive import ( + "bytes" "context" - "io" + "encoding/json" "os" - "path/filepath" "github.com/fatih/color" "github.com/go-faster/errors" - "github.com/mholt/archiver/v4" + "github.com/klauspost/compress/zstd" + "go.uber.org/multierr" - "github.com/iyear/tdl/pkg/consts" + "github.com/iyear/tdl/pkg/kv" ) -func Recover(ctx context.Context, file string) error { +func Recover(ctx context.Context, file string) (rerr error) { f, err := os.Open(file) if err != nil { return errors.Wrap(err, "open file") } - defer func(f *os.File) { - _ = f.Close() - }(f) + defer multierr.AppendInvoke(&rerr, multierr.Close(f)) - format := archiver.Zip{} + dec, err := zstd.NewReader(f) + if err != nil { + return errors.Wrap(err, "create zstd decoder") + } + defer dec.Close() - if err = format.Extract(ctx, f, nil, func(ctx context.Context, af archiver.File) error { - if af.IsDir() { - return nil - } - - v, err := af.Open() - if err != nil { - return errors.Wrap(err, "open archiver file") - } - defer func(v io.ReadCloser) { - _ = v.Close() - }(v) - - bytes, err := io.ReadAll(v) - if err != nil { - return errors.Wrap(err, "read all") - } - - return os.WriteFile(filepath.Join(consts.DataDir, af.Name()), bytes, 0o644) - }); err != nil { + metaB := bytes.NewBuffer(nil) + if _, err = dec.WriteTo(metaB); err != nil { return err } + var meta kv.Meta + if err = json.Unmarshal(metaB.Bytes(), &meta); err != nil { + return errors.Wrap(err, "unmarshal metadata") + } + + if err = kv.From(ctx).MigrateFrom(meta); err != nil { + return errors.Wrap(err, "migrate from") + } + color.Green("Recover successfully, file: %s", file) return nil } diff --git a/cmd/archive.go b/cmd/archive.go index f385cb4..b44050f 100644 --- a/cmd/archive.go +++ b/cmd/archive.go @@ -17,14 +17,14 @@ func NewBackup() *cobra.Command { Short: "Backup your data", RunE: func(cmd *cobra.Command, args []string) error { if dst == "" { - dst = fmt.Sprintf("tdl-backup-%s.zip", time.Now().Format("2006-01-02-15_04_05")) + dst = fmt.Sprintf("%s.backup.tdl", time.Now().Format("2006-01-02-15_04_05")) } return archive.Backup(cmd.Context(), dst) }, } - cmd.Flags().StringVarP(&dst, "dst", "d", "", "destination file path. Default: tdl-backup-