简体   繁体   中英

How to escape mysqldump quotes in bash?

I need to take a backup of my database using mysqldump tool. My test table has a TIMESTAMP column that I have to use as filter. I'm using a bash script with the following code:

#!/bin/bash
cmd="mysqldump --verbose --opt --extended-insert --result-file /home/backups/mysql/test/test_table.20161205.bak test --where=\"timestamp_c>='2016-12-03 00:00:00'\" --tables \"test_table\""
echo $cmd
$cmd

I'm printing the command that I assume should work. The above script produces the output:

mysqldump --verbose --opt --extended-insert --result-file /home/backups/mysql/test/test_table.20161205.bak test --where="timestamp_c>='2016-12-03 00:00:00'" --tables "test_table"

If I copy the printed command on the terminal, it works; however, the command on the script print the error:

mysqldump: Couldn't find table: "00:00:00'""

Is there something that I'm not understanding about quotes escape?

Variables hold data, not code. Define a function instead.

cmd () {
  mysqldump --verbose --opt --extended-insert \
            --result-file /home/backups/mysql/test/test_table.20161205.bak test \
            --where="timestamp_c>='2016-12-03 00:00:00'" \
            --tables "test_table"
}

Try executing your command using 'eval'. Example:

#!/bin/bash
cmd="mysqldump --verbose --opt --extended-insert --result-file /home/backups/mysql/test/test_table.20161205.bak test --where=\"timestamp_c>='2016-12-03 00:00:00'\" --tables \"test_table\""
eval $cmd

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM