Christian S.
Christian S.

Reputation: 3

MySQLdump with "--where"-option won't work

I try to build a script that dumpes old Sql-Data, but it just won't work.

Here is the script I've come up with so far:

#!/bin/bash

MAX_AGE="-3 months"    
DB="anyDb"
TABLE="logs"
USER="user"
PASS="pass"

# Get date
MAX_DATE=$(date -d "${MAX_AGE}" +"%Y-%m-%d")

# set Options
OPTIONS="--compact --extended-insert --where=\"created_at < '${MAX_DATE} 00:00:00'\""

mysqldump -u $USER -p$PASS $OPTIONS $DB $TABLE > someOldData.sql

Problem is, that this works perfectly fine when I enter the resulting command in the shell but when running the script, it gives me the following error:

Access denied for user 'user'@'localhost' to database '<' when selecting the database

Is there anything wrong with the escaping of the where-statement of my command?

Upvotes: 0

Views: 361

Answers (1)

Barmar
Barmar

Reputation: 782508

Quotes in shell variables are not processed when expanding it. Use a shell array instead of a single string:

OPTIONS=(--compact --extended --where="created_at < '${MAX_DATE} 00:00:00'")

mysqldump -u $USER -p$PASS "${OPTIONS[@]}" $DB $TABLE > someOldData.sql

See Quotes inside a variable are ignored

Upvotes: 1

Related Questions