1834930 Members
2591 Online
110071 Solutions
New Discussion

need backup script help

 
SOLVED
Go to solution
Anthony khan
Frequent Advisor

need backup script help

Hi All, I have to write a script to backup some files or directory on tape drive. The script can take arguments and compress the files and then tar or cpio (depends on user input) on tape drive and make a log file what which files are copied on tape drive. Suppose the script name is mediawrite, here is argument we want to pass to script.

mediawrite -f -m media -d device -o -e
Will cut to media the single specified file

mediawrite -d -m media -d device -o -e
Will cut to media all files in the specified directory

mediawrite -i -m media -d device -o -e
Will cut to media all files listed in the specified input file

.
there shouldn't be any problems with symbolic links? For example, I use "/prod/SSP/rpt/Rep1", which is a symbolic link to "/pool/prod/SSP/rpt/Rep1".

The process will take the appropriate input type and transfer the appropriate file to the specified media and/or device. The process will return an error code on any type of failure. If successful, the process will return a "0" return code. The media identification/serial number, and the individual files that were written to the media should be written to standard output after the files were successfully written to the media.

A possible format of the output may be:

Serial# File# Input File Name Media File Name
======== ======= ======================================================= ==================================
Abc123 1 /prod/SSP/rpt/Rep1/stmt.R.20020226.Bbkr1.0001.mfc.xrx stmt.R.20020226.Bbkr1.0001.mfc.xrx
Abc123 2 /prod/SSP/rpt/Cli2/stmt.CPEA.20020226.P1.0001.mfc.xof stmt.CPEA.20020226.P1.0001.mfc.xof

Can someone help me out here.
15 REPLIES 15
Anthony khan
Frequent Advisor

Re: need backup script help

Sorry one more thing if the files are bigger then tape drive should give an error message
Anthony khan
Frequent Advisor

Re: need backup script help

Can someone help me out form where i can start
steven Burgess_2
Honored Contributor
Solution

Re: need backup script help

Hi Anthony

It looks as though you are already there with what you want from the script but just need help putting it together?

Are you going to give users the choice of files to back up ? output devices etc

If so, to keep things simple I would you examples such as

#!/usr/bin/sh

echo "What would you like to do ?"
echo " Enter 1 if you wish to back up a file"
echo "Enter 2 if you wish to back up a directory"
echo --------------------------
echo " Please enter your choice \c"
read choice

echo "which files/directory do you wish to back up ? \c"
read files

echo "Here are you list of available output devices"

echo " /dev/rmt/0m "
echo " /dev/rmt/1m "
echo " /dev/rmt/2m "

echo "Please type the full device name \c "
read device

# from the users choice

case $choice in

1) < command > > /tmp/log 2> /tmp/errors
2) < command > > /tmp/log 2> /tmp/errors
;;

*) echo " you have not pressed a valid key

esac

You have 2 variables , files and device, when you enter your commands the choices entered from the user will be entered into the command as $files and $device

Is this the kind of thing you are after ?

Or something more complex

Steve
take your time and think things through
John Carr_2
Honored Contributor

Re: need backup script help

Hi

if you wish to pass arguments to your script you will could read up on getopts.

while getopts ":ab:c" opt; do
case $opt in

a) process options -a ;;
b) process options -b ;;
c) process options -c ;;

esac
done


Anthony khan
Frequent Advisor

Re: need backup script help

Hi Steve, Thanks for your reply, you make it realy very simple, is there any way we can add 2 more arguments like the media name and output format.
steven Burgess_2
Honored Contributor

Re: need backup script help

Hi Anthony

You either give the user a choice of medium names, then

read medium

or at the beginning of the script specify the medium

medium1=

your variable is then $medium_name which you simply pass into your full command

same goes with your output destination

I'm at home at the moment, but back in over the weekend. It's fairly quite then so if you haven't got a full solution I'll put more ideas down for you. I'm fairly new to scripting so it will be good practice for me also

Steve
take your time and think things through
steven Burgess_2
Honored Contributor

Re: need backup script help

Hi

Just one more before I log off. After each command there will always be an exit code of either 0 = successful or 1 = failure

You can perform commands depending on the outcome of the exit code of your previous commands

ie

mediawrite

if [$? = 0 ]
then
else
if [ $? = 1 ]
then
fi

Regards

Steve
take your time and think things through
harry d brown jr
Honored Contributor

Re: need backup script help

Anthony,

what do you mean by "media" with the "-m" option?

And you are going to have issues using the same option flag "-d" in your second example. Why not just have "-f" handle single files and directories?


live free or die
harry
Live Free or Die
Anthony khan
Frequent Advisor

Re: need backup script help

Hi Harry,

-m media is differnet tape drive, -o output format is tar/cpio.

Thanks
harry d brown jr
Honored Contributor

Re: need backup script help

Then what is "-d device", if "-m media" is the tape drive??

live free or die
Live Free or Die
Anthony khan
Frequent Advisor

Re: need backup script help

Sorry I was confuse, yes -d device is the tape and i'll take out the option -m media.
steven Burgess_2
Honored Contributor

Re: need backup script help

Hi Anthony

Have attached script ( not finished ) as I had a few problems come in over the weekend

You think initially it's quite simple but it does get complicated when user intervention comes into the equation

Have a look anyway. The more I got it into the more I thought of how functions and getopts would make things easier when putting it together.

As previously mentioned, i'm only 2 months into scripting so have got a long way to go before I become proficent

I haven't checked the 1st case

What comes into the equation is the location of the files requested to be backed up and whether they are actually there etc etc

I am going to finish it when I get chance

Have a look anyway and see what you think

you should be able to complete or adjust it to suit your needs

Regards

Steve
take your time and think things through
Anthony khan
Frequent Advisor

Re: need backup script help

Hi Steven, thanks for your help, I did some changes in your script and it was working good but the thing is that I have to run the script from the other program so i need command line type script, so attach is the script...but the problem is that the last argument (-l for logfile) is not working... Can you take a took at it and let me know what i am missing here, OR any one else help me out in it.
steven Burgess_2
Honored Contributor

Re: need backup script help

Hi Anthony

Your script is really good. I did want to look further into getops, but didn't get chance to.

Now you have given me a good example i can hopefully incorporate it more

Ok

I've printed the script , and as far as I can see you have got the user to specify the log file

l) LOG_FILE=$OPTARG;;

when checking whether correct arguments have been passed you just use

exit 1

This will just exit the script but not send anything to the log file

you could use

echo "exit 1 because $FILE_PATH not supplied by $LOGNAME" >> $LOG_FILE

Also pass the output of your backup commands to the log file again with the use of >> $LOG_FILE after the command

You can pass to both stdout and log file with the use of the tee command if you require

ie

tar -cvf $DEVICE $FILE_PATH | tee $LOG_FILE

What do you think ?

Steve
take your time and think things through
Anthony khan
Frequent Advisor

Re: need backup script help

Steven, I think is ok, I did add this in the script.

Thanks