Forum Moderators: bakedjake

Message Too Old, No Replies

Create tar based on date

         

Robber

3:58 pm on Feb 15, 2006 (gmt 0)

10+ Year Member



Hi,

MattyMoose gave me some great advice a while back when I was trying to automate a backup process, the result being the following command:

find ./ -name "*.txt" -print ¦ tar czf archive/db_022006.tar.gz -T - && find ./ -name "*.txt" -exec rm {} \;

Well now I want to take it a bit further so that the tar will be created and named according to the current date. So, I got the current date using:
date '+%d%m%Y'

But how can I put this into the backup command above?

I tried:

find ./ -name "*.txt" -print ¦ tar czf date '+archive/db_%d%m%Y.tar.gz' -T - && find ./ -name "*.txt" -exec rm {} \;

But unfortunately that didn't work. Anyone got a sokution they could share?

Many thanks

coopster

6:51 pm on Feb 15, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Using your first example there ...

find ./ -name "*.txt" -print ¦ tar czf archive/db_022006$(date '+%F_%H%I%S%P').tar.gz -T - && find ./ -name "*.txt" -exec rm {} \;

Robber

7:13 pm on Feb 15, 2006 (gmt 0)

10+ Year Member



Cheers Coopster,

I had tried a couple of bracket combinations, didn't know about the $ in front though - is there a good reference for this sort of thing somewhere? Coming from a Windows environment its quite a steep learning curve, that said I'm already finding you can do so much with just a single line command.

Many thanks

MattyMoose

6:47 am on Feb 21, 2006 (gmt 0)

10+ Year Member



Hi there!

Same idea as what coopster said, just I prefer to be able to reuse the date variable throughout my script, so I usually define something like this:

#!/bin/tcsh

DATE=`date '+%d%m%Y'`

... do stuff ...

find ./ -name "*.txt" -print ¦ tar czf date '+archive/db_${DATE}.tar.gz' -T - && find ./ -name "*.txt" -exec rm {} \;

Then that way you can re-use the date variable.

I can't seem to find the bookmark I had that I used as reference for shell scripting, but give this a try, or search around a bit, find a format that you like. :) [freeos.com...]

Cheers,
MM