command_fs_meta_save.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. package shell
  2. import (
  3. "flag"
  4. "fmt"
  5. "io"
  6. "os"
  7. "path/filepath"
  8. "strings"
  9. "sync"
  10. "sync/atomic"
  11. "time"
  12. "github.com/golang/protobuf/proto"
  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. `
  34. }
  35. func (c *commandFsMetaSave) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
  36. fsMetaSaveCommand := flag.NewFlagSet(c.Name(), flag.ContinueOnError)
  37. verbose := fsMetaSaveCommand.Bool("v", false, "print out each processed files")
  38. outputFileName := fsMetaSaveCommand.String("o", "", "output the meta data to this file")
  39. isObfuscate := fsMetaSaveCommand.Bool("obfuscate", false, "obfuscate the file names")
  40. // chunksFileName := fsMetaSaveCommand.String("chunks", "", "output all the chunks to this file")
  41. if err = fsMetaSaveCommand.Parse(args); err != nil {
  42. return nil
  43. }
  44. path, parseErr := commandEnv.parseUrl(findInputDirectory(fsMetaSaveCommand.Args()))
  45. if parseErr != nil {
  46. return parseErr
  47. }
  48. fileName := *outputFileName
  49. if fileName == "" {
  50. t := time.Now()
  51. fileName = fmt.Sprintf("%s-%d-%4d%02d%02d-%02d%02d%02d.meta",
  52. commandEnv.option.FilerHost, commandEnv.option.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 cipherKey util.CipherKey
  60. if *isObfuscate {
  61. cipherKey = util.GenCipherKey()
  62. }
  63. err = doTraverseBfsAndSaving(commandEnv, writer, path, *verbose, func(outputChan chan interface{}) {
  64. sizeBuf := make([]byte, 4)
  65. for item := range outputChan {
  66. b := item.([]byte)
  67. util.Uint32toBytes(sizeBuf, uint32(len(b)))
  68. dst.Write(sizeBuf)
  69. dst.Write(b)
  70. }
  71. }, func(entry *filer_pb.FullEntry, outputChan chan interface{}) (err error) {
  72. if !entry.Entry.IsDirectory {
  73. ext := filepath.Ext(entry.Entry.Name)
  74. if encrypted, encErr := util.Encrypt([]byte(entry.Entry.Name), cipherKey); encErr == nil {
  75. entry.Entry.Name = util.Base64Encode(encrypted)[:len(entry.Entry.Name)] + ext
  76. entry.Entry.Name = strings.ReplaceAll(entry.Entry.Name, "/", "x")
  77. }
  78. }
  79. bytes, err := proto.Marshal(entry)
  80. if err != nil {
  81. fmt.Fprintf(writer, "marshall error: %v\n", err)
  82. return
  83. }
  84. outputChan <- bytes
  85. return nil
  86. })
  87. if err == nil {
  88. fmt.Fprintf(writer, "meta data for http://%s:%d%s is saved to %s\n", commandEnv.option.FilerHost, commandEnv.option.FilerPort, path, fileName)
  89. }
  90. return err
  91. }
  92. 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 {
  93. var wg sync.WaitGroup
  94. wg.Add(1)
  95. outputChan := make(chan interface{}, 1024)
  96. go func() {
  97. saveFn(outputChan)
  98. wg.Done()
  99. }()
  100. var dirCount, fileCount uint64
  101. err := filer_pb.TraverseBfs(filerClient, util.FullPath(path), func(parentPath util.FullPath, entry *filer_pb.Entry) {
  102. protoMessage := &filer_pb.FullEntry{
  103. Dir: string(parentPath),
  104. Entry: entry,
  105. }
  106. if err := genFn(protoMessage, outputChan); err != nil {
  107. fmt.Fprintf(writer, "marshall error: %v\n", err)
  108. return
  109. }
  110. if entry.IsDirectory {
  111. atomic.AddUint64(&dirCount, 1)
  112. } else {
  113. atomic.AddUint64(&fileCount, 1)
  114. }
  115. if verbose {
  116. println(parentPath.Child(entry.Name))
  117. }
  118. })
  119. close(outputChan)
  120. wg.Wait()
  121. if err == nil && writer != nil {
  122. fmt.Fprintf(writer, "total %d directories, %d files\n", dirCount, fileCount)
  123. }
  124. return err
  125. }