I'm currently trying to create a shell script that will execute a mysqldump on a remote mysql database and then copy the output file back to my local machine. The problem I'm running into is that I don't want to hard code the password into the script. I tried using 'read' to prompt for user input, but that variable doesn't seem to be getting passed to the remote command.
For reference, my script looks something like this:
#!/bin/bash
PASSWORD=""
TABLE=""
while getopts t: flag
do
case "${flag}" in
t) TABLE=${OPTARG};;
*) echo 'invalid argument.'
esac
done
read -sp "input the mysql password:" PASSWORD
#this is just to verify what the local and remote command is seeing
echo "$TABLE, $PASSWORD"
ssh myserver -p 9999 "echo $TABLE, $PASSWORD"
#this is the ssh command
ssh myserver -p 9999 "mysqldump --xml -h somesql.us-east-1.rds.amazonaws.com -u admin -p ${PASSWORD} mydb ${TABLE} > ${TABLE}.xml"
scp -p 9999 myserver:/${TABLE}.xml ${TABLE.xml}
If I run that script with
./data_input.sh -t mytable
it will prompt for a password, but not see the PASSWORD variable when the ssh command runs, and mysqldump will fail to authenticate. What is the correct way to pass along the user input variable to the ssh command?