command_volume_copy.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package shell
  2. import (
  3. "context"
  4. "fmt"
  5. "io"
  6. "github.com/chrislusf/seaweedfs/weed/storage/needle"
  7. )
  8. func init() {
  9. Commands = append(Commands, &commandVolumeCopy{})
  10. }
  11. type commandVolumeCopy struct {
  12. }
  13. func (c *commandVolumeCopy) Name() string {
  14. return "volume.copy"
  15. }
  16. func (c *commandVolumeCopy) Help() string {
  17. return `copy a volume from one volume server to another volume server
  18. volume.copy <source volume server host:port> <target volume server host:port> <volume id>
  19. This command copies a volume from one volume server to another volume server.
  20. Usually you will want to unmount the volume first before copying.
  21. `
  22. }
  23. func (c *commandVolumeCopy) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
  24. if len(args) != 3 {
  25. fmt.Fprintf(writer, "received args: %+v\n", args)
  26. return fmt.Errorf("need 3 args of <source volume server host:port> <target volume server host:port> <volume id>")
  27. }
  28. sourceVolumeServer, targetVolumeServer, volumeIdString := args[0], args[1], args[2]
  29. volumeId, err := needle.NewVolumeId(volumeIdString)
  30. if err != nil {
  31. return fmt.Errorf("wrong volume id format %s: %v", volumeId, err)
  32. }
  33. if sourceVolumeServer == targetVolumeServer {
  34. return fmt.Errorf("source and target volume servers are the same!")
  35. }
  36. ctx := context.Background()
  37. _, err = copyVolume(ctx, commandEnv.option.GrpcDialOption, volumeId, sourceVolumeServer, targetVolumeServer)
  38. return
  39. }