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.

163 lines
4.4 KiB

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