I've come across a situation where I had to create a utilitary shell script that would be executed in many servers from different regions. The objective is to dump the local mysql database, copy it to another server and export the mysql to the remote server's database.
The script is going to read input from the user for this objective.
The problem is that I must avoid opening more than one SSH connection for the same server, in other words, the script should not ask more than once for a SSH password and the the solution I had in mind would end up making requiring it at least twice.
I also have some limitations: I can't change ssh's controlmaster and the server can't have sshpass installed. Also, some servers are not our own and I can't add keys for all of them as it was required that my script should be a stand alone approach with no further changes in any external server.
The steps I'm following are these:
- I export the database using mysqldump.
- Then I scp the .sql file to destination machine.
- I generate a new .sh file with the mysql import routine.
- Then open a new ssh connection to the destination server where I pass the .sh file and execute it.
- I clean up sql file and temporary sh file.
Does anyone know any better approach to execute this routine which can allow me to use only one session to accomplish all this work?
authorized_keys
and avoid passwords fully, then it would not matter how many connections are needed.