使用 mysqldump 时脚本抛出意外运算符

发布于 2024-10-24 23:31:55 字数 1805 浏览 1 评论 0原文

将 Debian 机器升级到 6.0 Squeeze 后,我用来备份 MySQL 数据库的脚本的一部分已停止正常工作。我已经通过 CLI 测试了备份代码,它工作正常。我相信这是在备份发生之前选择数据库的过程,可能与 $skipdb 变量有关。如果有更好的方法来执行该功能,那么我会尝试新的东西。任何见解将不胜感激。

$ sudo ./script.sh
[: 138: information_schema: unexpected operator
[: 138: -1: unexpected operator
[: 138: mysql: unexpected operator
[: 138: -1: unexpected operator

这里使用 bash -x script 是迭代之一:

+ for db in '$DBS'
+ skipdb=-1
+ '[' test '!=' '' ']'
+ for i in '$IGGY'
+ '[' mysql == test ']'
+ :
+ '[' -1 == -1 ']'
++ /bin/date +%F
+ FILE=/backups/hostname.2011-03-20.mysql.mysql.tar.gz
+ '[' no = yes ']'
+ /usr/bin/mysqldump --single-transaction -u root -h localhost '-ppassword' mysql
+ /bin/tar -czvf /backups/hostname.2011-03-20.mysql.mysql.tar.gz mysql.sql
mysql.sql
+ rm -f mysql.sql

这是代码。

if [ $MYSQL_UP = "yes" ]; then
echo "MySQL DUMP" >> /tmp/update.log
echo "--------------------------------" >> /tmp/update.log
DBS="$($MYSQL -u $MyUSER -h $MyHOST -p"$MyPASS" -Bse 'show databases')"
for db in $DBS
do
    skipdb=-1
    if [ "$IGGY" != "" ] ;
    then
        for i in $IGGY
        do
            [ "$db" == "$i" ] && skipdb=1 || :
        done
    fi
    if [ "$skipdb" == "-1" ] ;
    then
        FILE="$DEST$HOST.`$DATE +"%F"`.$db.mysql.tar.gz"
        if [ $ENCRYPT = "yes" ]; then
            $MYSQLDUMP -u $MyUSER -h $MyHOST -p"$MyPASS" $db > $db.sql && $TAR -czvf - $db.sql | $OPENSSL enc -aes-256-cbc -salt -out $FILE.enc -k $ENC_PASS && rm -f $db.sql
        else
            $MYSQLDUMP --single-transaction -u $MyUSER -h $MyHOST -p"$MyPASS" $db > $db.sql && $TAR -czvf $FILE $db.sql && rm -f $db.sql
        fi
    fi
done

A portion of a script I use to backup MySQL databases has stopped working correctly after upgrading a Debian box to 6.0 Squeeze. I have tested the backup code via CLI and it works fine. I believe it is in the selection of the databases before the backup occurs, possibly something to do with the $skipdb variable. If there is a better way to perform the function then I'm will to try something new. Any insight would be greatly appreciated.

$ sudo ./script.sh
[: 138: information_schema: unexpected operator
[: 138: -1: unexpected operator
[: 138: mysql: unexpected operator
[: 138: -1: unexpected operator

Using bash -x script here is one of the iterations:

+ for db in '$DBS'
+ skipdb=-1
+ '[' test '!=' '' ']'
+ for i in '$IGGY'
+ '[' mysql == test ']'
+ :
+ '[' -1 == -1 ']'
++ /bin/date +%F
+ FILE=/backups/hostname.2011-03-20.mysql.mysql.tar.gz
+ '[' no = yes ']'
+ /usr/bin/mysqldump --single-transaction -u root -h localhost '-ppassword' mysql
+ /bin/tar -czvf /backups/hostname.2011-03-20.mysql.mysql.tar.gz mysql.sql
mysql.sql
+ rm -f mysql.sql

Here is the code.

if [ $MYSQL_UP = "yes" ]; then
echo "MySQL DUMP" >> /tmp/update.log
echo "--------------------------------" >> /tmp/update.log
DBS="$($MYSQL -u $MyUSER -h $MyHOST -p"$MyPASS" -Bse 'show databases')"
for db in $DBS
do
    skipdb=-1
    if [ "$IGGY" != "" ] ;
    then
        for i in $IGGY
        do
            [ "$db" == "$i" ] && skipdb=1 || :
        done
    fi
    if [ "$skipdb" == "-1" ] ;
    then
        FILE="$DEST$HOST.`$DATE +"%F"`.$db.mysql.tar.gz"
        if [ $ENCRYPT = "yes" ]; then
            $MYSQLDUMP -u $MyUSER -h $MyHOST -p"$MyPASS" $db > $db.sql && $TAR -czvf - $db.sql | $OPENSSL enc -aes-256-cbc -salt -out $FILE.enc -k $ENC_PASS && rm -f $db.sql
        else
            $MYSQLDUMP --single-transaction -u $MyUSER -h $MyHOST -p"$MyPASS" $db > $db.sql && $TAR -czvf $FILE $db.sql && rm -f $db.sql
        fi
    fi
done

fi

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

画中仙 2024-10-31 23:31:55

我最好的猜测是您没有 shebang 行或者默认 shell 已更改。你的问题出在这一行:

[ "$db" == "$i" ] && skipdb=1 || :

和/或这一行:

if [ "$skipdb" == "-1" ]

Bash 对单方括号内的 == 非常满意,但例如 Dash 则不然。将这些行更改为使用单个等号:

[ "$db" = "$i" ] && skipdb=1 || :

和:

if [ "$skipdb" = "-1" ]

或将 shebang 更改为 #!/bin/bash

My best guess is that you don't have a shebang line or that the default shell has changed. Your problem is in this line:

[ "$db" == "$i" ] && skipdb=1 || :

and/or this one:

if [ "$skipdb" == "-1" ]

Bash is perfectly happy with == inside single square brackets, but Dash, for example, is not. Change those lines to use single equal signs:

[ "$db" = "$i" ] && skipdb=1 || :

and:

if [ "$skipdb" = "-1" ]

or change your shebang to #!/bin/bash.

抠脚大汉 2024-10-31 23:31:55

我的猜测是,您的 $MYSQL 不会返回您期望的值,而是打印一些您使用 for db in $DBS 进行迭代的错误消息;做...完成

尝试手动运行 $MYSQL -u $MyUSER -h $MyHOST -p"$MyPASS" -Bse 'showdatabases' 命令。

另外,bash -x script 也是您的朋友。

My guess is that your $MYSQL does not return the value you expect, but print some error message which you iterate over in with for db in $DBS ; do ... done.

Try to run the $MYSQL -u $MyUSER -h $MyHOST -p"$MyPASS" -Bse 'show databases' command manually.

Also bash -x script is your friend here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文