[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: How do I do this in bash ??



On Sun, Jan 19, 2003 at 10:48:43AM +0000, Dave Selby wrote:
> I am writting a weekly automated backup script, very simple .. tars a 
> directory called 'myfiles' to a second hard drive.
> 
> Works AOK except I want the name of the file written by tar to be the time 
> and date. So I get a list of tared dated backups
> 
> I know 'date' gives me exactly what I want but I cant figure out how to get 
> tar to write a file with the value of date as its file name ...
> 
> 
> #! /bin/sh
> 
> # Backup entire 'myfiles/' directory, name it with the date.
> 
> cd /usr/local/
> tar -czf /mnt/archive/autoarchive/date myfiles
> 
> 
> I put this in /etc/cron.weekly and I get a file called date !!???
> I have tried '' and "" all to no avail

What you want is backticks (`date`). Since date's default output isn't
very good as a filename you probably want to specify the output format.
I would use something like this:

tar czf /mnt/archive/autoarchive/`date '+%Y%m%d'`.tar.gz myfiles

which would give you a file named 20030119.tar.gz. The reason I use
year-month-day format is that the alphabetical sorting that ls does by
default will match the chronological order of the files.

-- 
Michael Heironimus



Reply to: