Operating System - HP-UX
1825667 Members
3827 Online
109686 Solutions
New Discussion

Script to back up filesystem after it reaches 20 %....

 
Akhter
New Member

Script to back up filesystem after it reaches 20 %....

I need a script which will trigger the backup process once the filesystem reaches a pre-defined level ( say for ex : 20 % ).
After the backup process is complete, the script should delete files from the filesystem. This script should run as an continous process and should monitor the filesystem for its pre-defined level. This is basically required for my SAP R/3 environment where I need to backup my redo logs on a tape library. Also advice if there is an better process than this.

Regards
-Charu
15 REPLIES 15
KapilRaj
Honored Contributor

Re: Script to back up filesystem after it reaches 20 %....

#!/usr/bin/sh
if [ $# -lt 2 ]
then
echo "Usage $0 fs trigger"
exit 0
fi
FS=$1
TR=$2
USAGE=`df -k $FS |awk '{print $4}' |sed "s/%//g"`
if [ $USAGE -gt $TR ]
then
tar cvf /dev/rmt/0m $fs
if [ $? -ne 0 ]
then
echo "Hey bakcup failed |mail -s URGENT sysadmin@company.com"
exit8
else
rm $FS/*
fi
else
echo "USAGE of $FS is currently $USAGE"
fi

Schedule it in cron for every 30 minutes ?.


Regds,

Kapil
Nothing is impossible
Charu
New Member

Re: Script to back up filesystem after it reaches 20 %....

Hi Kapil,

Thanks for the script.
Can you educate me as to where should I enter the name of the filesystem and the percentage which I need to pre-define.

As I am new to unix, please try to elaborate as much as you can.

Thanks in advance
Regards
-Charu
Akhter
New Member

Re: Script to back up filesystem after it reaches 20 %....

Hi Kapil,

Thanks for the script.
Can you educate me as to where should I enter the name of the filesystem and the percentage which I need to pre-define.

As I am new to unix, please try to elaborate as much as you can.

Thanks in advance
Regards
-Charu
KapilRaj
Honored Contributor

Re: Script to back up filesystem after it reaches 20 %....

Invoke my script as

./script /database/arch 20

Syntax :

script

Do a proper testing before put anything to production ! Comment oput the tar and rm entries and have some friendly mesgs so that you are sure what u r doing !

Regds,

Kaps
Nothing is impossible
Senthil Kumar .A_1
Honored Contributor

Re: Script to back up filesystem after it reaches 20 %....

hello charu,

I would be glad to interpret Kapils shell script.Let us assume you create the shell script with the name "fscheck".

-----------------------------------------------
if [ $# -lt 2 ]
then
echo "Usage $0 fs trigger"
exit 0
fi
-----------------------------------------------

These line actually test the correct number of parameter/argument you are likely to pass when you are executing shell script.Here it is expecting two argument.first argument indicates the path to your filesystem under scrutiny,Sencond argument is indicative of the percentage when you want to trigger the backup.ex: "fscheck /var 20" would mean that you are checking /var filesystem for 20% accupancy.

---------------------------------------------
USAGE=`df -k $FS |awk '{print $4}' |sed "s/%//g"`
---------------------------------------------

Here Kapil is calculating the current usage and assigning the value to variable USAGE.

---------------------------------------------
if [ $USAGE -gt $TR ]
then
tar cvf /dev/rmt/0m $fs
if [ $? -ne 0 ]
then
echo "Hey bakcup failed |mail -s URGENT sysadmin@company.com"
exit8
else
rm $FS/*
fi
else
echo "USAGE of $FS is currently $USAGE"
fi
---------------------------------------------

here kapil has used nested IF conditions.basically the first if loop tests wether the usage is greater than the value you mentioned as the second argument($TR).If the test failed it simply echos the current usage of the fs.If the test succeded it starts backing up the filesystem using tar command.after the execution of tar The script tests wether tar command succeded or not using another "if" condition with the help of the variable "$?".if the test failed it removes the backedup files(that is if tar command succeded).

NOTE: here i would suggest to use rm -rf $FS/* instead of rm $FS/* considering the fact that a directory might be encontered.

if the test succeded(if tar command fails), the script sents you a mail with URGENT as message title.

NOTE:please use "exit" in the script instead of "exit8" i guess it would have been a typo.

regards
Senthil

p.s:
points
Let your effort be such, the very words to define it, by a layman - would sound like a "POETRY" ;)
KapilRaj
Honored Contributor

Re: Script to back up filesystem after it reaches 20 %....

thanks senthil ..

I did miss a space after exit in that statement. You have explained it the way i thought. Normaly redologs are just files and there is rare chances of hving a directory in there.

Infact I wanted the requester to write his own script with his own knowledge rather than trying something which he doesn't know !.

I can suggest few more options like compress the logs file and move it to a directory which will be backed up daily and somebody can query the backup whether it is successful and then delete the files. Something like take a list of files in this directory in to a loop, check in "tar tvf" to see whether it is backed up if "YES" then delete it from filesystem

Anyway it kis upto him to think and implement the best

Kaps
Nothing is impossible
Zeev Schultz
Honored Contributor

Re: Script to back up filesystem after it reaches 20 %....

Pardon my unfamiliarity of SAP redo log
but comparing to Oracle redo logs I assume
its continuously I/O'ed log.In that case how
could you delete files from filesystem?

And afaik (Oracle again) isn't it being rewriten after a space limit is reached?

I assume in those real time I/O's you can better think about some sort of snapshot solution (filesystem or storage level).

rgds,

Zeev

P.S pls give points to the crowd.
So computers don't think yet. At least not chess computers. - Seymour Cray
Akhter
New Member

Re: Script to back up filesystem after it reaches 20 %....

Hi,
This script is not working. I tried running it but it displays errors right from line no 14 and then line no 19, 21...
Request you to guide me with a script and the entire steps to run it.
Please be sweet and simple.
-Charu
KapilRaj
Honored Contributor

Re: Script to back up filesystem after it reaches 20 %....

Can u post a df -k o/p please ...

run the script as follows and if possible post the o/p in the forum

sh -x ./script fs_name 20

Also comment out those rm and tar commands ..

Kaps
Nothing is impossible
Akhter
New Member

Re: Script to back up filesystem after it reaches 20 %....

# sh -x ./charu /opt 20
+ [ 2 -lt 2 ]
+ FS=/opt
+ TR=20
+ + df -k /opt
+ awk {print $4}
+ sed s/%//g
USAGE=:
Kb
Kb
used
./charu[9]: Syntax error at line 20 : `fi' is not expected.
# df -k /opt
/opt (/dev/vg00/lvol6 ) : 1513328 total allocated Kb
280752 free allocated Kb
1232576 used allocated Kb
81 % allocation used
#
KapilRaj
Honored Contributor

Re: Script to back up filesystem after it reaches 20 %....

which operating system is this ?

Kaps
Nothing is impossible
Bharat Katkar
Honored Contributor

Re: Script to back up filesystem after it reaches 20 %....

Akther,
Looking at ur error:
Syntax error at line 20 : `fi' is not expected.
it seems there is some mismatch with the if branching. It is written as follows:
1. if [expr]
then commands;
fi
2. if [expr]
then commands;
else commands;
fi
Make sure that "if" is ended with correspoding "fi". If you use "if" inside "if" then it has to ended first with "fi" then the parent one again with "fi"


You need to know a lot to actually know how little you know
Senthil Kumar .A_1
Honored Contributor

Re: Script to back up filesystem after it reaches 20 %....

hello charu,

In your script replace
USAGE=`df -k $FS |awk '{print $4}' |sed "s/%//g"`

with
USAGE=`df -k /var|tail -1|tr -s " "|cut -d " " -f 2`


Please NOTE:this is a command substitution and hence back quote to be used AND thus,not single quotes.
Let your effort be such, the very words to define it, by a layman - would sound like a "POETRY" ;)
Senthil Kumar .A_1
Honored Contributor

Re: Script to back up filesystem after it reaches 20 %....

hello charu,

can you attach the script you have written.I will run it in my system and return you the corrected version.

regards
Senthil
Let your effort be such, the very words to define it, by a layman - would sound like a "POETRY" ;)
Senthil Kumar .A_1
Honored Contributor

Re: Script to back up filesystem after it reaches 20 %....

hello charu/akther,

spare the expense of sending your shell script.I have tried the shell script which kapil has indicated(ofcourse with major modification to removing the files...etc...).

I can assure you it will work fine 100% on hpux systems if you replace
USAGE=`df -k $FS |awk '{print $4}' |sed "s/%//g"` with
USAGE=`df -k /var|tail -1|tr -s " "|cut -d " " -f 2`

regards,
Senthil
Let your effort be such, the very words to define it, by a layman - would sound like a "POETRY" ;)