Using Crontab to tar a directory at a regular interval
Solution 1:
You want dates in ISO format - YYYY-MM-DD. They sort properly that way.
tar -cf srcdirectorybackup`date +%F`.tar /path/to/src
Assumes tar and date are in the path. You can add this line to a crontab.
Cripes! ericslaw is absolutely right. Those %'s need to be escaped if they're used in the crontab file or they'll be interpreted as newlines!
So, if you're going to put this line right into the crontab, escape the "%" with a "\".
tar -cf srcdirectorybackup`date +\%F`.tar /path/to/src
The rep really ought to go to ericslaw...
Solution 2:
ACK!
several folks have suggested the wonderful date +%Y%m%d_%H%M%S style solution but nobody has mentioned the major caveat of '%' in crontabs...
'%' it is equivalent to '\n' so your cronjob will likely fire and fail mystereously!
You'll more likely want to simply escape it with backslash like this (and I also like to get some kind of inventory or other output to check that it ran).
0 4 * * * tar vcf /path/to/dsttarfile.tar.`date +\%Y\%m\%d_\%H\%M\%S` /path/to/srcdir > /path/to/logfile.log 2>&1
You might consider using `date +%w' as part of your tarfile, so you have a tar file for each of the last 7 days and dont have to worry about purging old copies.