我有一个mysqlbackup脚本,我希望它: - 单独备份所有数据库 - 将任何错误记录到文件中 - 通过电子邮件发送该文件
我现在所拥有的是:
\#!/bin/sh
TIMESTAMP=$(date +"%F-%H-%M")
BACKUP_DIR="/mnt/usb/mysql/"
MYSQL_USER="backup"
MYSQL=/usr/bin/mysql
MYSQL_PASSWORD="*****"
MYSQLDUMP=/usr/bin/mysqldump
EMAIL=*****
RETENTION="+14"
MSG=/tmp/mysqlbackup.messages
echo "Date: $(date)">$MSG
echo "Hostname: $(hostname)" >>$MSG
echo "Backup script has run. [don't worry, it won't get far!]" >>$MSG
echo " ----- ----- ----- " >> $MSG
echo " " >> $MSG
echo " " >> $MSG
mkdir -p "$BACKUP_DIR/$TIMESTAMP"
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/$TIMESTAMP/$db.gz"
echo "Backing up: $db" >> $MSG
done
echo " " >> $MSG
echo " " >> $MSG
echo " ----- ----- ----- " >> $MSG
echo " " >> $MSG
echo " " >> $MSG
find $BACKUP_DIR* -mtime $RETENTION -exec rm {} \; >> $MSG 2>&1
mail -s "MySQL Backup script has run" "$EMAIL" <$MSG
rm -f $MSG
这非常完美,但是,我将这些错误/警告带到控制台:
[root@linux scripts]# ./mysqldump-all-databases.sh
mysqldump: Error 1194: Table 'XXXXX' is marked as crashed and should be repaired when dumping table `XXXXX` at row: 81052
mysqldump: Error 1194: Table 'XXXXX' is marked as crashed and should be repaired when dumping table `XXXXX` at row: 68532
-- Warning: Skipping the data of table mysql.event. Specify the --events option explicitly.
如何将它们记录到$ MSG文件以通过电子邮件发送?更好的是,如果发现错误,是否可以更改电子邮件主题[因此不会被忽略]