我正在打算编写一个脚本,我打算每天早上1点由cron安排备份mySql数据库。
通常我用它来转储数据库:
mysqldump --no-create-db --single-transaction myDB | gzip > ~/my_backup.sql.gz
在我的脑海里,我所写的应该是:
#!/bin/bash
# This script will run each night to backup
# the mySql DB then will upload to Amazon S3
DB_NAME="myDB"
S3_BUCKET="my.s3.bucket"
BACKUP_PATH="~/backups/${DB_NAME}.sql.gz"
mysqldump --no-create-db --single-transaction ${DB_NAME} 2> database.err | gzip > ${BACKUP_PATH}
if [ "$?" -eq 0 ]
then
echo "Database dump complted sucessuflly... wtiting to S3"
aws s3 cp ${BACKUP_PATH} s3://${S3_BUCKET}/
if [ "$?" -eq 0 ]
then
echo "Backup sucessfully written to S3"
else
echo "Error writing to S3"
fi
else
echo "Mysqldump encountered a problem look in database.err for information"
fi
脚本正在做的是进入mysqldump行,但无法区分我指定DB的参数和2> (文件描述符我认为是术语?)。这是错误:
./backup-script: line 12: ~/backups/myDB.sql.gz: No such file or directory
mysqldump: Got error: 1049: Unknown database 'myDB 2' when selecting the database
Mysqldump encountered a problem look in database.err for information
任何人都可以建议这里发生的事情/我做错了什么。
答案 0 :(得分:1)
首先尝试输入数据库名称
mysqldump "${DB_NAME}" --no-create-db --single-transaction