LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-21-2007, 02:39 PM   #1
0.o
Member
 
Registered: May 2004
Location: Raleigh, NC
Distribution: Debian, Solaris, HP-UX, AIX
Posts: 208

Rep: Reputation: 35
Script to dump and restore rrd files


I was wondering if someone could point me in the right direction with this script.

Code:
#!/bin/sh
set -x
case "$1" in
        dump)
                Dump()
                exit
                ;;
        import)
                xmlimport
                exit
                ;;
        *)
                echo "nothing"
                exit
                ;;
esac

Dump()
{
        for rrd in `find . -type f -name "*.rrd"`
                do
                        xml=`echo $rrd | sed 's/.rrd//g'`

                        rrdtool dump $rrd > $xml
                done
}

xmlimport()
{
        for xml in `find . -type f -name "*.xml"`
        do
                rrd=`echo $xml | sed 's/.xml//g'`

                rrdtool restore $xml $rrd

                rm $xml
        done
}
I am not really used to using functions in my scripts and am trying to get used to doing so. I can use the for loops without a function and it work fine. Please advise. Thanks!!
 
Old 12-21-2007, 03:20 PM   #2
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,928

Rep: Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612
You need to put the functions before the case statement in the file. Shell functions must be read in first so that they are available when called.
 
Old 12-31-2007, 12:07 PM   #3
0.o
Member
 
Registered: May 2004
Location: Raleigh, NC
Distribution: Debian, Solaris, HP-UX, AIX
Posts: 208

Original Poster
Rep: Reputation: 35
Yeah, i had a brain fart when i wrote that. Here is the finished script if anyone wants it:

Code:
#!/bin/sh

ECHO="echo -e"

rrdump ()
{
	for rrd in `find . -type f -name "*.rrd"`
		do
			xml=`echo $rrd | sed 's/.rrd//g'`
			rrdtool dump $rrd > $xml.xml
			rm $rrd
		done
}	

xmlimport ()
{
	for xml in `find . -type f -name "*.xml"`
        do
                rrd=`echo $xml | sed 's/.xml//g'`
                rrdtool restore $xml $rrd.rrd
                rm $xml
        done
}


case "$1" in
        dump)
                rrdump
		;;
        import)
                xmlimport
                ;;
        *)
                $ECHO "$0 takes one of two arguments\n"
		$ECHO "$0 dump -- dumps contents to xml\n"
		$ECHO "$0 import -- imports xml files to rrd\n"
                ;;
esac

If executed at the top of the directory with the rrd files, i will search through and dump all rrd files to xml and preserve the path. It will do the same for xml -> rrd.

Last edited by 0.o; 12-31-2007 at 12:10 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Need bash script to list files, drop extension and dump to file talwar_ Programming 10 06-03-2011 09:18 AM
Shell Script to save/restore files and dir permissions joangopan Fedora 1 09-09-2007 02:51 AM
dump & restore hiren_a Linux - Newbie 1 04-29-2003 12:56 AM
dump/restore moger Linux - Software 4 12-31-2002 04:34 AM
dump and restore nat Linux - General 1 04-26-2002 04:08 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:54 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration