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.

118 lines
4.3 KiB

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