Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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.

#!/bin/bash
backup_filename=/var/www/vhosts/foo.com/private/backup_foo_db.sql
filename="db_daily_"`date +%Y%m%d`".tar.gz"
compressed_filename=/var/www/vhosts/foo.com/private/backup_foo_db_$filename.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.

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
add comment

2 Answers 2

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"`
filename="db_daily_"$myDate".tar.gz"
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
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.