根据本文我编写了 cron 作业来备份数据库,如果我在命令行上手动运行它,它会正常工作。但如果脚本作为 cron 作业运行,它会失败并出现错误:文本文件繁忙有人能告诉我我做错了什么吗?
#!/bin/bash
cd /root/tmp/
TIMESTAMP=$(date +"%Y-%m-%d-%H-%M")
BACKUP_DIR="/root/backups/databases/$TIMESTAMP"
MYSQL=/usr/bin/mysql
MYSQL_USER="root2"
MYSQL_PASSWORD="xxxxxxxxx"
MYSQLDUMP=/usr/bin/mysqldump
mkdir -p "$BACKUP_DIR"
databases=`$MYSQL --user=$MYSQL_USER -p$MYSQL_PASSWORD -e "SHOW DATABASES;" | grep -Ev "(Database|information_schema|performance_schema)"`
for db in $databases; do
$MYSQLDUMP --force --opt --user=$MYSQL_USER -p$MYSQL_PASSWORD --databases $db | gzip > "$BACKUP_DIR/$db.gz"
done
这是扩展的错误日志:
+ cd /root/tmp/
++ date +%Y-%m-%d-%H-%M
+ TIMESTAMP=2020-06-15-19-28
+ BACKUP_DIR=/root/backups/databases/2020-06-15-19-28
+ MYSQL=/usr/bin/mysql
+ MYSQL_USER=root2
+ MYSQL_PASSWORD=xxxxxxxxxxxxxx
+ MYSQLDUMP=/usr/bin/mysqldump
+ mkdir -p /root/backups/databases/2020-06-15-19-28
++ /usr/bin/mysql --user=root2 -pxxxxxxxxxx -e 'SHOW DATABASES;'
++ grep -Ev '(Database|information_schema|performance_schema)'
mysql: [Warning] Using a password on the command line interface can be insecure.
+ databases='mysql
phpmyadmin
sys
tatrytec'
+ for db in $databases
+ /usr/bin/mysqldump --force --opt --user=root2 -pxxxxxxxxxxx --databases mysql
+ gzip
mysqldump: [Warning] Using a password on the command line interface can be insecure.
+ for db in $databases
+ /usr/bin/mysqldump --force --opt --user=root2 -pxxxxxxxxxxx --databases phpmyadmin
+ gzip
mysqldump: [Warning] Using a password on the command line interface can be insecure.
+ for db in $databases
+ gzip
+ /usr/bin/mysqldump --force --opt --user=root2 -pxxxxxxxxxxx --databases sys
mysqldump: [Warning] Using a password on the command line interface can be insecure.
+ for db in $databases
+ /usr/bin/mysqldump --force --opt --user=root2 -pxxxxxxxxxxx --databases tatrytec
mysqldump: [Warning] Using a password on the command line interface can be insecure.
+ gzip