| View previous topic :: View next topic |
| Author |
Message |
Craxus
Joined: Tue Dec 23, 2008 8:45 pm Posts: 10 Location: Surrey, UK
|
Posted: Sun Nov 04, 2012 8:24 am Post subject: Using tar in shell script [SOLVED] |
|
|
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 |
|
 |
MartyBartfast LXF regular

Joined: Mon Aug 22, 2005 8:25 am Posts: 780 Location: Hants, UK
|
Posted: Sun Nov 04, 2012 9:22 am Post subject: |
|
|
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 |
|
 |
nelz Moderator

Joined: Mon Apr 04, 2005 12:52 pm Posts: 8002 Location: Warrington, UK
|
Posted: Sun Nov 04, 2012 12:30 pm Post subject: Re: Using tar in shell script |
|
|
Or
| Code: |
find . -type f -name running-config -exec tar czf $(dirname {})/running-config-`/bin/date +\%Y\%m\%d`.gz {} \;
|
_________________ Unix is user-friendly. It's just very selective about who it's friends are. |
|
| Back to top |
|
 |
Craxus
Joined: Tue Dec 23, 2008 8:45 pm Posts: 10 Location: Surrey, UK
|
Posted: Sun Nov 04, 2012 6:37 pm Post subject: |
|
|
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 previous topic :: View next topic |
|