Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I want to make a cron job that creates a tar of my web directory.

I have this job in place

* * * * * tar -zcvfp /disk1/archives/websites/`date +%Y-%m-%d_%I-%M-%S-%p`/`date +%Y-%m-%d_%I:%M:%S:%p`.tar.gz /web

this job SHOULD (but doesn't) create a tar file to my /disk1... directory. so I'm stumped on that one.

another thing that has been on my mind is, is it possible to make all backup tar file get dumped into a date +%Y-%m-%d and have all files that were created on that same day be placed there?

So an output would look like

/disk1/archives/websites/2012-02-05/2012-02-05_5:25:04.tar.gz

I have made this job run every minute for test purposes.

Is there anything wrong with my syntax?

share|improve this question

2 Answers 2

up vote 2 down vote accepted

cron does not know what tar or date is because it doesn't have environment variables. Reffer to the binaries using their full path.

If you don't know the full path, use which to get it.

Ex:

[bart@dev ~]$ which tar
/bin/tar
[bart@dev ~]$ which date
/bin/date

This would make your cron-line this:

/bin/tar -zcvfp /disk1/archives/websites/`/bin/date +%Y-%m-%d_%I-%M-%S`/`/bin/date +%Y-%m-%d_%I:%M:%S`.tar.gz /web
share|improve this answer
1  
thanks, ya its late... i need sleep, your 100% right. I forgot to insert the path of the command i'm trying to run. –  s2xi Feb 5 '12 at 13:56

Put your tar inside a shell script, say, /usr/local/bin/backup.sh. Then call that script from cron.

* * * * * /usr/local/bin/backup.sh

The trick is that cron is not a shell, so things that work in shell expansion may not work in crontabs. It's much, much safer and easier to debug to put your commands into a script, and then call that script (by it's full path).

share|improve this answer
    
You would still need to include full paths to binaries or but bash in front of your script so it would load a shell for it. –  Bart De Vos Feb 5 '12 at 14:31

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.