command_volume_copy.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. package shell
  2. import (
  3. "flag"
  4. "fmt"
  5. "github.com/seaweedfs/seaweedfs/weed/pb"
  6. "io"
  7. "github.com/seaweedfs/seaweedfs/weed/storage/needle"
  8. )
  9. func init() {
  10. Commands = append(Commands, &commandVolumeCopy{})
  11. }
  12. type commandVolumeCopy struct {
  13. }
  14. func (c *commandVolumeCopy) Name() string {
  15. return "volume.copy"
  16. }
  17. func (c *commandVolumeCopy) Help() string {
  18. return `copy a volume from one volume server to another volume server
  19. volume.copy -source <source volume server host:port> -target <target volume server host:port> -volumeId <volume id>
  20. This command copies a volume from one volume server to another volume server.
  21. Usually you will want to unmount the volume first before copying.
  22. `
  23. }
  24. func (c *commandVolumeCopy) HasTag(CommandTag) bool {
  25. return false
  26. }
  27. func (c *commandVolumeCopy) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
  28. volCopyCommand := flag.NewFlagSet(c.Name(), flag.ContinueOnError)
  29. volumeIdInt := volCopyCommand.Int("volumeId", 0, "the volume id")
  30. sourceNodeStr := volCopyCommand.String("source", "", "the source volume server <host>:<port>")
  31. targetNodeStr := volCopyCommand.String("target", "", "the target volume server <host>:<port>")
  32. noLock := volCopyCommand.Bool("noLock", false, "do not lock the admin shell at one's own risk")
  33. if err = volCopyCommand.Parse(args); err != nil {
  34. return nil
  35. }
  36. if *noLock {
  37. commandEnv.noLock = true
  38. } else if err = commandEnv.confirmIsLocked(args); err != nil {
  39. return
  40. }
  41. sourceVolumeServer, targetVolumeServer := pb.ServerAddress(*sourceNodeStr), pb.ServerAddress(*targetNodeStr)
  42. volumeId := needle.VolumeId(*volumeIdInt)
  43. if sourceVolumeServer == targetVolumeServer {
  44. return fmt.Errorf("source and target volume servers are the same!")
  45. }
  46. _, err = copyVolume(commandEnv.option.GrpcDialOption, writer, volumeId, sourceVolumeServer, targetVolumeServer, "", 0)
  47. return
  48. }