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.

173 lines
4.7 KiB

6 years ago
  1. package command
  2. import (
  3. "github.com/chrislusf/seaweedfs/weed/filer2"
  4. "github.com/chrislusf/seaweedfs/weed/glog"
  5. "github.com/chrislusf/seaweedfs/weed/notification"
  6. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  7. "github.com/chrislusf/seaweedfs/weed/server"
  8. "github.com/spf13/viper"
  9. )
  10. func init() {
  11. cmdFilerExport.Run = runFilerExport // break init cycle
  12. }
  13. var cmdFilerExport = &Command{
  14. UsageLine: "filer.export -sourceStore=mysql -targetStroe=cassandra",
  15. Short: "export meta data in filer store",
  16. Long: `Iterate the file tree and export all metadata out
  17. Both source and target store:
  18. * should be a store name already specified in filer.toml
  19. * do not need to be enabled state
  20. If target store is empty, only the directory tree will be listed.
  21. If target store is "notification", the list of entries will be sent to notification.
  22. This is usually used to bootstrap filer replication to a remote system.
  23. `,
  24. }
  25. var (
  26. // filerExportOutputFile = cmdFilerExport.Flag.String("output", "", "the output file. If empty, only list out the directory tree")
  27. filerExportSourceStore = cmdFilerExport.Flag.String("sourceStore", "", "the source store name in filer.toml")
  28. filerExportTargetStore = cmdFilerExport.Flag.String("targetStore", "", "the target store name in filer.toml, or \"notification\" to export all files to message queue")
  29. dirListLimit = cmdFilerExport.Flag.Int("dirListLimit", 100000, "limit directory list size")
  30. dryRun = cmdFilerExport.Flag.Bool("dryRun", false, "not actually moving data")
  31. )
  32. type statistics struct {
  33. directoryCount int
  34. fileCount int
  35. }
  36. func runFilerExport(cmd *Command, args []string) bool {
  37. weed_server.LoadConfiguration("filer", true)
  38. config := viper.GetViper()
  39. var sourceStore, targetStore filer2.FilerStore
  40. for _, store := range filer2.Stores {
  41. if store.GetName() == *filerExportSourceStore {
  42. viperSub := config.Sub(store.GetName())
  43. if err := store.Initialize(viperSub); err != nil {
  44. glog.Fatalf("Failed to initialize source store for %s: %+v",
  45. store.GetName(), err)
  46. } else {
  47. sourceStore = store
  48. }
  49. break
  50. }
  51. }
  52. for _, store := range filer2.Stores {
  53. if store.GetName() == *filerExportTargetStore {
  54. viperSub := config.Sub(store.GetName())
  55. if err := store.Initialize(viperSub); err != nil {
  56. glog.Fatalf("Failed to initialize target store for %s: %+v",
  57. store.GetName(), err)
  58. } else {
  59. targetStore = store
  60. }
  61. break
  62. }
  63. }
  64. if sourceStore == nil {
  65. glog.Errorf("Failed to find source store %s", *filerExportSourceStore)
  66. println("existing data sources are:")
  67. for _, store := range filer2.Stores {
  68. println(" " + store.GetName())
  69. }
  70. return false
  71. }
  72. if targetStore == nil && *filerExportTargetStore != "" && *filerExportTargetStore != "notification" {
  73. glog.Errorf("Failed to find target store %s", *filerExportTargetStore)
  74. println("existing data sources are:")
  75. for _, store := range filer2.Stores {
  76. println(" " + store.GetName())
  77. }
  78. return false
  79. }
  80. stat := statistics{}
  81. var fn func(level int, entry *filer2.Entry) error
  82. if *filerExportTargetStore == "notification" {
  83. weed_server.LoadConfiguration("notification", false)
  84. v := viper.GetViper()
  85. notification.LoadConfiguration(v.Sub("notification"))
  86. fn = func(level int, entry *filer2.Entry) error {
  87. printout(level, entry)
  88. if *dryRun {
  89. return nil
  90. }
  91. return notification.Queue.SendMessage(
  92. string(entry.FullPath),
  93. &filer_pb.EventNotification{
  94. NewEntry: entry.ToProtoEntry(),
  95. },
  96. )
  97. }
  98. } else if targetStore == nil {
  99. fn = printout
  100. } else {
  101. fn = func(level int, entry *filer2.Entry) error {
  102. printout(level, entry)
  103. if *dryRun {
  104. return nil
  105. }
  106. return targetStore.InsertEntry(entry)
  107. }
  108. }
  109. doTraverse(&stat, sourceStore, filer2.FullPath("/"), 0, fn)
  110. glog.Infof("processed %d directories, %d files", stat.directoryCount, stat.fileCount)
  111. return true
  112. }
  113. func doTraverse(stat *statistics, filerStore filer2.FilerStore, parentPath filer2.FullPath, level int, fn func(level int, entry *filer2.Entry) error) {
  114. limit := *dirListLimit
  115. lastEntryName := ""
  116. for {
  117. entries, err := filerStore.ListDirectoryEntries(parentPath, lastEntryName, false, limit)
  118. if err != nil {
  119. break
  120. }
  121. for _, entry := range entries {
  122. if fnErr := fn(level, entry); fnErr != nil {
  123. glog.Errorf("failed to process entry: %s", entry.FullPath)
  124. }
  125. if entry.IsDirectory() {
  126. stat.directoryCount++
  127. doTraverse(stat, filerStore, entry.FullPath, level+1, fn)
  128. } else {
  129. stat.fileCount++
  130. }
  131. }
  132. if len(entries) < limit {
  133. break
  134. }
  135. }
  136. }
  137. func printout(level int, entry *filer2.Entry) error {
  138. for i := 0; i < level; i++ {
  139. if i == level-1 {
  140. print("+-")
  141. } else {
  142. print("| ")
  143. }
  144. }
  145. println(entry.FullPath.Name())
  146. return nil
  147. }