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.

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