command_fs_meta_save.go 4.4 KB

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