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.

143 lines
3.8 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. package shell
  2. import (
  3. "flag"
  4. "fmt"
  5. "io"
  6. "os"
  7. "sync"
  8. "sync/atomic"
  9. "time"
  10. "github.com/golang/protobuf/proto"
  11. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  12. "github.com/chrislusf/seaweedfs/weed/util"
  13. )
  14. func init() {
  15. Commands = append(Commands, &commandFsMetaSave{})
  16. }
  17. type commandFsMetaSave struct {
  18. }
  19. func (c *commandFsMetaSave) Name() string {
  20. return "fs.meta.save"
  21. }
  22. func (c *commandFsMetaSave) Help() string {
  23. return `save all directory and file meta data to a local file for metadata backup.
  24. fs.meta.save / # save from the root
  25. fs.meta.save -v -o t.meta / # save from the root, output to t.meta file.
  26. fs.meta.save /path/to/save # save from the directory /path/to/save
  27. fs.meta.save . # save from current directory
  28. fs.meta.save # save from current directory
  29. The meta data will be saved into a local <filer_host>-<port>-<time>.meta file.
  30. These meta data can be later loaded by fs.meta.load command,
  31. `
  32. }
  33. func (c *commandFsMetaSave) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
  34. fsMetaSaveCommand := flag.NewFlagSet(c.Name(), flag.ContinueOnError)
  35. verbose := fsMetaSaveCommand.Bool("v", false, "print out each processed files")
  36. outputFileName := fsMetaSaveCommand.String("o", "", "output the meta data to this file")
  37. // chunksFileName := fsMetaSaveCommand.String("chunks", "", "output all the chunks to this file")
  38. if err = fsMetaSaveCommand.Parse(args); err != nil {
  39. return nil
  40. }
  41. path, parseErr := commandEnv.parseUrl(findInputDirectory(fsMetaSaveCommand.Args()))
  42. if parseErr != nil {
  43. return parseErr
  44. }
  45. fileName := *outputFileName
  46. if fileName == "" {
  47. t := time.Now()
  48. fileName = fmt.Sprintf("%s-%d-%4d%02d%02d-%02d%02d%02d.meta",
  49. commandEnv.option.FilerHost, commandEnv.option.FilerPort, t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second())
  50. }
  51. dst, openErr := os.OpenFile(fileName, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
  52. if openErr != nil {
  53. return fmt.Errorf("failed to create file %s: %v", fileName, openErr)
  54. }
  55. defer dst.Close()
  56. err = doTraverseBfsAndSaving(commandEnv, writer, path, *verbose, func(outputChan chan interface{}) {
  57. sizeBuf := make([]byte, 4)
  58. for item := range outputChan {
  59. b := item.([]byte)
  60. util.Uint32toBytes(sizeBuf, uint32(len(b)))
  61. dst.Write(sizeBuf)
  62. dst.Write(b)
  63. }
  64. }, func(entry *filer_pb.FullEntry, outputChan chan interface{}) (err error) {
  65. bytes, err := proto.Marshal(entry)
  66. if err != nil {
  67. fmt.Fprintf(writer, "marshall error: %v\n", err)
  68. return
  69. }
  70. outputChan <- bytes
  71. return nil
  72. })
  73. if err == nil {
  74. fmt.Fprintf(writer, "meta data for http://%s:%d%s is saved to %s\n", commandEnv.option.FilerHost, commandEnv.option.FilerPort, path, fileName)
  75. }
  76. return err
  77. }
  78. func doTraverseBfsAndSaving(filerClient filer_pb.FilerClient, writer io.Writer, path string, verbose bool, saveFn func(outputChan chan interface{}), genFn func(entry *filer_pb.FullEntry, outputChan chan interface{}) error) error {
  79. var wg sync.WaitGroup
  80. wg.Add(1)
  81. outputChan := make(chan interface{}, 1024)
  82. go func() {
  83. saveFn(outputChan)
  84. wg.Done()
  85. }()
  86. var dirCount, fileCount uint64
  87. err := filer_pb.TraverseBfs(filerClient, util.FullPath(path), func(parentPath util.FullPath, entry *filer_pb.Entry) {
  88. protoMessage := &filer_pb.FullEntry{
  89. Dir: string(parentPath),
  90. Entry: entry,
  91. }
  92. if err := genFn(protoMessage, outputChan); err != nil {
  93. fmt.Fprintf(writer, "marshall error: %v\n", err)
  94. return
  95. }
  96. if entry.IsDirectory {
  97. atomic.AddUint64(&dirCount, 1)
  98. } else {
  99. atomic.AddUint64(&fileCount, 1)
  100. }
  101. if verbose {
  102. println(parentPath.Child(entry.Name))
  103. }
  104. })
  105. close(outputChan)
  106. wg.Wait()
  107. if err == nil && writer != nil {
  108. fmt.Fprintf(writer, "total %d directories, %d files\n", dirCount, fileCount)
  109. }
  110. return err
  111. }