Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. 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

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
Please take the time to spell the title properly. It makes searching much easier for everyone. – Mikel Apr 26 '11 at 1:01
You wrote "How to add uniqie id to file nime in cron job". – Mikel Apr 26 '11 at 23:21
Why don't you use automysqlbackup? – stoeff Feb 5 at 17:59
up vote 13 down vote accepted

For bash, maybe:

... > /srv/backup/mysql_daily-$(date -u +\%Y\%m\%dT\%H\%M\%S)Z.${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).

update made it ISO 8601 per @johan comment


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

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


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

Your Answer


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.