2

I have the following ssh script:

#Here I am getting the mysql password for the root user password=$(cat /root/.my.cnf | grep "password" | awk -F\" '{print $2}') #here I am trying to dump the database remotely mysqldump -uroot -p$password $db | ssh root@$destination_server "cat > /backup/mysql/$db.sql" 

The thing is that I am getting the following error:

/bin/bash: -c: line 0: syntax error near unexpected token `)' /bin/bash: -c: line 0: `mysqldump -uroot -pbyt)uy6 database_name ' 

I have tested quite a lot and I found that the error is caused due to the special character in the password. Since I am getting the password from the .my.cnf file I am unable to escape it by hardcoding the escape characters.

Can anyone offer me a way to maybe search for those special characters in the $password variable and add to those escape slashes ("\").

EDIT: I forgot to mention that I am executing this via OpenVZ partial virtualization by using the vzctl exec command which is the reason why I needed a method which will allow me to escape the special characters as I have requested. The method I have developer was a simple use of the sed command:

sed 's/[!-+]/\\&/g' 

Thank you all for your help.

Best Regards!

1
  • 4
    Parsing the my.cnf in a shell scripts is not needed when the credentials are set up correctly in the first place, mysqldump can read them directly from the file. Second if your version is greater than 5.6 you don't even have to store the password as plain text. encrypted passwords are supported as well Commented Feb 24, 2015 at 12:36

1 Answer 1

1

You don't need to do any of this. As HBruijn mentioned in a comment, the $HOME/.my.cnf file already contains your credentials and MySQL's command line tools will use them automatically without you needing to do anything special.

So just do:

mysqldump $db | ssh root@$destination_server "cat > /backup/mysql/$db.sql" 
5
  • Typically yes, you are correct. However I am executing this via partial virtualization OpenVZ with a vzctl exec command and believe me this thing cannot use special characters. Commented Feb 25, 2015 at 13:37
  • @SimeonMitev I don't see that that is relevant at all. Did you forget to mention something important in your question? Commented Feb 25, 2015 at 13:39
  • Actually this is the reason why the issue is there. If you read carefully my post you will understand that I am not looking for a suggestion on how to improve the mysqldump command but instead simply a way to add that escaping slash. Thank you! Commented Feb 25, 2015 at 13:43
  • @SimeonMitev No, that does not explain why you are attempting to parse the .my.cnf file! It looks entirely unnecessary. Commented Feb 25, 2015 at 13:45
  • I cannot dump a mysql database directly via the mysqldump command because I am executing this outside the virtual container @Michael. This is causing for the mysqldump command to end execution with errors mostly caused by some bug in the OpenVZ VM. This is the reason why I need to add the -u and -p options to the command. I would like to assure you that this issue is not with the mysqldump command and your suggestion is correct when it comes for dumping the database on the virtual machine. Commented Feb 25, 2015 at 13:49

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.