Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

My cronjob looks like this:

5 3 * * * mysqldump -u root test > /srv/backup/mysql_daily.dump

How can I make filename unique for every time when cronjob writes mysql_daily.dump?

share|improve this question
2  
Please take the time to spell the title properly. It makes searching much easier for everyone. –  Mikel Apr 26 '11 at 1:01
1  
You wrote "How to add uniqie id to file nime in cron job". –  Mikel Apr 26 '11 at 23:21

3 Answers 3

up vote 7 down vote accepted

For bash, maybe:

... > /srv/backup/mysql_daily-$(date -u +\%Y\%m\%dt\%H\%M\%S).${RANDOM}.dump

Personally, i generally only put simple commands in my crontab. I'd put this in a little script and use the script in the crontab. This would have the benefit of not requiring the % characters to be escaped (a common crontab pitfall).

share|improve this answer
    
You could modify the syntax just a little and it would be perfect ISO 8601 date format. [YYYY][MM][DD]T[hh][mm][ss]Z, since you use date -u for UTC you could add a Z after the date ;). en.wikipedia.org/wiki/… –  Johan Apr 26 '11 at 8:04

Try

5 3 * * * mysqldump -u root test > /srv/backup/mysql_daily.$(/bin/date +\%Y\%m\%d).dump

which will put the current date in the format YYYYMMDD into the filename.

share|improve this answer

The general practice for this is to use a timestamp using date +%s to generate something useful to mark the file (I used seconds as an example but you can use any date format). It's really not necessary to have a truly unique file name as long as the file name doesn't conflict.

5 3 * * * mysqldump -u root test > /srv/backup/mysql_daily_`date +\%s`.dump
share|improve this answer

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.