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.

174 lines
4.9 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, default to currently enabled store")
  28. filerExportTargetStore = cmdFilerExport.Flag.String("targetStore", "", "the target store name in filer.toml, or \"notification\" to export all files to message queue")
  29. dir = cmdFilerExport.Flag.String("dir", "/", "only process files under this directory")
  30. dirListLimit = cmdFilerExport.Flag.Int("dirListLimit", 100000, "limit directory list size")
  31. dryRun = cmdFilerExport.Flag.Bool("dryRun", false, "not actually moving data")
  32. )
  33. type statistics struct {
  34. directoryCount int
  35. fileCount int
  36. }
  37. func runFilerExport(cmd *Command, args []string) bool {
  38. weed_server.LoadConfiguration("filer", true)
  39. config := viper.GetViper()
  40. var sourceStore, targetStore filer2.FilerStore
  41. for _, store := range filer2.Stores {
  42. if store.GetName() == *filerExportSourceStore || *filerExportSourceStore == "" && config.GetBool(store.GetName()+".enabled") {
  43. viperSub := config.Sub(store.GetName())
  44. if err := store.Initialize(viperSub); err != nil {
  45. glog.Fatalf("Failed to initialize source store for %s: %+v",
  46. store.GetName(), err)
  47. } else {
  48. sourceStore = store
  49. }
  50. break
  51. }
  52. }
  53. for _, store := range filer2.Stores {
  54. if store.GetName() == *filerExportTargetStore {
  55. viperSub := config.Sub(store.GetName())
  56. if err := store.Initialize(viperSub); err != nil {
  57. glog.Fatalf("Failed to initialize target store for %s: %+v",
  58. store.GetName(), err)
  59. } else {
  60. targetStore = store
  61. }
  62. break
  63. }
  64. }
  65. if sourceStore == nil {
  66. glog.Errorf("Failed to find source store %s", *filerExportSourceStore)
  67. println("existing data sources are:")
  68. for _, store := range filer2.Stores {
  69. println(" " + store.GetName())
  70. }
  71. return false
  72. }
  73. if targetStore == nil && *filerExportTargetStore != "" && *filerExportTargetStore != "notification" {
  74. glog.Errorf("Failed to find target store %s", *filerExportTargetStore)
  75. println("existing data sources are:")
  76. for _, store := range filer2.Stores {
  77. println(" " + store.GetName())
  78. }
  79. return false
  80. }
  81. stat := statistics{}
  82. var fn func(level int, entry *filer2.Entry) error
  83. if *filerExportTargetStore == "notification" {
  84. weed_server.LoadConfiguration("notification", false)
  85. v := viper.GetViper()
  86. notification.LoadConfiguration(v.Sub("notification"))
  87. fn = func(level int, entry *filer2.Entry) error {
  88. printout(level, entry)
  89. if *dryRun {
  90. return nil
  91. }
  92. return notification.Queue.SendMessage(
  93. string(entry.FullPath),
  94. &filer_pb.EventNotification{
  95. NewEntry: entry.ToProtoEntry(),
  96. },
  97. )
  98. }
  99. } else if targetStore == nil {
  100. fn = printout
  101. } else {
  102. fn = func(level int, entry *filer2.Entry) error {
  103. printout(level, entry)
  104. if *dryRun {
  105. return nil
  106. }
  107. return targetStore.InsertEntry(entry)
  108. }
  109. }
  110. doTraverse(&stat, sourceStore, filer2.FullPath(*dir), 0, fn)
  111. glog.Infof("processed %d directories, %d files", stat.directoryCount, stat.fileCount)
  112. return true
  113. }
  114. func doTraverse(stat *statistics, filerStore filer2.FilerStore, parentPath filer2.FullPath, level int, fn func(level int, entry *filer2.Entry) error) {
  115. limit := *dirListLimit
  116. lastEntryName := ""
  117. for {
  118. entries, err := filerStore.ListDirectoryEntries(parentPath, lastEntryName, false, limit)
  119. if err != nil {
  120. break
  121. }
  122. for _, entry := range entries {
  123. if fnErr := fn(level, entry); fnErr != nil {
  124. glog.Errorf("failed to process entry: %s", entry.FullPath)
  125. }
  126. if entry.IsDirectory() {
  127. stat.directoryCount++
  128. doTraverse(stat, filerStore, entry.FullPath, level+1, fn)
  129. } else {
  130. stat.fileCount++
  131. }
  132. }
  133. if len(entries) < limit {
  134. break
  135. }
  136. }
  137. }
  138. func printout(level int, entry *filer2.Entry) error {
  139. for i := 0; i < level; i++ {
  140. if i == level-1 {
  141. print("+-")
  142. } else {
  143. print("| ")
  144. }
  145. }
  146. println(entry.FullPath.Name())
  147. return nil
  148. }