0

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?

anubhava
  • 761,203
  • 64
  • 569
  • 643
Takumi
  • 67
  • 1
  • 8

2 Answers2

2

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"
}
chepner
  • 497,756
  • 71
  • 530
  • 681
-1

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
yogur
  • 810
  • 10
  • 19
  • Wont 'eval' evaluate the value of the string, then execute it? I can't see the reason for the downvote. Please offer an explanation. – yogur Dec 05 '16 at 17:32
  • Variables are not meant to contain code. One can use a function instead. – Andreas Louv Dec 05 '16 at 17:36
  • @andlrc That's true, still I simply offered an answer on Takumi's specific question. – yogur Dec 05 '16 at 17:38
  • If you know that's the case you should suggest OP to use a different approach. Now you learned OP a to use a tool that is not the correct for the job. – Andreas Louv Dec 05 '16 at 17:41
  • 'eval' works for me. However I'm only try this answer thinking that's a good approach, without read about it first. Now I'm looking for a better way. – Takumi Dec 05 '16 at 17:53
  • @Takumi I've just learned that it's not the best approach. I usually use it a lot myself. Please see this answer: http://stackoverflow.com/questions/17529220/why-should-eval-be-avoided-in-bash-and-what-should-i-use-instead – yogur Dec 05 '16 at 17:56
  • @Takumi The following popular answer suggests using 'eval', which is a bit confusing. http://stackoverflow.com/questions/2005192/how-to-execute-a-bash-command-stored-as-a-string-with-quotes-and-asterisk – yogur Dec 05 '16 at 18:05
  • @andlrc I've read the links and you're right, it's confusing and of course, there are hidden bad things behind 'eval' that I didn't know. Thanks for the info. – Takumi Dec 05 '16 at 19:56