command_volume_copy.go 1.4 KB

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