0

I'm trying to build a script to automate the backup process for an existing database.

The problem I'm having is passing to the mysqldump command (see bellow) the database password that has two words separated by a blank space.

# database credentials
DATABASEHOST="localhost"
DATABASEUSER="username"
DATABASEPASSWORD="Compound Password"
DATABASESCHEMA="schema"

# Local directory of mysqldump file
LOCALDIR=/home/user/db-bkp

#--- end config

mysqldump --host=$DATABASEHOST --user=$DATABASEUSER --password=$DATABASEPASSWORD $DATABASESCHEMA > $LOCALDIR/`date +%Y%m%d`_bkp_$DATABASESCHEMA.sql

When I run the script, the relevant mysqldump related message is:

mysqldump: Got error: 1045: "Access denied for user 'username'@'localhost' (using password: YES)" when trying to connect

What am I doing wrong here and how to solve it?

5

Quote your variables:

mysqldump --host="$DATABASEHOST" \
          --user="$DATABASEUSER" \
          --password="$DATABASEPASSWORD" \
          --database="$DATABASESCHEMA" \
  > "$LOCALDIR"/"$(date +%Y%m%d)_bkp_$DATABASESCHEMA.sql"

See also: Security implications of forgetting to quote a variable in bash/POSIX shells

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.