I am writing a mysqldump bash backup script, but can’t get it to escape the password correctly.
At the top if have:
PLESKPASS=( `cat /etc/psa/.psa.shadow` )
Using ‘set -x’ I can see this returns:
+ echo '$AES-128-CBC$ssdvgdfvgRipfbUZcg==$Mi3asdNOTREALddnerpTTzVhYBgKriyBA='
$AES-128-CBC$ssdvgdfvgRipfbUZcg==$Mi3asdNOTREALddnerpTTzVhYBgKriyBA=
Which is fine.
My command has the following:
COMMAND='mysqldump -R -a --single-transaction -uadmin -p'$PLESKPASS' '$DB' > mysqldumps/'$DB'.sql'
The $DB string is returned correctly, however the Plesk password isn’t, it runs as:
COMMAND='mysqldump -R -a --single-transaction -uadmin -p$AES-128-CBC$ssdvgdfvgRipfbUZcg==$Mi3asdNOTREALddnerpTTzVhYBgKriyBA= phpmyadmin_5IAf0u4U5vXa > mysqldumps/phpmyadmin_5IAf0u4U5vXa.sql'
Which looks ok, but doesn’t work without the password being enclosed in quotes.
Ive tried many different combinations to get this to work. Using ”’ before and after, doubles quotes here and there, “‘”, “‘” etc to no avail and it’s slowly driving me insane as I’m sure its something simple, but what you don’t know will always seem complicated!
UPDATE:
I played around with the code some more, and through trial and error and trying each of these:
PLESKPASS="$(</etc/psa/.psa.shadow)"
PLESKPASS=$(eval echo -e `</etc/psa/.psa.shadow`)
PLESKPASS=( `cat /etc/psa/.psa.shadow`
Which all returned the password correctly, I noticed someone used $() and on a whim tried that before my command, so it went from:
COMMAND=’mysqldump -R -a –single-transaction -uadmin -p’$PLESKPASS’
‘$DB’ > mysqldumps/’$DB’.sql’
to
COMMAND=$(mysqldump -R -a –single-transaction -uadmin -p$PLESKPASS
$DB > mysqldumps/$DB.sql)
And it gives the desired result! I am still none the wiser, many thanks for everyones help.
3
Answers
Your first command is creating an array.
Try this:
Try
Using
"
should keep the'
in the string.Double quotes around the whole variable, single quotes around the passwords work:
prints out