command_fs_meta_save.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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-%4d%02d%02d-%02d%02d%02d.meta",
  53. commandEnv.option.FilerAddress.ToHttpAddress(), 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(entry *filer_pb.FullEntry, outputChan chan interface{}) (err error) {
  65. if !entry.Entry.IsDirectory {
  66. ext := filepath.Ext(entry.Entry.Name)
  67. if encrypted, encErr := util.Encrypt([]byte(entry.Entry.Name), cipherKey); encErr == nil {
  68. entry.Entry.Name = util.Base64Encode(encrypted)[:len(entry.Entry.Name)] + ext
  69. entry.Entry.Name = strings.ReplaceAll(entry.Entry.Name, "/", "x")
  70. }
  71. }
  72. bytes, err := proto.Marshal(entry)
  73. if err != nil {
  74. fmt.Fprintf(writer, "marshall error: %v\n", err)
  75. return
  76. }
  77. outputChan <- bytes
  78. return nil
  79. }, func(outputChan chan interface{}) {
  80. sizeBuf := make([]byte, 4)
  81. for item := range outputChan {
  82. b := item.([]byte)
  83. util.Uint32toBytes(sizeBuf, uint32(len(b)))
  84. dst.Write(sizeBuf)
  85. dst.Write(b)
  86. }
  87. })
  88. if err == nil {
  89. fmt.Fprintf(writer, "meta data for http://%s%s is saved to %s\n", commandEnv.option.FilerAddress.ToHttpAddress(), path, fileName)
  90. }
  91. return err
  92. }
  93. func doTraverseBfsAndSaving(filerClient filer_pb.FilerClient, writer io.Writer, path string, verbose bool, genFn func(entry *filer_pb.FullEntry, outputChan chan interface{}) error, saveFn func(outputChan chan interface{})) 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. }