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.

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