我在网上找到了以下脚本,它可以备份数据库并将其上传到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.sh
(带sudo crontab -e
),也许我应该使用crontab -e
并将cron更改为0 10 * * * sudo sh /var/www/magicpin/backup/backup.sh
提前致谢!
编辑:只是为了澄清脚本第一次工作的原因。它看起来是原始脚本(我稍后修改了一下,有一行改变了目录的权限)。所以,当我第一次运行时,我可以通过./backup.sh,来自用户Ubuntu。下次,因为权限已更改,这让我“被拒绝”,所以当我开始尝试使用sh
和权限和内容时。
答案 0 :(得分:5)
你的bash
脚本似乎很好,我相信这是你调用它的方式。 sh
和bash
是不同的shell。尝试像这样运行它:
sudo ./backup.sh
或
sudo bash ./backup.sh
而不是
sudo sh ./backup.sh