Password prompts are cumbersome. The way to make ssh more usable is to use keys for authentication, and run a key agent (ssh-agent
) where you can register a key once per session (with an optional timeout). Then you can directly run
ssh root@server1 mysqldump --databases db | ssh root@server2 mysql
If you can log in from server1 to server2, you should transfer the data directly between the two servers.
ssh root@server1 'mysqldump --databases db | ssh root@server2 mysql'
(or the opposite if you can log in from server2 to server1). Again, have an SSH key for authentication. On your local machine, register an SSH private key that gives you access to server1 and one that gives you access to server2; make sure that agent forwarding is enabled (AgentForwarding yes
in ~/.ssh/config
).
If you really can't avoid entering a password, your best recourse is to first establish the connection, then go and transfer the data. With recent enough versions of OpenSSH, you can open a master connection, then subsequently route slave connections through it. The slave connections require no extra authentication. In your ~/.ssh/config
:
ControlMaster auto
ControlPath ~/.ssh/control:%h:%p:%r
Start a master connection to both servers:
ssh -N -M root@server1 &
ssh -N -M root@server2 &
Then do the copy:
ssh root@server1 mysqldump --databases db | ssh root@server2 mysql
After this you can kill the master connections if you don't need them anymore.
ssh -O exit root@server1
ssh -O exit root@server2