Showing posts with label shell script. Show all posts
Showing posts with label shell script. Show all posts

Wednesday, 3 September 2008

Flash archive backup central server

I create flash archive backup and copy to central server. If there is any error in backup process or scp process I got mail.
Also scp works passwordless and secure.

First create transfer user at central server and give necceseery permissions.
At the main server:


# useradd transfer -d /export/home/transfer
# passwd transfer
# chown transfer /centralbackup/dir


At servers which you are going to backup, create public keys.


# ssh-keygen -t rsa
# cat ~/.ssh/id_rsa.pub


copy content to the transfer user in central server /transferuserhomedirectory/.ssh/authorized_keys . Be carefull there is no line break in the id_rsa.pub file.
now you can login without password to central server test is ssh transfer@centralserver.

This is the script that you can backup weekly.


backupname=$(hostname)_$(date '+%Y%m%d')
backupdir=/site/local/backupdir
remotebackupdir=/centralbackup/dir
remoteloc=transfer@mainserver:$remotebackupdir
mailaddress="yourmailaddress@domaim.com"
#if you want to exclude directories when you are creating flash archive use -x
flarcreate -c -S -n $backupname -x /putdirthatyouwanttoexclude -x /anotherexclude $backupdir/$backupname.flar
#check if there is problem creating flash archive
if [ $? -ne 0 ] ; then
echo flarcreate problem in $(hostname) "\n" | mailx -s "$(hostname) flarcreate problem" $mailaddress
exit 1
fi
scp $backupdir/$backupname.flar $remoteloc
#check if there is problem copying to central server.
if [ $? -ne 0 ] ; then
echo backup copy problem in $(hostname) "\n" | mailx -s "$(hostname) backup copy problem" $mailaddress
exit 1
fi
#deletes backup of previous week
rm $backdir/$(hostname)_$(TZ=GMT+167 date +%Y%m%d).flar
#check if there is problem deleting file
if [ $? -ne 0 ] ; then
echo old backup delete problem in $(hostname) "\n" | mailx -s "$(hostname) old backup delete problem" $mailaddress
exit 1
fi

Friday, 15 August 2008

Shell script argument check and directory check

With this part you can check arguments are supplied and give usage information.
if [ $# -ne 2 ]
then
echo 1>&2 Usage: $0 firstdatafile lastdatafile
exit 127
fi

Create if directory does not exist
DESTDIR=dirname
if ! test -d "$DESTDIR"
then mkdir -p $DESTDIR
fi

Thursday, 24 July 2008

unary operator expected shell script

Place quotes around your variables

if [ "$state" != "" ]
then
echo problem
fi

Wednesday, 12 March 2008

Shell script Get the time difference

Get difference

date1=`date +%s`
commands
..
..
.
date2=`date +%s`
date -d "00:00:00 $(( ${date2} - ${date1} )) seconds" +"%H:%m:%S"

Wednesday, 5 March 2008

Sed and awk, find pattern and get next lines

Search pattern and print next two lines. You can add more getline and print here for awk and n;p for sed. Also you can do it via +2p

awk '/pattern/{getline;print;getline;getline;print}' file
sed -n '/pattern/{n;p;n;p;}' file
sed -n '/pattern/,+2p' file

Get between two pattern

awk '/pattern1/,/pattern2/' file
sed -n '/pattern1/,/pattern2/p' file

This is small script do same another way.

pattern=yourpattern
bgn=$(grep -n $pattern scsconfig.log awk -F: {'print $1'})
ed=`expr $bgn + 3`
cat scsconfig.log sed -n ''$bgn','$ed'p'

Tuesday, 14 August 2007

HP Raid control in Linux

Think that you are using HP DL series (DL380) hardware and running linux, you want to check raid status then you need:
  • Install hpacucli from HP.
  • Run this script
#!/bin/sh
SLOTLIST=$(hpacucli ctrl all show | \
grep Slot | sed -e 's/^.*Slot //g' -e 's/ .*$//g')

for i in $SLOTLIST
do
echo
hpacucli ctrl slot=$i show status | grep -v "^$"
echo
hpacucli ctrl slot=$i ld all show status | grep -v "^$"
hpacucli ctrl slot=$i pd all show status | grep -v "^$"
done
echo

  • Output:

Smart Array 6i in Slot 0
Controller Status: OK
Cache Status: OK

logicaldrive 1 (203 GB, 5): Ok
physicaldrive 2:0 (port 2:id 0, 72.8 GB): OK
physicaldrive 2:1 (port 2:id 1, 72.8 GB): OK
physicaldrive 2:2 (port 2:id 2, 72.8 GB): OK
physicaldrive 2:3 (port 2:id 3, 72.8 GB): OK