command_fs_meta_save.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. package shell
  2. import (
  3. "context"
  4. "flag"
  5. "fmt"
  6. "io"
  7. "os"
  8. "time"
  9. "github.com/chrislusf/seaweedfs/weed/filer2"
  10. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  11. "github.com/chrislusf/seaweedfs/weed/util"
  12. "github.com/golang/protobuf/proto"
  13. )
  14. func init() {
  15. Commands = append(Commands, &commandFsMetaSave{})
  16. }
  17. type commandFsMetaSave struct {
  18. }
  19. func (c *commandFsMetaSave) Name() string {
  20. return "fs.meta.save"
  21. }
  22. func (c *commandFsMetaSave) Help() string {
  23. return `save all directory and file meta data to a local file for metadata backup.
  24. fs.meta.save / # save from the root
  25. fs.meta.save -v -o t.meta / # save from the root, output to t.meta file.
  26. fs.meta.save /path/to/save # save from the directory /path/to/save
  27. fs.meta.save . # save from current directory
  28. fs.meta.save # save from current directory
  29. The meta data will be saved into a local <filer_host>-<port>-<time>.meta file.
  30. These meta data can be later loaded by fs.meta.load command,
  31. This assumes there are no deletions, so this is different from taking a snapshot.
  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. if err = fsMetaSaveCommand.Parse(args); err != nil {
  39. return nil
  40. }
  41. filerServer, filerPort, path, err := commandEnv.parseUrl(findInputDirectory(fsMetaSaveCommand.Args()))
  42. if err != nil {
  43. return err
  44. }
  45. ctx := context.Background()
  46. return commandEnv.withFilerClient(ctx, filerServer, filerPort, func(client filer_pb.SeaweedFilerClient) error {
  47. t := time.Now()
  48. fileName := *outputFileName
  49. if fileName == "" {
  50. fileName = fmt.Sprintf("%s-%d-%4d%02d%02d-%02d%02d%02d.meta",
  51. filerServer, filerPort, t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second())
  52. }
  53. dst, err := os.OpenFile(fileName, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
  54. if err != nil {
  55. return nil
  56. }
  57. defer dst.Close()
  58. var dirCount, fileCount uint64
  59. sizeBuf := make([]byte, 4)
  60. err = doTraverse(ctx, writer, client, filer2.FullPath(path), func(parentPath filer2.FullPath, entry *filer_pb.Entry) error {
  61. protoMessage := &filer_pb.FullEntry{
  62. Dir: string(parentPath),
  63. Entry: entry,
  64. }
  65. bytes, err := proto.Marshal(protoMessage)
  66. if err != nil {
  67. return fmt.Errorf("marshall error: %v", err)
  68. }
  69. util.Uint32toBytes(sizeBuf, uint32(len(bytes)))
  70. dst.Write(sizeBuf)
  71. dst.Write(bytes)
  72. if entry.IsDirectory {
  73. dirCount++
  74. } else {
  75. fileCount++
  76. }
  77. if *verbose {
  78. println(parentPath.Child(entry.Name))
  79. }
  80. return nil
  81. })
  82. if err == nil {
  83. fmt.Fprintf(writer, "\ntotal %d directories, %d files", dirCount, fileCount)
  84. fmt.Fprintf(writer, "\nmeta data for http://%s:%d%s is saved to %s\n", filerServer, filerPort, path, fileName)
  85. }
  86. return err
  87. })
  88. }
  89. func doTraverse(ctx context.Context, writer io.Writer, client filer_pb.SeaweedFilerClient, parentPath filer2.FullPath, fn func(parentPath filer2.FullPath, entry *filer_pb.Entry) error) (err error) {
  90. paginatedCount := -1
  91. startFromFileName := ""
  92. paginateSize := 1000
  93. for paginatedCount == -1 || paginatedCount == paginateSize {
  94. resp, listErr := client.ListEntries(ctx, &filer_pb.ListEntriesRequest{
  95. Directory: string(parentPath),
  96. Prefix: "",
  97. StartFromFileName: startFromFileName,
  98. InclusiveStartFrom: false,
  99. Limit: uint32(paginateSize),
  100. })
  101. if listErr != nil {
  102. err = listErr
  103. return
  104. }
  105. paginatedCount = len(resp.Entries)
  106. for _, entry := range resp.Entries {
  107. if err = fn(parentPath, entry); err != nil {
  108. return err
  109. }
  110. if entry.IsDirectory {
  111. subDir := fmt.Sprintf("%s/%s", parentPath, entry.Name)
  112. if parentPath == "/" {
  113. subDir = "/" + entry.Name
  114. }
  115. if err = doTraverse(ctx, writer, client, filer2.FullPath(subDir), fn); err != nil {
  116. return err
  117. }
  118. }
  119. startFromFileName = entry.Name
  120. }
  121. }
  122. return
  123. }