5

I have a shell script to get a MySQL database dump. This script works fine when run manually

#!/bin/sh
fqn=/home/Mysqluser/daily_dumps/bookstore_`date +%Y%m%d_%H%M%S`.sql.gz
mysqldump -u root -h localhost -pmysql#passwd bookstore | gzip > $fqn

But when I include this script to be run in cron job, it doesnt make the backup.

This is what I included in the cron job:

00 00 * * *  /home/Mysqluser/daily_dumps/bin/backup_database.sh

What am I missing here?

AgentAkki
  • 107
  • 9
user120612
  • 51
  • 3

2 Answers2

0

It's probably because the environment(s) (type env at prompt to see it) are different.

I had similar problem trying to run a mysql query in cron.daily, I solved it by adding this flag --defaults-file=/root/.my.cnf

It look like mysqldump has a similar flag --defaults-extra-file (needs to be first argument) but if that doesn't work try to to find out what is the difference between your command-line environment and cron environment. Create a cronjob that prints the output of env to a file and compare it to what you get when you run it from your shell.

Nifle
  • 34,203
  • 26
  • 108
  • 137
  • I had something similar on running a php script through cron. The cron job was set to run as my user account, but when we delved further, the php being invoked under cron was from a completely different directory and therefore not running under the same php.ini as my interactive user account. – Fiasco Labs Feb 29 '12 at 08:17
0

Also, be sure that mysqldump is in the $PATH when running from cron. A best practice is to set $PATH directly in your script, or to specify full paths to any commands that are invoked (e.g., /usr/bin/mysqldump).

zackse
  • 622
  • 4
  • 8