I am running mariadb in a docker container and I am try to back it up via cron and a bash script. I can't figure out the correct syntax of the ' and "

This is what I want to do, this works when I insert it into my terminal:

docker exec mariadb sh -c 'exec mysqldump --single-transaction -u myDb_user -p"secret" myDb' > tmp.sql

This is the line of code which causes the error in my bash script (should do the exact same thing than the line above, but in a bash script and with a few variables (need to back up multiple things):

docker exec mariadb sh -c 'exec mysqldump --single-transaction -u ${dbUser} -p"${dbPassword}" ${database}' > "${backupdir}/${fileNameBackupDb}"

The output when I run the bash script (via sudo ./backup.sh)

Usage: mysqldump [OPTIONS] database [tables]
OR     mysqldump [OPTIONS] --databases [OPTIONS] DB1 [DB2 DB3...]
OR     mysqldump [OPTIONS] --all-databases [OPTIONS]
For more options, use mysqldump --help
  • I edited my question, thanks for the feedback @dessert
    – TMaddox
    Apr 6 '20 at 5:56
  • 1
    Are those variables defined inside the container? You have used single quotes, your script isn't going to expand them
    – muru
    Apr 6 '20 at 5:57
  • Updated my question, please have a look, thanks! @muru
    – TMaddox
    Apr 6 '20 at 7:19

I figured it out, here is the correct syntax:

docker exec mariadb sh -c "exec mysqldump --single-transaction -u ${dbUser} -p'"${dbPassword}"' ${database}" > "${backupdir}/${fileNameBackupDb}"

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.