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?

gtludwig
  • 873
  • 3
  • 12
  • 22

1 Answers1

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

Kusalananda
  • 333,661