Im preparing a shell script which will take mysql table dump with where clause. Im passing table name and where condition from a parameter.

Backup command:

mysqldump -u root-p'pass'  db blocked_reasons --where=" created_time <  DATE(Now() - INTERVAL 2 month)" > test.sql

Run this command with parameters:

where="--where="\""created_time <  DATE(Now() - INTERVAL 2 month)"\"""

verify with echo:

echo " mysqldump -u root -p db $tbl $where > test.sql"
 mysqldump -u root-p db blocked_reasons  --where="created_time <  DATE(Now() - INTERVAL 2 month)" > test.sql

But when I start to execute this command Im getting errors:

mysqldump: Couldn't find table: "<"
  • 1
    I'd get rid of some of the spacing in your query in the --where clause. Take a look at this for an example: stackoverflow.com/questions/5658284/… – ryekayo Nov 27 '18 at 20:11
  • Yeah, seems its spacing issue. But its a sting right, how can I add space inside it? – Bhuvanesh Nov 27 '18 at 20:15
  • I read this on a SO question. Double quotes will corrupt your string. – Bhuvanesh Nov 27 '18 at 20:18
  • Ah yeah, thatll do it.. I was on the fence about suggesting to swap the double quotes to single quotes originally. – ryekayo Nov 27 '18 at 20:19
  • 1
    Never use -p'pass' in your backup scripts. This can be spotted by regular user in /proc/[PID]/cmdline. Instead simply assign your password to MYSQL_PWD variable within script before launching mysqldump – DevilaN Nov 27 '18 at 20:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.