简体   繁体   English

使用mysqldump的特殊字符转义密码变量

[英]Escaping out password variable with special characters for mysqldump

I'm trying to create a shell script to automatically backup my whole database.我正在尝试创建一个 shell 脚本来自动备份我的整个数据库。 My script expects the password in a new line because I don't want to store it anywhere.我的脚本需要在新行中输入密码,因为我不想将它存储在任何地方。 Here is my code:这是我的代码:

#!/bin/bash
echo -n Password:
read -s PASS
echo
docker exec db /usr/bin/mysqldump --all-databases -u admin --password='\''$PASS\'' > backup-$(date "+%Y-%m-%d").sql

The problem is, that I need to put the variable into single quotes because of the $ signs in the password, but I can't read variables in single quotes.问题是,由于密码中的 $ 符号,我需要将变量放入单引号中,但我无法读取单引号中的变量。 I know there are several similar questions out there, but I could not solve is with them.我知道那里有几个类似的问题,但我无法解决它们。

So if the password is '$ecretPa$$word', then the actual executed program should be something like this:所以如果密码是'$ecretPa$$word',那么实际执行的程序应该是这样的:

docker exec db /usr/bin/mysqldump --all-databases -u admin --password='$ecretPa$$word' > backup-2020-03-03.sql

I managed to make this work.我设法完成了这项工作。 Maybe the problem was not with the escaping but with the way I tried to run this.也许问题不在于逃避,而在于我试图运行它的方式。 My final code:我的最终代码:

#!/bin/bash
echo -n Password:
read -s PASS
echo
cmd="docker exec db /usr/bin/mysqldump --all-databases -u admin --password='$PASS' > backup-$(date "+%Y-%m-%d").sql"
eval "$cmd";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM