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.

132 lines
4.8 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. package command
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/chrislusf/seaweedfs/weed/glog"
  6. "github.com/chrislusf/seaweedfs/weed/pb"
  7. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  8. "github.com/chrislusf/seaweedfs/weed/pb/remote_pb"
  9. "github.com/chrislusf/seaweedfs/weed/replication/source"
  10. "github.com/chrislusf/seaweedfs/weed/security"
  11. "github.com/chrislusf/seaweedfs/weed/util"
  12. "google.golang.org/grpc"
  13. "time"
  14. )
  15. type RemoteSyncOptions struct {
  16. filerAddress *string
  17. grpcDialOption grpc.DialOption
  18. readChunkFromFiler *bool
  19. debug *bool
  20. timeAgo *time.Duration
  21. dir *string
  22. createBucketAt *string
  23. createBucketRandomSuffix *bool
  24. mappings *remote_pb.RemoteStorageMapping
  25. remoteConfs map[string]*remote_pb.RemoteConf
  26. bucketsDir string
  27. }
  28. var _ = filer_pb.FilerClient(&RemoteSyncOptions{})
  29. func (option *RemoteSyncOptions) WithFilerClient(fn func(filer_pb.SeaweedFilerClient) error) error {
  30. return pb.WithFilerClient(*option.filerAddress, option.grpcDialOption, func(client filer_pb.SeaweedFilerClient) error {
  31. return fn(client)
  32. })
  33. }
  34. func (option *RemoteSyncOptions) AdjustedUrl(location *filer_pb.Location) string {
  35. return location.Url
  36. }
  37. var (
  38. remoteSyncOptions RemoteSyncOptions
  39. )
  40. func init() {
  41. cmdFilerRemoteSynchronize.Run = runFilerRemoteSynchronize // break init cycle
  42. remoteSyncOptions.filerAddress = cmdFilerRemoteSynchronize.Flag.String("filer", "localhost:8888", "filer of the SeaweedFS cluster")
  43. remoteSyncOptions.dir = cmdFilerRemoteSynchronize.Flag.String("dir", "/", "a mounted directory on filer")
  44. remoteSyncOptions.createBucketAt = cmdFilerRemoteSynchronize.Flag.String("createBucketAt", "", "one remote storage name to create new buckets in")
  45. remoteSyncOptions.createBucketRandomSuffix = cmdFilerRemoteSynchronize.Flag.Bool("createBucketWithRandomSuffix", false, "add randomized suffix to bucket name to avoid conflicts")
  46. remoteSyncOptions.readChunkFromFiler = cmdFilerRemoteSynchronize.Flag.Bool("filerProxy", false, "read file chunks from filer instead of volume servers")
  47. remoteSyncOptions.debug = cmdFilerRemoteSynchronize.Flag.Bool("debug", false, "debug mode to print out filer updated remote files")
  48. remoteSyncOptions.timeAgo = cmdFilerRemoteSynchronize.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\"")
  49. }
  50. var cmdFilerRemoteSynchronize = &Command{
  51. UsageLine: "filer.remote.sync -dir=/mount/s3_on_cloud or -createBucketAt=clound1",
  52. Short: "resumable continuously write back updates to remote storage",
  53. Long: `resumable continuously write back updates to remote storage
  54. filer.remote.sync listens on filer update events.
  55. If any mounted remote file is updated, it will fetch the updated content,
  56. and write to the remote storage.
  57. There are two modes:
  58. 1)Write back one mounted folder to remote storage
  59. weed filer.remote.sync -dir=/mount/s3_on_cloud
  60. 2)Watch /buckets folder and write back all changes.
  61. Any new buckets will be created in this remote storage.
  62. weed filer.remote.sync -createBucketAt=cloud1
  63. `,
  64. }
  65. func runFilerRemoteSynchronize(cmd *Command, args []string) bool {
  66. util.LoadConfiguration("security", false)
  67. grpcDialOption := security.LoadClientTLS(util.GetViper(), "grpc.client")
  68. remoteSyncOptions.grpcDialOption = grpcDialOption
  69. dir := *remoteSyncOptions.dir
  70. filerAddress := *remoteSyncOptions.filerAddress
  71. filerSource := &source.FilerSource{}
  72. filerSource.DoInitialize(
  73. filerAddress,
  74. pb.ServerToGrpcAddress(filerAddress),
  75. "/", // does not matter
  76. *remoteSyncOptions.readChunkFromFiler,
  77. )
  78. storageName := *remoteSyncOptions.createBucketAt
  79. if storageName != "" {
  80. remoteSyncOptions.bucketsDir = "/buckets"
  81. // check buckets again
  82. remoteSyncOptions.WithFilerClient(func(filerClient filer_pb.SeaweedFilerClient) error {
  83. resp, err := filerClient.GetFilerConfiguration(context.Background(), &filer_pb.GetFilerConfigurationRequest{})
  84. if err != nil {
  85. return err
  86. }
  87. remoteSyncOptions.bucketsDir = resp.DirBuckets
  88. return nil
  89. })
  90. fmt.Printf("synchronize %s, default new bucket creation in %s ...\n", remoteSyncOptions.bucketsDir, storageName)
  91. util.RetryForever("filer.remote.sync buckets "+storageName, func() error {
  92. return remoteSyncOptions.followBucketUpdatesAndUploadToRemote(filerSource)
  93. }, func(err error) bool {
  94. if err != nil {
  95. glog.Errorf("synchronize %s to %s: %v", remoteSyncOptions.bucketsDir, storageName, err)
  96. }
  97. return true
  98. })
  99. }
  100. if dir != "" {
  101. fmt.Printf("synchronize %s to remote storage...\n", dir)
  102. util.RetryForever("filer.remote.sync "+dir, func() error {
  103. return followUpdatesAndUploadToRemote(&remoteSyncOptions, filerSource, dir)
  104. }, func(err error) bool {
  105. if err != nil {
  106. glog.Errorf("synchronize %s: %v", dir, err)
  107. }
  108. return true
  109. })
  110. }
  111. return true
  112. }