用于进行数据库备份的Shell脚本仅适用于提示

时间:2016-08-29 15:47:54

标签: linux shell ubuntu-14.04

我有一个shell脚本,可以在命令提示符下正常工作,但是从我的cron作业中它会抛出unexpected operator

BACKUP=/var/www/backups/mysql
NOW=$(date +"%Y%m%d-%H%M")
MUSER="root"
MPASS="secr3T"
MHOST="localhost"
MYSQL="$(which mysql)"
MYSQLDUMP="$(which mysqldump)"
GZIP="$(which gzip)"

find $BACKUP/* -mtime +15 -exec rm {} \;

DBS="$($MYSQL -h $MHOST -u $MUSER -p$MPASS -Bse 'show databases')"
for db in $DBS
do

if [ "$db" == "myDBase" ]; then
    if [ "$db" != "performance_schema" ]; then
        FILE=$BACKUP/mysql_backup_$db.$NOW.tar.gz
        $MYSQLDUMP -h $MHOST -u $MUSER -p$MPASS $db | $GZIP -9 > $FILE
        echo $db
        fi
    fi
done

我正在使用crontab 20 0 * * * var/www/backups/backup.site.sh

访问该脚本

我得到的错误是:

/var/www/backups/backup.mysql.sh: 20: [: information_schema: unexpected operator
/var/www/backups/backup.mysql.sh: 20: [: mysql: unexpected operator
/var/www/backups/backup.mysql.sh: 20: [: olb: unexpected operator
/var/www/backups/backup.mysql.sh: 20: [: performance_schema: unexpected operator

我在Ubuntu 14.04上运行最新版本的mySQL。 Doe有谁知道为什么?

1 个答案:

答案 0 :(得分:2)

如果没有指定shell,默认情况下会得到/ bin / sh(在Ubuntu 14.04上测试)。 sh内置的[不支持==运算符,因此sh不知道如何比较第20行的$dbmyDBase

您应该始终使用"#!"用于指定shell的行。要获得bash,第一行将是#!/bin/bash