使用 mysqldump 时脚本抛出意外运算符
将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我最好的猜测是您没有 shebang 行或者默认 shell 已更改。你的问题出在这一行:
和/或这一行:
Bash 对单方括号内的
==
非常满意,但例如 Dash 则不然。将这些行更改为使用单个等号:和:
或将 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:
and/or this one:
Bash is perfectly happy with
==
inside single square brackets, but Dash, for example, is not. Change those lines to use single equal signs:and:
or change your shebang to
#!/bin/bash
.我的猜测是,您的
$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 withfor 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.