You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

172 lines
4.4 KiB

  1. package shell
  2. import (
  3. "context"
  4. "flag"
  5. "fmt"
  6. "io"
  7. "os"
  8. "sync"
  9. "sync/atomic"
  10. "time"
  11. "github.com/golang/protobuf/proto"
  12. "github.com/chrislusf/seaweedfs/weed/filer2"
  13. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  14. "github.com/chrislusf/seaweedfs/weed/util"
  15. )
  16. func init() {
  17. Commands = append(Commands, &commandFsMetaSave{})
  18. }
  19. type commandFsMetaSave struct {
  20. }
  21. func (c *commandFsMetaSave) Name() string {
  22. return "fs.meta.save"
  23. }
  24. func (c *commandFsMetaSave) Help() string {
  25. return `save all directory and file meta data to a local file for metadata backup.
  26. fs.meta.save / # save from the root
  27. fs.meta.save -v -o t.meta / # save from the root, output to t.meta file.
  28. fs.meta.save /path/to/save # save from the directory /path/to/save
  29. fs.meta.save . # save from current directory
  30. fs.meta.save # save from current directory
  31. The meta data will be saved into a local <filer_host>-<port>-<time>.meta file.
  32. These meta data can be later loaded by fs.meta.load command,
  33. This assumes there are no deletions, so this is different from taking a snapshot.
  34. `
  35. }
  36. func (c *commandFsMetaSave) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
  37. fsMetaSaveCommand := flag.NewFlagSet(c.Name(), flag.ContinueOnError)
  38. verbose := fsMetaSaveCommand.Bool("v", false, "print out each processed files")
  39. outputFileName := fsMetaSaveCommand.String("o", "", "output the meta data to this file")
  40. if err = fsMetaSaveCommand.Parse(args); err != nil {
  41. return nil
  42. }
  43. filerServer, filerPort, path, parseErr := commandEnv.parseUrl(findInputDirectory(fsMetaSaveCommand.Args()))
  44. if parseErr != nil {
  45. return parseErr
  46. }
  47. ctx := context.Background()
  48. t := time.Now()
  49. fileName := *outputFileName
  50. if fileName == "" {
  51. fileName = fmt.Sprintf("%s-%d-%4d%02d%02d-%02d%02d%02d.meta",
  52. filerServer, filerPort, t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second())
  53. }
  54. dst, openErr := os.OpenFile(fileName, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
  55. if openErr != nil {
  56. return fmt.Errorf("failed to create file %s: %v", fileName, openErr)
  57. }
  58. defer dst.Close()
  59. var dirCount, fileCount uint64
  60. err = doTraverseBFS(ctx, writer, commandEnv.getFilerClient(filerServer, filerPort), filer2.FullPath(path), func(parentPath filer2.FullPath, entry *filer_pb.Entry) {
  61. protoMessage := &filer_pb.FullEntry{
  62. Dir: string(parentPath),
  63. Entry: entry,
  64. }
  65. bytes, err := proto.Marshal(protoMessage)
  66. if err != nil {
  67. fmt.Fprintf(writer, "marshall error: %v\n", err)
  68. return
  69. }
  70. sizeBuf := make([]byte, 4)
  71. util.Uint32toBytes(sizeBuf, uint32(len(bytes)))
  72. dst.Write(sizeBuf)
  73. dst.Write(bytes)
  74. if entry.IsDirectory {
  75. atomic.AddUint64(&dirCount, 1)
  76. } else {
  77. atomic.AddUint64(&fileCount, 1)
  78. }
  79. if *verbose {
  80. println(parentPath.Child(entry.Name))
  81. }
  82. })
  83. if err == nil {
  84. fmt.Fprintf(writer, "\ntotal %d directories, %d files", dirCount, fileCount)
  85. fmt.Fprintf(writer, "\nmeta data for http://%s:%d%s is saved to %s\n", filerServer, filerPort, path, fileName)
  86. }
  87. return err
  88. }
  89. func doTraverseBFS(ctx context.Context, writer io.Writer, filerClient filer2.FilerClient,
  90. parentPath filer2.FullPath, fn func(parentPath filer2.FullPath, entry *filer_pb.Entry)) (err error) {
  91. K := 5
  92. var jobQueueWg sync.WaitGroup
  93. queue := util.NewQueue()
  94. jobQueueWg.Add(1)
  95. queue.Enqueue(parentPath)
  96. var isTerminating bool
  97. for i := 0; i < K; i++ {
  98. go func() {
  99. for {
  100. if isTerminating {
  101. break
  102. }
  103. t := queue.Dequeue()
  104. if t == nil {
  105. time.Sleep(329 * time.Millisecond)
  106. continue
  107. }
  108. dir := t.(filer2.FullPath)
  109. processErr := processOneDirectory(ctx, writer, filerClient, dir, queue, &jobQueueWg, fn)
  110. if processErr != nil {
  111. err = processErr
  112. }
  113. jobQueueWg.Done()
  114. }
  115. }()
  116. }
  117. jobQueueWg.Wait()
  118. isTerminating = true
  119. return
  120. }
  121. func processOneDirectory(ctx context.Context, writer io.Writer, filerClient filer2.FilerClient,
  122. parentPath filer2.FullPath, queue *util.Queue, jobQueueWg *sync.WaitGroup,
  123. fn func(parentPath filer2.FullPath, entry *filer_pb.Entry)) (err error) {
  124. return filer2.ReadDirAllEntries(ctx, filerClient, string(parentPath), "", func(entry *filer_pb.Entry, isLast bool) {
  125. fn(parentPath, entry)
  126. if entry.IsDirectory {
  127. subDir := fmt.Sprintf("%s/%s", parentPath, entry.Name)
  128. if parentPath == "/" {
  129. subDir = "/" + entry.Name
  130. }
  131. jobQueueWg.Add(1)
  132. queue.Enqueue(filer2.FullPath(subDir))
  133. }
  134. })
  135. }