Put date into backup .tar.gz filename

11

5

I'd like to run a simple tar command to backup my website directory. It will run through cron, but I want to be able to put the time at which the backup was made as the filename. I don't really mind what format it's in, but something vaguely readable would be good. I'm just looking for a generic command; a way of putting the date in the filename - I can do the rest of the tar stuff.

Bojangles

Posted 2011-03-11T18:38:13.567

Reputation: 535

Answers

24

tar cfz backup-$(date +%Y-%m-%d).tar.gz ...

man strftime or man date to see what %-escapes can be used with date.

geekosaur

Posted 2011-03-11T18:38:13.567

Reputation: 10 195

Thank you :-) I found the date thingy before, but couldn't get it to work for some reason. Geekosaur saves the day! – Bojangles – 2011-03-11T19:11:37.080

This may be specific to which date command variant you use, but this probably does the same thing with a few less characters: tar cfz backup-$(date +%F).tar.gz ... – rich – 2014-04-26T17:31:12.083