I'm writing a script to restore Master-Slave replication on a set of servers. Lost in bash syntax trying to assign a local variable with a result of a remotely ran command substitution with local values:
function doRemote() {
ssh s1.domain.com <<ENDSSH
mysql -u root -pXXX --execute="DROP DATABASE db; CREATE DATABASE db;"
mysql -u root -pXXX --database=db < $WORKDIR$FILENAME
sudo rm -rf /var/log/mysql/db-bin.*
mysql -u root -pXXX --execute="FLUSH LOGS;"
CURRENT_LOG=`mysql -u root -pXXX --execute="SHOW MASTER STATUS" -AN | awk '{print $1}'`
CURRENT_POS=`mysql -u root -pXXX --execute="SHOW MASTER STATUS" -AN | awk '{print $2}'`
# ...
ENDSSH
}
The two lines assigning CURRENT_* variables are the problem: the mysql -u... command gets executed locally, instead of remote session.
Please advice how to run that remotely, assigning the local variable with a result of a remote mysql command.
CURRENT_LOG=$(ssh s1.somain.com "mysql -u root -pXXX --execute=\"SHOW MASTER STATUS\" -AN | awk '{print \$1}'")