How to add unique id to file name in cron job?

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

cron


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

Tags:

Filenames

Cron