command_fs_meta_load.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package shell
  2. import (
  3. "flag"
  4. "fmt"
  5. "io"
  6. "os"
  7. "strings"
  8. "time"
  9. "google.golang.org/protobuf/proto"
  10. "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
  11. "github.com/seaweedfs/seaweedfs/weed/util"
  12. )
  13. func init() {
  14. Commands = append(Commands, &commandFsMetaLoad{})
  15. }
  16. type commandFsMetaLoad struct {
  17. dirPrefix *string
  18. }
  19. func (c *commandFsMetaLoad) Name() string {
  20. return "fs.meta.load"
  21. }
  22. func (c *commandFsMetaLoad) Help() string {
  23. return `load saved filer meta data to restore the directory and file structure
  24. fs.meta.load <filer_host>-<port>-<time>.meta
  25. fs.meta.load -v=false <filer_host>-<port>-<time>.meta // skip printing out the verbose output
  26. fs.meta.load -dirPrefix=/buckets/important* <filer_host>.meta // load any dirs with prefix "important"
  27. `
  28. }
  29. func (c *commandFsMetaLoad) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
  30. if len(args) == 0 {
  31. fmt.Fprintf(writer, "missing a metadata file\n")
  32. return nil
  33. }
  34. fileName := args[len(args)-1]
  35. metaLoadCommand := flag.NewFlagSet(c.Name(), flag.ContinueOnError)
  36. c.dirPrefix = metaLoadCommand.String("dirPrefix", "", "load entries only with directories matching prefix")
  37. verbose := metaLoadCommand.Bool("v", true, "verbose mode")
  38. if err = metaLoadCommand.Parse(args[0 : len(args)-1]); err != nil {
  39. return nil
  40. }
  41. dst, err := os.OpenFile(fileName, os.O_RDONLY, 0644)
  42. if err != nil {
  43. return nil
  44. }
  45. defer dst.Close()
  46. var dirCount, fileCount uint64
  47. lastLogTime := time.Now()
  48. err = commandEnv.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error {
  49. sizeBuf := make([]byte, 4)
  50. for {
  51. if n, err := dst.Read(sizeBuf); n != 4 {
  52. if err == io.EOF {
  53. return nil
  54. }
  55. return err
  56. }
  57. size := util.BytesToUint32(sizeBuf)
  58. data := make([]byte, int(size))
  59. if n, err := dst.Read(data); n != len(data) {
  60. return err
  61. }
  62. fullEntry := &filer_pb.FullEntry{}
  63. if err = proto.Unmarshal(data, fullEntry); err != nil {
  64. return err
  65. }
  66. // check collection name pattern
  67. entryFullName := string(util.FullPath(fullEntry.Dir).Child(fullEntry.Entry.Name))
  68. if *c.dirPrefix != "" {
  69. if !strings.HasPrefix(fullEntry.Dir, *c.dirPrefix) {
  70. if *verbose {
  71. fmt.Fprintf(writer, "not match dir prefix %s\n", entryFullName)
  72. }
  73. continue
  74. }
  75. }
  76. if *verbose || lastLogTime.Add(time.Second).Before(time.Now()) {
  77. if !*verbose {
  78. lastLogTime = time.Now()
  79. }
  80. fmt.Fprintf(writer, "load %s\n", entryFullName)
  81. }
  82. fullEntry.Entry.Name = strings.ReplaceAll(fullEntry.Entry.Name, "/", "x")
  83. if err := filer_pb.CreateEntry(client, &filer_pb.CreateEntryRequest{
  84. Directory: fullEntry.Dir,
  85. Entry: fullEntry.Entry,
  86. }); err != nil {
  87. return err
  88. }
  89. if fullEntry.Entry.IsDirectory {
  90. dirCount++
  91. } else {
  92. fileCount++
  93. }
  94. }
  95. })
  96. if err == nil {
  97. fmt.Fprintf(writer, "\ntotal %d directories, %d files", dirCount, fileCount)
  98. fmt.Fprintf(writer, "\n%s is loaded.\n", fileName)
  99. }
  100. return err
  101. }