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.

106 lines
3.4 KiB

3 years ago
3 years ago
4 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/pb/filer_pb"
  7. "github.com/chrislusf/seaweedfs/weed/replication/source"
  8. "github.com/chrislusf/seaweedfs/weed/security"
  9. "github.com/chrislusf/seaweedfs/weed/util"
  10. "google.golang.org/grpc"
  11. "time"
  12. )
  13. type RemoteSyncOptions struct {
  14. filerAddress *string
  15. grpcDialOption grpc.DialOption
  16. readChunkFromFiler *bool
  17. timeAgo *time.Duration
  18. dir *string
  19. }
  20. var _ = filer_pb.FilerClient(&RemoteSyncOptions{})
  21. func (option *RemoteSyncOptions) WithFilerClient(fn func(filer_pb.SeaweedFilerClient) error) error {
  22. return pb.WithFilerClient(pb.ServerAddress(*option.filerAddress), option.grpcDialOption, func(client filer_pb.SeaweedFilerClient) error {
  23. return fn(client)
  24. })
  25. }
  26. func (option *RemoteSyncOptions) AdjustedUrl(location *filer_pb.Location) string {
  27. return location.Url
  28. }
  29. var (
  30. remoteSyncOptions RemoteSyncOptions
  31. )
  32. func init() {
  33. cmdFilerRemoteSynchronize.Run = runFilerRemoteSynchronize // break init cycle
  34. remoteSyncOptions.filerAddress = cmdFilerRemoteSynchronize.Flag.String("filer", "localhost:8888", "filer of the SeaweedFS cluster")
  35. remoteSyncOptions.dir = cmdFilerRemoteSynchronize.Flag.String("dir", "", "a mounted directory on filer")
  36. remoteSyncOptions.readChunkFromFiler = cmdFilerRemoteSynchronize.Flag.Bool("filerProxy", false, "read file chunks from filer instead of volume servers")
  37. 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\"")
  38. }
  39. var cmdFilerRemoteSynchronize = &Command{
  40. UsageLine: "filer.remote.sync",
  41. Short: "resumable continuously write back updates to remote storage",
  42. Long: `resumable continuously write back updates to remote storage
  43. filer.remote.sync listens on filer update events.
  44. If any mounted remote file is updated, it will fetch the updated content,
  45. and write to the remote storage.
  46. There are two modes:
  47. 1)By default, watch /buckets folder and write back all changes.
  48. # if there is only one remote storage configured
  49. weed filer.remote.sync
  50. # if there are multiple remote storages configured
  51. # specify a remote storage to create new buckets.
  52. weed filer.remote.sync -createBucketAt=cloud1
  53. 2)Write back one mounted folder to remote storage
  54. weed filer.remote.sync -dir=/mount/s3_on_cloud
  55. `,
  56. }
  57. func runFilerRemoteSynchronize(cmd *Command, args []string) bool {
  58. util.LoadConfiguration("security", false)
  59. grpcDialOption := security.LoadClientTLS(util.GetViper(), "grpc.client")
  60. remoteSyncOptions.grpcDialOption = grpcDialOption
  61. dir := *remoteSyncOptions.dir
  62. filerAddress := pb.ServerAddress(*remoteSyncOptions.filerAddress)
  63. filerSource := &source.FilerSource{}
  64. filerSource.DoInitialize(
  65. filerAddress.ToHttpAddress(),
  66. filerAddress.ToGrpcAddress(),
  67. "/", // does not matter
  68. *remoteSyncOptions.readChunkFromFiler,
  69. )
  70. if dir != "" {
  71. fmt.Printf("synchronize %s to remote storage...\n", dir)
  72. util.RetryForever("filer.remote.sync "+dir, func() error {
  73. return followUpdatesAndUploadToRemote(&remoteSyncOptions, filerSource, dir)
  74. }, func(err error) bool {
  75. if err != nil {
  76. glog.Errorf("synchronize %s: %v", dir, err)
  77. }
  78. return true
  79. })
  80. return true
  81. }
  82. return true
  83. }