开发者

Script throwing unexpected operator when using mysqldump

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 dat开发者_StackOverflow中文版abases 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


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜