views:

131

answers:

2

Question Rewritten:

HOMEDIR="ftpuser"
REMOTEIP="1.1.1.1"
MYSQLPASS="password"

Q1="DROP USER "$HOMEDIR"_shop;"
Q2="DROP DATABASE "$HOMEDIR"_shop;"
Q3="CREATE DATABASE IF NOT EXISTS "$HOMEDIR"_shop;"
Q4="GRANT ALL ON "$HOMEDIR"_shop TO '"$HOMEDIR"_shop'@'localhost' IDENTIFIED BY '$MYSQLPASS';"
Q5="GRANT ALL ON "$HOMEDIR"_shop TO '"$HOMEDIR"_shop'@'anotherip' IDENTIFIED BY '$MYSQLPASS';"
# Need to grant permissions from another server as well
Q6="FLUSH PRIVILEGES;"
SQL="${Q1}${Q2}${Q3}${Q4}${Q5}${Q6}"

echo $SQL
echo " "
ssh -p 8899 root@$REMOTEIP "mysql -u root -p "$SQL""

I then run:

/root/testing/migratesite.sh

And get:

bash: DROP: command not found
bash: CREATE: command not found
bash: GRANT: command not found
bash: GRANT: command not found
bash: FLUSH: command not found

What am I missing?

+2  A: 

You are missing quotes and a proper mysql client command line:

ssh -p 8899 root@$REMOTEIP "mysql -u root -p -e \"$SQL\""

You need to escape the quotes around the $SQL variable so they get passed to the remote shell, else they get interpreted by the local shell (that's why you get DROP: command not found, the semi colon is interpreted by the shell.) Also, to have the mysql client to execute a command you have to pass the -e command line option.

Vinko Vrsalovic
A: 

Did you try this:

ssh -p 8899 root@$REMOTEIP "echo \"$SQL\" | mysql -u root --password=$SQL_PASS"
Pascal
This will fail as well due to lack of quoting. This has the added problem that mysql client will interpret the SQL as the password.
Vinko Vrsalovic
Yeah, should have changed `-p` to `--password=$PASS_VARIABLE`. Corrected.
Pascal
As long as $PASS_VARIABLE gets read from the arguments (and not stored within the script) that should be fine.
Vinko Vrsalovic