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.

129 lines
4.7 KiB

3 years ago
  1. package command
  2. import (
  3. "fmt"
  4. "github.com/seaweedfs/seaweedfs/weed/glog"
  5. "github.com/seaweedfs/seaweedfs/weed/pb"
  6. "github.com/seaweedfs/seaweedfs/weed/replication/source"
  7. "github.com/seaweedfs/seaweedfs/weed/security"
  8. "github.com/seaweedfs/seaweedfs/weed/util"
  9. "google.golang.org/grpc"
  10. "strings"
  11. "time"
  12. )
  13. type FilerBackupOptions struct {
  14. isActivePassive *bool
  15. filer *string
  16. path *string
  17. excludePaths *string
  18. debug *bool
  19. proxyByFiler *bool
  20. timeAgo *time.Duration
  21. }
  22. var (
  23. filerBackupOptions FilerBackupOptions
  24. )
  25. func init() {
  26. cmdFilerBackup.Run = runFilerBackup // break init cycle
  27. filerBackupOptions.filer = cmdFilerBackup.Flag.String("filer", "localhost:8888", "filer of one SeaweedFS cluster")
  28. filerBackupOptions.path = cmdFilerBackup.Flag.String("filerPath", "/", "directory to sync on filer")
  29. filerBackupOptions.excludePaths = cmdFilerBackup.Flag.String("filerExcludePaths", "", "exclude directories to sync on filer")
  30. filerBackupOptions.proxyByFiler = cmdFilerBackup.Flag.Bool("filerProxy", false, "read and write file chunks by filer instead of volume servers")
  31. filerBackupOptions.debug = cmdFilerBackup.Flag.Bool("debug", false, "debug mode to print out received files")
  32. filerBackupOptions.timeAgo = cmdFilerBackup.Flag.Duration("timeAgo", 0, "start time before now. \"300ms\", \"1.5h\" or \"2h45m\". Valid time units are \"ns\", \"us\" (or \"µs\"), \"ms\", \"s\", \"m\", \"h\"")
  33. }
  34. var cmdFilerBackup = &Command{
  35. UsageLine: "filer.backup -filer=<filerHost>:<filerPort> ",
  36. Short: "resume-able continuously replicate files from a SeaweedFS cluster to another location defined in replication.toml",
  37. Long: `resume-able continuously replicate files from a SeaweedFS cluster to another location defined in replication.toml
  38. filer.backup listens on filer notifications. If any file is updated, it will fetch the updated content,
  39. and write to the destination. This is to replace filer.replicate command since additional message queue is not needed.
  40. If restarted and "-timeAgo" is not set, the synchronization will resume from the previous checkpoints, persisted every minute.
  41. A fresh sync will start from the earliest metadata logs. To reset the checkpoints, just set "-timeAgo" to a high value.
  42. `,
  43. }
  44. func runFilerBackup(cmd *Command, args []string) bool {
  45. util.LoadConfiguration("security", false)
  46. util.LoadConfiguration("replication", true)
  47. grpcDialOption := security.LoadClientTLS(util.GetViper(), "grpc.client")
  48. clientId := util.RandomInt32()
  49. var clientEpoch int32
  50. for {
  51. clientEpoch++
  52. err := doFilerBackup(grpcDialOption, &filerBackupOptions, clientId, clientEpoch)
  53. if err != nil {
  54. glog.Errorf("backup from %s: %v", *filerBackupOptions.filer, err)
  55. time.Sleep(1747 * time.Millisecond)
  56. }
  57. }
  58. return true
  59. }
  60. const (
  61. BackupKeyPrefix = "backup."
  62. )
  63. func doFilerBackup(grpcDialOption grpc.DialOption, backupOption *FilerBackupOptions, clientId int32, clientEpoch int32) error {
  64. // find data sink
  65. config := util.GetViper()
  66. dataSink := findSink(config)
  67. if dataSink == nil {
  68. return fmt.Errorf("no data sink configured in replication.toml")
  69. }
  70. sourceFiler := pb.ServerAddress(*backupOption.filer)
  71. sourcePath := *backupOption.path
  72. excludePaths := strings.Split(*backupOption.excludePaths, ",")
  73. timeAgo := *backupOption.timeAgo
  74. targetPath := dataSink.GetSinkToDirectory()
  75. debug := *backupOption.debug
  76. // get start time for the data sink
  77. startFrom := time.Unix(0, 0)
  78. sinkId := util.HashStringToLong(dataSink.GetName() + dataSink.GetSinkToDirectory())
  79. if timeAgo.Milliseconds() == 0 {
  80. lastOffsetTsNs, err := getOffset(grpcDialOption, sourceFiler, BackupKeyPrefix, int32(sinkId))
  81. if err != nil {
  82. glog.V(0).Infof("starting from %v", startFrom)
  83. } else {
  84. startFrom = time.Unix(0, lastOffsetTsNs)
  85. glog.V(0).Infof("resuming from %v", startFrom)
  86. }
  87. } else {
  88. startFrom = time.Now().Add(-timeAgo)
  89. glog.V(0).Infof("start time is set to %v", startFrom)
  90. }
  91. // create filer sink
  92. filerSource := &source.FilerSource{}
  93. filerSource.DoInitialize(
  94. sourceFiler.ToHttpAddress(),
  95. sourceFiler.ToGrpcAddress(),
  96. sourcePath,
  97. *backupOption.proxyByFiler)
  98. dataSink.SetSourceFiler(filerSource)
  99. processEventFn := genProcessFunction(sourcePath, targetPath, excludePaths, dataSink, debug)
  100. processEventFnWithOffset := pb.AddOffsetFunc(processEventFn, 3*time.Second, func(counter int64, lastTsNs int64) error {
  101. glog.V(0).Infof("backup %s progressed to %v %0.2f/sec", sourceFiler, time.Unix(0, lastTsNs), float64(counter)/float64(3))
  102. return setOffset(grpcDialOption, sourceFiler, BackupKeyPrefix, int32(sinkId), lastTsNs)
  103. })
  104. return pb.FollowMetadata(sourceFiler, grpcDialOption, "backup_"+dataSink.GetName(), clientId, clientEpoch, sourcePath, nil, startFrom.UnixNano(), 0, 0, processEventFnWithOffset, pb.TrivialOnError)
  105. }