James McMahon
James McMahon

Reputation: 49629

Prompting for MySQLDump password in a bash script?

I'm trying to write a bash script that runs a mysqldump command that uses the -p flag. This flag prompts the user for a password, which works as expected when run in the shell directly, but does not appear when run in a script.

#!/usr/bin/env 

ssh [email protected] 'mysqldump -u mysqluser -p --databases foo | bzip2' > ~/temp/foo-dump.sql.bz2

Now I could embed the password in the script or pass it as an arguments, but I really want the script to prompt the user for the password so the password doesn't show up in my scripts repo or in my bash history.

Anyone have any idea on how to accomplish this?

Upvotes: 2

Views: 1758

Answers (1)

msb
msb

Reputation: 4408

This should do the trick:

read -p "mysql password: " PASS && ssh [email protected] 'mysqldump -u mysqluser -p'$PASS' --databases foo | bzip2' > foo-dump.sql.bz2 ; PASS=""

In this case, you will first enter the mysql password, and then be prompted for the ssh password. Note that the mysql password will not be hidden, i.e., someone can read it over your shoulder. If you want to avoid that, use the flag -s

read -s -p "mysql password: " PASS && ...

Note also that there mustn't be any space between the "p" (in -p for password) and the quotation mark for the password variable.

Also, your shebang is not specifying which interpreter to use, which might be a problem. I'd suggest you use #!/bin/bash or #!/usr/bin/env bash.

Upvotes: 1

Related Questions