Linux Format forums Forum Index Linux Format forums
Help, discussion, magazine feedback and more
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Using tar in shell script [SOLVED]

 
Post new topic   Reply to topic    Linux Format forums Forum Index -> Help!
View previous topic :: View next topic  
Author Message
Craxus



Joined: Tue Dec 23, 2008 8:45 pm
Posts: 10
Location: Surrey, UK

PostPosted: Sun Nov 04, 2012 8:24 am    Post subject: Using tar in shell script [SOLVED] Reply with quote

I use my TFTP server to store the configs of various routers, switches and firewalls. Each device has its own sub-directory beneath the TFTP root directory, and in each sub-directory is one file, named 'running-config'. I want to run a script just before midnight each day to check whether the file has been modified on that day, and if so, make a backup copy. I've written a small script to use 'find' and 'tar' but it puts the backup files in the location I run the script from, (i.e. the TFTP root directory), but I want each backup file in each config directory. I know why it's doing this, but I can't figure out the correct code to get it to do what I want. This is my script:
Code:

#!/bin/bash
cd /var/lib/tftpboot/
find . -type f -name running-config -exec tar czf running-config-`/bin/date +\%Y\%m\%d`.gz {} \;

How do I make the backup file stay in the same directory as the original file?


Last edited by Craxus on Sun Nov 04, 2012 6:42 pm; edited 2 times in total
Back to top
View user's profile Send private message
MartyBartfast
LXF regular


Joined: Mon Aug 22, 2005 8:25 am
Posts: 814
Location: Hants, UK

PostPosted: Sun Nov 04, 2012 9:22 am    Post subject: Reply with quote

I would probably do something like this:
Code:

#!/bin/bash
cd /var/lib/tftpboot/
for configfile in `find . -type f -name running-config` ; do  tar czf "${configfile}-`/bin/date +\%Y\%m\%d`.gz"  "${configfile}" ; done

_________________
I have been touched by his noodly appendage.
Back to top
View user's profile Send private message
nelz
Site admin


Joined: Mon Apr 04, 2005 12:52 pm
Posts: 8464
Location: Warrington, UK

PostPosted: Sun Nov 04, 2012 12:30 pm    Post subject: Re: Using tar in shell script Reply with quote

Or
Code:

find . -type f -name running-config -exec tar czf $(dirname {})/running-config-`/bin/date +\%Y\%m\%d`.gz {} \;

_________________
"Insanity: doing the same thing over and over again and expecting different results." (Albert Einstein)
Back to top
View user's profile Send private message
Craxus



Joined: Tue Dec 23, 2008 8:45 pm
Posts: 10
Location: Surrey, UK

PostPosted: Sun Nov 04, 2012 6:37 pm    Post subject: Reply with quote

That's fixed it, thanks.

Code:

#!/bin/bash
cd /var/lib/tftpboot/
for configfile in `find . -type f -name running-config` ; do  tar czf "${configfile}-`/bin/date +\%Y\%m\%d`.gz"  "${configfile}" ; done
Back to top
View user's profile Send private message
View previous topic :: View next topic  
Display posts from previous:   
Post new topic   Reply to topic    Linux Format forums Forum Index -> Help! All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Linux Format forums topic RSS feed 


Powered by phpBB © 2001, 2005 phpBB Group


Copyright 2011 Future Publishing, all rights reserved.


Web hosting by UKFast