在 bash 脚本中捕获 mysqldump 错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26363230/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 11:33:41  来源:igfitidea点击:

Catching mysqldump error in bash script

bashshellerror-handlingmysqldump

提问by 0Neji

I'm currently trying to write a script that backs up several of my sites along with their databases.

我目前正在尝试编写一个脚本来备份我的几个站点及其数据库。

All is working well but I'm trying to be a little cleverer with my log - basically, at the moment it will attempt to run mysqldump and will echo success or failure.

一切都运行良好,但我试图对我的日志更聪明一点 - 基本上,目前它会尝试运行 mysqldump 并会回应成功或失败。

What I'd rather do is output the actual error rather than just an unhelpful 'mysqldump failed' message. I've had a look around and I'm almost able to do this how I want using "2> log_file.txt"

我宁愿做的是输出实际错误,而不仅仅是无用的“mysqldump 失败”消息。我环顾四周,我几乎可以使用“2> log_file.txt”按照我想要的方式执行此操作

My original command:

我的原始命令:

mysqldump -u asdsa --password=$DB_PASS $DB_NAME > $MYSQL_LOCATION

if [ "$?" -eq 0 ]
then
    echo -e "mysqldump successfully finished at $(date +'%d-%m-%Y %H:%M:%S')"$'\r' >> "$LOG_LOCATION"
else
    echo -e "mysqldump failed at $(date +'%d-%m-%Y %H:%M:%S')"$'\r' >> "$LOG_LOCATION"
fi

So I added "2> $LOG_LOCATION |" to catch the actual error. So my updated command looks like this (this addition is in the first line):

所以我添加了“2> $LOG_LOCATION |” 捕捉实际错误。所以我更新的命令看起来像这样(这个添加在第一行):

mysqldump -u $DB_USER--password=$DB_PASS $DB_NAME 2> $LOG_LOCATION | > $MYSQL_LOCATION

if [ "$?" -eq 0 ]
then
    echo -e "mysqldump successfully finished at $(date +'%d-%m-%Y %H:%M:%S')"$'\r' >> "$LOG_LOCATION"
else
    echo -e "mysqldump failed at $(date +'%d-%m-%Y %H:%M:%S')"$'\r' >> "$LOG_LOCATION"
fi

This outputs the error to my log file but overwrites anything in there. It also means that my error checking if statement doesn't pick up the error.

这会将错误输出到我的日志文件,但会覆盖其中的任何内容。这也意味着我的错误检查 if 语句没有发现错误。

Is there any way that I can:

有什么办法可以:

a) catch the actual error from the mysqldump function (i.e. access denied for this user) b) append this error to an existing error log c) also append a success or fail message after the above error has outputted

a) 从 mysqldump 函数中捕获实际错误(即该用户拒绝访问) b) 将此错误附加到现有错误日志 c) 在上述错误输出后还附加成功或失败消息

Any help would be great!

任何帮助都会很棒!

Thanks!

谢谢!

回答by tripleee

So I added "2> $LOG_LOCATION |"

所以我添加了“2> $LOG_LOCATION |”

This is an error -- the pipe character is superfluous, and changes the meaning from a simple redirection into a pipeline.

这是一个错误——管道字符是多余的,并将含义从简单的重定向更改为管道。

Anyway, the proper way to use ifis to run the actual command whose success you want to check as the condition.

无论如何,正确的使用方法if是运行您要检查其成功作为条件的实际命令。

if mysqldump -u "$DB_USER" --password="$DB_PASS" "$DB_NAME" 2>"$LOG_LOCATION" >"$MYSQL_LOCATION"
then
    echo -e "mysqldump successfully finished at $(date +'%d-%m-%Y %H:%M:%S')"$'\r' >> "$LOG_LOCATION"
else
    echo -e "mysqldump failed at $(date +'%d-%m-%Y %H:%M:%S')"$'\r' >> "$LOG_LOCATION"
fi

You are probably better off with printfover echo -ebut I won't change those parts.

你可能有更好printfecho -e,但我不会改变的部分。

回答by Géza T?r?k

a) You can catch the error output in a variable like this:

a) 您可以在这样的变量中捕获错误输出:

somevar="$( ( mysqldump -u $DB_USER --password=$DB_PASS $DB_NAME > $MYSQL_LOCATION ) 2>&1 )"

b) You may examine the contents of this variable, display a message as usual, and

b) 你可以检查这个变量的内容,像往常一样显示一条消息,然后

c) You can easily forward it into your log file using echo "$somevar" >> $LOG_LOCATION

c) 您可以使用以下命令轻松将其转发到您的日志文件中 echo "$somevar" >> $LOG_LOCATION

Regards

问候