0

I'm using Percona's Xtrabackup tools to back up a database nightly. I wrote a wrapper script to keep the proper number of backups around, write the output to a log file, and email the log file if the backup dies. When I manually run the script as root, the log file is generated as expected. However, once I put it in cron, the log file is created, but no content is generated. Here's the relevant portion of the script (let me know if I should post the whole thing.

DATE=`date +%F-%H%M`
LOC=/var/mysqlbackups/dbBackup-$DATE
LOGLOC="$LOC.log"
/usr/bin/innobackupex --user=$SQLUSER --password=$SQLPASSWORD --no-timestamp $LOC &>>$LOGLOC
if [ $? -ne 0 ]
then
  CONTENT="Backup Failed. Log information below.\n\n--------------------------------------------------------\n$CONTENT"
  CONTENT+=`cat $LOGLOC`
  echo -e "$CONTENT" | mail -s "Backup on DB3 failed" me@my.address
  exit 1
fi

Here's the crontab entry in root's crontab:

5 1 * * * /usr/local/sbin/dbBackup.sh

And the resulting directories/files. Not that on 1/28 I ran it manually, then on 1/29 it was run by cron:

drwxr-xr-x 5 root root  4096 Jan 28 10:18 dbBackup-2015-01-28-1010
-rw-r--r-- 1 root root 21441 Jan 28 10:18 dbBackup-2015-01-28-1010.log
drwxr-xr-x 5 root root  4096 Jan 29 01:14 dbBackup-2015-01-29-0105
-rw-r--r-- 1 root root     0 Jan 29 01:05 dbBackup-2015-01-29-0105.log

I've asked the Googles, but haven't had any luck identifying the problem. Any help would be appreciated.

Andrew Schulman
  • 8,811
  • 21
  • 32
  • 47
Josh H
  • 19
  • 6

2 Answers2

0

I see two possibilities here. One, I strongly recommend using full paths for all commands in cron jobs - e.g. /usr/bin/cat instead of cat. Two, your shell may be a more restrictive one in the cron environment where the string += syntax doesn't work. Try to figure a way around that somehow - I'm not sure how off the top of my head, but cat'ing to another file would be where I would start.

John
  • 9,070
  • 1
  • 29
  • 34
  • I'm not sure that either apply - the trouble happens before that, as the log file in the filesystem itself is emtpy. Your response did make me think though - I wonder if the problem is with using `&>>` for redirection. I'll try with more traditional IO redirection and report back. – Josh H Jan 30 '15 at 19:13
0

It turns out that using IO redirection that only bash supports was breaking the script when it ran in cron. I switched to the old school 2>&1 syntax, and the problem is solved.

Josh H
  • 19
  • 6