From c6caa6e4cc65f3a72b454b8440e75cbbf2b2ea9c Mon Sep 17 00:00:00 2001 From: root Date: Sun, 20 Nov 2022 20:28:47 -0800 Subject: [PATCH] wip --- go.mod | 1 + go.sum | 7 ++ .../shell/command_fs_meta_snapshots_create.go | 86 +++++++++++++++++++ 3 files changed, 94 insertions(+) create mode 100644 weed/shell/command_fs_meta_snapshots_create.go diff --git a/go.mod b/go.mod index 1fe1c4dcb..fe85fa815 100644 --- a/go.mod +++ b/go.mod @@ -148,6 +148,7 @@ require ( github.com/hanwen/go-fuse/v2 v2.1.1-0.20220627082937-d01fda7edf17 github.com/hashicorp/raft v1.3.11 github.com/hashicorp/raft-boltdb/v2 v2.2.2 + github.com/otiai10/copy v1.9.0 github.com/rabbitmq/amqp091-go v1.5.0 github.com/tikv/client-go/v2 v2.0.2 github.com/ydb-platform/ydb-go-sdk-auth-environ v0.1.2 diff --git a/go.sum b/go.sum index c863e62f2..902abfa61 100644 --- a/go.sum +++ b/go.sum @@ -1360,6 +1360,13 @@ github.com/openzipkin-contrib/zipkin-go-opentracing v0.4.5/go.mod h1:/wsWhb9smxS github.com/openzipkin/zipkin-go v0.1.6/go.mod h1:QgAqvLzwWbR/WpD4A3cGpPtJrZXNIiJc5AZX7/PBEpw= github.com/openzipkin/zipkin-go v0.2.1/go.mod h1:NaW6tEwdmWMaCDZzg8sh+IBNOxHMPnhQw8ySjnjRyN4= github.com/openzipkin/zipkin-go v0.2.2/go.mod h1:NaW6tEwdmWMaCDZzg8sh+IBNOxHMPnhQw8ySjnjRyN4= +github.com/otiai10/copy v1.9.0 h1:7KFNiCgZ91Ru4qW4CWPf/7jqtxLagGRmIxWldPP9VY4= +github.com/otiai10/copy v1.9.0/go.mod h1:hsfX19wcn0UWIHUQ3/4fHuehhk2UyArQ9dVFAn3FczI= +github.com/otiai10/curr v0.0.0-20150429015615-9b4961190c95/go.mod h1:9qAhocn7zKJG+0mI8eUu6xqkFDYS2kb2saOteoSB3cE= +github.com/otiai10/curr v1.0.0/go.mod h1:LskTG5wDwr8Rs+nNQ+1LlxRjAtTZZjtJW4rMXl6j4vs= +github.com/otiai10/mint v1.3.0/go.mod h1:F5AjcsTsWUqX+Na9fpHb52P8pcRX2CI6A3ctIT91xUo= +github.com/otiai10/mint v1.4.0 h1:umwcf7gbpEwf7WFzqmWwSv0CzbeMsae2u9ZvpP8j2q4= +github.com/otiai10/mint v1.4.0/go.mod h1:gifjb2MYOoULtKLqUAEILUG/9KONW6f7YsJ6vQLTlFI= github.com/pact-foundation/pact-go v1.0.4/go.mod h1:uExwJY4kCzNPcHRj+hCR/HBbOOIwwtUjcrb0b5/5kLM= github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc= github.com/pascaldekloe/goe v0.1.0 h1:cBOtyMzM9HTpWjXfbbunk26uA6nG3a8n06Wieeh0MwY= diff --git a/weed/shell/command_fs_meta_snapshots_create.go b/weed/shell/command_fs_meta_snapshots_create.go new file mode 100644 index 000000000..1a58dbf81 --- /dev/null +++ b/weed/shell/command_fs_meta_snapshots_create.go @@ -0,0 +1,86 @@ +package shell + +import ( + "flag" + "fmt" + cp "github.com/otiai10/copy" + "github.com/seaweedfs/seaweedfs/weed/filer" + "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb" + "github.com/seaweedfs/seaweedfs/weed/util" + "github.com/syndtr/goleveldb/leveldb" + "io" +) + +func init() { + Commands = append(Commands, &commandFsMetaSnapshotsCreate{}) +} + +type commandFsMetaSnapshotsCreate struct { +} + +func (c *commandFsMetaSnapshotsCreate) Name() string { + return "fs.meta.snapshots.create" +} + +func (c *commandFsMetaSnapshotsCreate) Help() string { + return `create snapshots of meta data from given time range. + + fs.meta.snapshots.create -s yyyy-mm-dd -e yyyy-mm-dd -o + + //These snapshot maybe later used to backup the system to certain timestamp. + +` +} + +func (c *commandFsMetaSnapshotsCreate) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) { + db, err := leveldb.OpenFile("/root/go/repos/seaweedfs/weed/snapshots.db", nil) + if err != nil { + return err + } + defer db.Close() + fsMetaSnapshotsCreateCommand := flag.NewFlagSet(c.Name(), flag.ContinueOnError) + if err = fsMetaSnapshotsCreateCommand.Parse(args); err != nil { + return err + } + + path := filer.SystemLogDir + count := 0 + err = filer_pb.TraverseBfs(commandEnv, util.FullPath(path), func(parentPath util.FullPath, entry *filer_pb.Entry) { + if !entry.IsDirectory { + fmt.Println("%+v", entry) + err = db.Put([]byte(entry.GetName()), entry.Content, nil) + count++ + if count%2 == 0 { + snapshotName := fmt.Sprintf("/root/go/repos/seaweedfs/weed/snapshots-%d.db", count/2) + e := cp.Copy("/root/go/repos/seaweedfs/weed/snapshots.db", snapshotName) + println(snapshotName) + if e != nil { + println("failed to generate " + snapshotName + e.Error()) + } + } + } + }) + verifying, err := leveldb.OpenFile("/root/go/repos/seaweedfs/weed/snapshots-1.db", nil) + if err != nil { + return err + } + iter := verifying.NewIterator(nil, nil) + for iter.Next() { + key := iter.Key() + fmt.Println("1 ---->") + fmt.Println(string(key)) + } + verifying.Close() + verifying, err = leveldb.OpenFile("/root/go/repos/seaweedfs/weed/snapshots-4.db", nil) + if err != nil { + return err + } + iter = verifying.NewIterator(nil, nil) + for iter.Next() { + key := iter.Key() + fmt.Println("4 ---->") + fmt.Println(string(key)) + } + verifying.Close() + return err +}