0

我在网上找到了以下脚本,它可以备份数据库并将其上传到 S3 存储桶:

#!/bin/bash
# Shell script to backup MySql database

# CONFIG - Only edit the below lines to setup the script
# ===============================

MyUSER="test"           # USERNAME
MyPASS="test"       # PASSWORD
MyHOST="localhost"      # Hostname

S3Bucket="test" # S3 Bucket

# DO NOT BACKUP these databases
IGNORE="information_schema mysql performance_schema phpmyadmin"

# DO NOT EDIT BELOW THIS LINE UNLESS YOU KNOW WHAT YOU ARE DOING
# ===============================

# Linux bin paths, change this if it can not be autodetected via which command
MYSQL="$(which mysql)"
MYSQLDUMP="$(which mysqldump)"
CHOWN="$(which chown)"
CHMOD="$(which chmod)"
GZIP="$(which gzip)"

# Backup Dest directory, change this if you have someother location
DEST="/var/www/test/backup"

# Main directory where backup will be stored
MBD="$DEST/mysql-$(date +"%d-%m-%Y")"

# Get hostname
HOST="$(hostname)"

# Get data in dd-mm-yyyy format
NOW="$(date +"%d-%m-%Y")"

# File to store current backup file
FILE=""

# Store list of databases
DBS=""

[ ! -d $MBD ] && mkdir -p $MBD || :

# Get all database list first

if [ "$MyPASS" == "" ];
then
  DBS="$($MYSQL -u $MyUSER -h $MyHOST -Bse 'show databases')"
else
  DBS="$($MYSQL -u $MyUSER -h $MyHOST -p$MyPASS -Bse 'show databases')"
fi

for db in $DBS
do
    skipdb=-1
    if [ "$IGNORE" != "" ];
    then
        for i in $IGNORE
        do
            [ "$db" == "$i" ] && skipdb=1 || :
        done
    fi

    if [ "$skipdb" == "-1" ] ; then
        FILE="$MBD/$db.$HOST.$NOW.gz"
        # dump database to file and gzip
        if [ "$MyPASS" == "" ]; then
          $MYSQLDUMP -u $MyUSER -h $MyHOST $db | $GZIP -9 > $FILE
        else
          $MYSQLDUMP -u $MyUSER -h $MyHOST -p$MyPASS $db | $GZIP -9 > $FILE
        fi
    fi
done

# copy mysql backup directory to S3
s3cmd sync -rv --skip-existing $MBD s3://$S3Bucket/

因此,我添加了一个 cron 作业来运行它并第一次手动运行它,一切正常,备份出现在 S3 存储桶中。但是,从今天开始,当我尝试手动运行它时,我收到了几十个错误:

/var/www/test/backup/backup.sh: 48: [: test: unexpected operator
/var/www/test/backup/backup.sh: 62: [: information_schema: unexpected operator
/var/www/test/backup/backup.sh: 62: [: information_schema: unexpected operator
/var/www/test/backup/backup.sh: 62: [: information_schema: unexpected operator
/var/www/test/backup/backup.sh: 62: [: information_schema: unexpected operator
/var/www/test/backup/backup.sh: 62: [: information_schema: unexpected operator
/var/www/test/backup/backup.sh: 66: [: -1: unexpected operator
/var/www/test/backup/backup.sh: 62: [: mysql: unexpected operator
/var/www/test/backup/backup.sh: 62: [: mysql: unexpected operator
/var/www/test/backup/backup.sh: 62: [: mysql: unexpected operator
/var/www/test/backup/backup.sh: 62: [: mysql: unexpected operator
/var/www/test/backup/backup.sh: 62: [: mysql: unexpected operator
/var/www/test/backup/backup.sh: 66: [: -1: unexpected operator

就像不接受数据库列表或 mysql 的凭据一样。我还没有碰过那些。这可能是什么原因?我尝试再次上传脚本,完全相同的脚本,用普通用户运行它,sudo sh ./backup.sh但都没有成功。我在 Amazon EC2 上使用 Ubuntu,所以可能是有权限的东西导致了这个?(我将其设置为 exectutabe sudo chmod +x backup.sh,尝试使用 chown 777 但均未成功。

我不是 shell 脚本专家,所以这让我很困惑,我知道我在终端上的方式,但无法弄清楚为什么在脚本根本没有改变的情况下会发生这种情况。

cron 命令是:(0 10 * * * /var/www/magicpin/backup/backup.shsudo crontab -e),也许我应该使用crontab -e并将 cron 更改为0 10 * * * sudo sh /var/www/magicpin/backup/backup.sh

提前致谢!

编辑:只是为了澄清为什么脚本第一次起作用。它似乎是原始脚本(我后来对其进行了一些修改,其中有一行更改了目录的权限)。所以,当我第一次运行它时,我可以通过用户 Ubuntu 的 ./backup.sh 来完成它。下一次,因为权限被更改,这让我“权限被拒绝”,所以当我开始尝试sh权限和东西的时候。

4

1 回答 1

5

您的bash脚本似乎很好,我相信这是您调用它的方式。sh并且bash是不同的贝壳。尝试像这样运行它:

sudo ./backup.sh

或者

sudo bash ./backup.sh

代替

sudo sh ./backup.sh
于 2013-07-18T10:36:31.903 回答