title: how to log mysqldump errors & warnings to file?
body: <p>I have a mysqlbackup script that I would like for it to:
  - backup all databases individually
  - log any errors to file
  - email that file</p>
  
  <p>what I have right now is: </p>
  
  <pre><code>\#!/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)"&gt;$MSG
  echo "Hostname: $(hostname)" &gt;&gt;$MSG
  echo "Backup script has run. [don't worry, it won't get far!]" &gt;&gt;$MSG
  echo " ----- ----- ----- " &gt;&gt; $MSG
  echo " " &gt;&gt; $MSG
  echo " " &gt;&gt; $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 &gt; "$BACKUP_DIR/$TIMESTAMP/$db.gz"
          echo "Backing up: $db" &gt;&gt; $MSG
  done
  
  echo " " &gt;&gt; $MSG
  echo " " &gt;&gt; $MSG
  echo " ----- ----- ----- " &gt;&gt; $MSG
  echo " " &gt;&gt; $MSG
  echo " " &gt;&gt; $MSG
  
  find $BACKUP_DIR* -mtime $RETENTION -exec rm {} \;  &gt;&gt; $MSG 2&gt;&amp;1
  
  mail  -s "MySQL Backup script has run" "$EMAIL" &lt;$MSG
  rm -f $MSG
  </code></pre>
  
  <p>Which works pretty much perfectly, however, I get these errors/warniongs to the console:</p>
  
  <pre><code>[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.
  </code></pre>
  
  <p>How can I log them to the $MSG file to get emailed? Even better, is it possible to change the email subject if errors are found [so it doesn't get ignored]</p>