I'm running this bash script to make a backup of the database, but I'm always getting an error on the date and tar command.

filename="db_daily_"`date +%Y%m%d`".tar.gz"
mysqldump --add-drop-table -h localhost -ufoo -pbar foobar > $backup_filename
tar cvfz $compressed_filename $backup_filename
rm $backup_filename

./mysqlbackup.sh: line 3: date: command not found

Could this be a permission problem? I'm not running that file as root.

  • Try specifying the full path: /usr/bin/date, /usr/bin/tar, etc Sep 26, 2011 at 15:18
  • If you just want to compress a file, don't use tar, use something like bzip2 or gzip. Sep 26, 2011 at 15:19

2 Answers 2


Are you running this as a cron job or something similar? If so, the problem is likely that it's running with a different PATH than normal, and it doesn't include the directories that date and tar are in. There are two easy ways to fix this: either use full paths for the commands (e.g. /usr/bin/date, as @glenn jackman suggested) or explicitly set PATH at the top of the script (e.g. PATH=/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin or something similar).

BTW, you're also going to wind up with a weird backup filename. First, you set filename to something like "db_daily_20110926.tar.gz", then set compressed_filename to something like "/var/www/vhosts/foo.com/private/backup_foo_db_db_daily_20110926.tar.gz.tar.gz". This is probably not what you intended.


Simplify and split this line into multiple lines, from:

filename="db_daily_"`date +%Y%m%d`".tar.gz"

So it becomes something like this:

myDate=`date +"%Y%m%d"`
  • Or even nicer, myDate=$(date +"%Y%m%d"). Sep 26, 2011 at 15:51
  • @KAK I think it is a matter of taste since they do the same, but you are right that the $() is maybe a little bit nicer.
    – Johan
    Sep 26, 2011 at 16:02
  • 1
    You could even say filename=$(date "+db_daily_%Y%m%d.tar.gz") -- date's format string does not have to be just date fields. Sep 26, 2011 at 16:26
  • 1
    @Johan, the $(...) syntax also has the advantage that it actually nests properly. Sep 27, 2011 at 8:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .