Using tar in shell script [SOLVED]

The place to post if you need help or advice

Moderators: ChrisThornett, LXF moderators

Using tar in shell script [SOLVED]

Postby Craxus » Sun Nov 04, 2012 8:24 am

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: Select all
#!/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.
Craxus
 
Posts: 10
Joined: Tue Dec 23, 2008 8:45 pm
Location: Surrey, UK

Postby MartyBartfast » Sun Nov 04, 2012 9:22 am

I would probably do something like this:
Code: Select all
#!/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.
User avatar
MartyBartfast
LXF regular
 
Posts: 820
Joined: Mon Aug 22, 2005 7:25 am
Location: Hants, UK

Re: Using tar in shell script

Postby nelz » Sun Nov 04, 2012 12:30 pm

Or
Code: Select all
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)
User avatar
nelz
Site admin
 
Posts: 8577
Joined: Mon Apr 04, 2005 11:52 am
Location: Warrington, UK

Postby Craxus » Sun Nov 04, 2012 6:37 pm

That's fixed it, thanks.

Code: Select all
#!/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
Craxus
 
Posts: 10
Joined: Tue Dec 23, 2008 8:45 pm
Location: Surrey, UK


Return to Help!

Who is online

Users browsing this forum: Exabot [Bot] and 1 guest