使用shell做数据库备份的时候,遇到了以下问题,原因未知

时间:2021-11-29 01:39:24

由于对shell不是很精通,遇到了下面的问题。如果有人能给以解答,不胜感激

 

代码如下

#!/bin/bash
#実行タイミング 10 0 1 * * bash /var/www/borg/fuel/app/tasks/db_backup.sh

SAVE_PATH="/var/tmp/"
ymdhms=$(date  %Y%m%d%H%M%S)
dump_file_name="backup_${ymdhms}.dump"

if [[ $BORG_FUEL_BATCH_ENV == "development" ]]
then
    dbuser="root"
    dbpasswd=!Test1234#
    db=test_ph0_dev
elif [[ $TEST_FUEL_BATCH_ENV == "staging" ]]
then
    dbuser="test"
    dbpasswd=Test-123
    db=test_ph0_stg 
else
    dbuser="test"
    dbpasswd=Test-123
    db=test_ph0_prd
fi

echo $TEST_FUEL_BATCH_ENV
backup_command="mysqldump --single-transaction -u ${dbuser} -p‘${dbpasswd}‘ ${db} > ${SAVE_PATH}${dump_file_name}"
echo $backup_command

#command1 result
=`$backup_command`

#command2
#result=$($backup_command)
#command3
#result
=`mysqldump --single-transaction -u ${dbuser} -p"${dbpasswd}" ${db} > ${SAVE_PATH}${dump_file_name}`;

 

conmand1和command2的运行都会报错,运行结果如下。而使用command3却正常运行

development
mysqldump --single-transaction -u root -p!Test1234# test_ph0_dev > /var/tmp/backup_20200203110247.dump          //打印出的 back_command
mysqldump: [Warning] Using a password on the command line interface can be insecure.
mysqldump: Got error: 1045: Access denied for user root@localhost (using password: YES) when trying to connect

 

直接运行打印出的back_command也能正常运行

[[email protected] log]# mysqldump --single-transaction -u root -p!Borg1234# borg_ph0_dev > /var/tmp/backup_20200203110247.dump
mysqldump: [Warning] Using a password on the command line interface can be insecure.

 

不知道这是什么原因,希望有大神能够解答。谢谢!