Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

./ line 3: date: command not found

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

share|improve this question
Try specifying the full path: /usr/bin/date, /usr/bin/tar, etc – glenn jackman Sep 26 '11 at 15:18
If you just want to compress a file, don't use tar, use something like bzip2 or gzip. – glenn jackman Sep 26 '11 at 15:19

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"`
share|improve this answer
Or even nicer, myDate=$(date +"%Y%m%d"). – Kusalananda Sep 26 '11 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 '11 at 16:02
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. – glenn jackman Sep 26 '11 at 16:26
@Johan, the $(...) syntax also has the advantage that it actually nests properly. – Kusalananda Sep 27 '11 at 8:28

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/". This is probably not what you intended.

share|improve this answer

You must log in to answer this question.

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