I use the following script to read database credentials from a Wordpress installation:
DB_NAME=$(sed -n "s/define( *'DB_NAME', *'\([^']*\)'.*/\1/p" wp-config.php)
DB_USER=$(sed -n "s/define( *'DB_USER', *'\([^']*\)'.*/\1/p" wp-config.php)
DB_PASSWORD=$(sed -n "s/define( *'DB_PASSWORD', *'\([^']*\)'.*/\1/p" wp-config.php)
DB_HOST=$(sed -n "s/define( *'DB_HOST', *'\([^']*\)'.*/\1/p" wp-config.php)
When I echo the variables like this:
echo $DB_NAME;
echo $DB_USER;
echo $DB_PASSWORD;
echo $DB_HOST;
Everything is fine. When I use them in a mysqldump
statement:
mysqldump --add-drop-table -u${DB_USERNAME} -p${DB_PASSWORD} ${DB_NAME} > ../backups/${SQL_BACKUP} 2>&1
or
mysqldump --add-drop-table -u$DB_USERNAME -p$DB_PASSWORD $DB_NAME > ../backups/$SQL_BACKUP 2>&1
I get this error message:
mysqldump: Got error: 1045: Access denied for user '-8zSkcRrgVad3F'@'localhost' (using password: NO) when trying to connect
When I use the statement as this:
mysqldump --add-drop-table -udatabaseuser -p8zSkcRrgVad3F database > ../backups/test.sql 2>&1
Everything works fine. I have no special chars in my user/database/password, but the command takes the -p8zSkcRrgVad3F
as the username as well? Do I need to trim the values before or something like this?