2

I apologize if the title doesn't quite describe what I'm looking for. Please edit if possible.

I have a bash script that does the following:

serveruser="root"
serverip=1.2.3.4
serverpath="/var/www"


ssh "$serveruser"@"$serverip" /bin/bash <<\EOF
mkdir -p "$serverpath/mysqldumps/"
cd "$serverpath/mysqldumps/"
domainname=somedomain.com
mysqldump -h 192.168.1.4 -udba -ppassword -c --add-drop-table --add-locks --create-options --databases --quick --lock-tables $domainname > $domainname.sql
EOF

My problem is that the multiple lines I am "feeding" to ssh don't parse the variables I set before. This is obviously because the variables aren't set on the remote machine, only in my local shell. How could I pass these variables or possibly pass multiple lines to ssh in a different way?

  • Why not do it the other way as in echo "cmd $var | ssh user@remote bash? – Zoredache Apr 18 '13 at 18:26
2

The way you're escaping the here-doc word is preventing variable substitution. Contrast

cat <<\END
$PATH
$LOGNAME
END

versus

cat <<END
$PATH
$LOGNAME
END

update

On closer inspection, I see you're setting a variable in the heredoc. That should not be expanded on the local machine, so you need to escape those in the mysqldump command. Try this:

ssh "$serveruser"@"$serverip" <<EOF
mkdir -p "$serverpath/mysqldumps/"
cd "$serverpath/mysqldumps/"
domainname=somedomain.com
mysqldump -h 192.168.1.4 -udba -ppassword -c --add-drop-table --add-locks --create-options --databases --quick --lock-tables \$domainname > \$domainname.sql
EOF
  • For some reason I get strange things happening when I don't use the backslash. For example, my shell seems to pause for over a minute and I get an error later on that mysql can't connect to the server, as if the commands were actually running locally and not on the remote ssh server. I have verified that within the here-doc setting the serverpath var works as expected. I need to be able to set out outside of that here-doc though. – senorsmile Apr 18 '13 at 18:34
  • 1
    What's happening is that you're logging on to the remote server, running bash non-interactively, and that exits right away. Then the commands in the heredoc get executed. Simply remove "/bin/bash" from the ssh command and it will execute the heredoc remotely. – glenn jackman Apr 18 '13 at 18:41
  • I added /bin/bash to ssh because without it I get the error: Pseudo-terminal will not be allocated because stdin is not a terminal. That error doesn't seem to actually break anything though. – senorsmile Apr 18 '13 at 18:45
  • Use the ssh options -t or -T to force/disable a pseudo-tty, depending on your needs. That will at least take care of the error message. – glenn jackman Apr 18 '13 at 19:27
  • Thanks for the tip about -T. At first I thought removing /bin/bash solved my problems... but on further testing it's still not expanding the variables within the heredoc. – senorsmile Apr 18 '13 at 21:36
0

You need to quote the whole command.. and no need to quote the vars like that put them like ${var}

serveruser="root"
serverip=1.2.3.4
serverpath="/var/www"


ssh ${serveruser}@${serverip} "/bin/bash <<\EOF
mkdir -p ${serverpath}/mysqldumps/
cd ${serverpath}/mysqldumps/
EOF"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.