Showing posts with label scripts. Show all posts
Showing posts with label scripts. Show all posts

Saturday, 7 March 2020

Backup of file system using scripts and moving the backup file to remote server.

 #!/bin/bash
# This script is used to take backup of mount point /var and /etc

tar -cvzf /tmp/backup.tar.gz /var /tmp
find /tmp/backup.tar.gz -mtime +1 -type f -print &> /dev/null
If [ $? -eq 0 ]
        then
        echo backup completed successfully
        echo
 echo moving backup file to backup  server...
   echo .
   echo ..
   echo ...
scp /tmp/backup.tar.gz  root@192.168.1.1 x:/backup/
else
echo backup failed
echo
echo troubleshoot the script
fi

:wq!

Friday, 6 March 2020

Delete old files through scripts

# To delete old files older then 90 days
find /home/scripts/    -mtime +90 -exec rm ( )\;

#To add .old extension to older files from more then 90 days
find /home/scripts/ -mtime +90 -exec mv {} {}.old ( ) \;

#To list all the files older then 90 days

find /home/scripts/ -mtime +90 -exec ls -l ( ) \;

Thursday, 5 March 2020

How to include linux commands in scripts, effectively with echo command - Basic OS monitor script



#/bin/bash
echo hi!!! Naveen
echo
echo welcome to basic os monitoring script
echo
echo top 10 processes running now
top | head -10
echo
echo mount point size
df -h
echo
echo free memory in the system now
free -m
echo
echo output of uptime command
uptime
echo
echo io statistics
iostat
echo
echo

OUTPUT :

Hostname:/install # ./basic_mon.sh
hi!!! Naveen

welcome to basic os monitoring script

top 10 processes running now
top - 11:10:05 up 79 days,  2:20,  1 user,  load average: 0.19, 3.69, 3.22
Tasks: 425 total,   1 running, 424 sleeping,   0 stopped,   0 zombie
%Cpu(s):  0.3 us,  0.3 sy,  0.0 ni, 99.4 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem:  13201688+total, 66643632 used, 65373244 free,   593712 buffers
KiB Swap: 20971516 total,    78036 used, 20893480 free. 29159464 cached Mem

  PID USER      PR  NI    VIRT    RES    SHR S   %CPU  %MEM     TIME+ COMMAND
 1962 root      20   0  425120   1596    508 S  6.250 0.001 331:15.05 tuned
20580 pl0adm    20   0 13.934g 9.828g 640468 S  6.250 7.806   8:37.73 hdbindexserver
27050 root      20   0   41340   3260   2208 R  6.250 0.002   0:00.02 top

mount point size
Filesystem                                   Size  Used Avail Use% Mounted on
/dev/xvda1                                    40G  4.8G   33G  13% /
devtmpfs                                      64G     0   64G   0% /dev
tmpfs                                        110G     0  110G   0% /dev/shm
tmpfs                                         64G  800K   64G   1% /run
tmpfs                                         64G     0   64G   0% /sys/fs/cgroup
/dev/xvdd                                    2.9G  771M  2.0G  28% /opt/bmc
/dev/xvdc                                    2.0G   21M  1.8G   2% /tmp
/dev/xvdb                                    2.0G  1.3G  536M  72% /var
/dev/xvde                                    7.8G  6.9G  471M  94% /usr/sap
:/vol1_XY0_PL0/q_sysfiles/sapmnt  100G   23G   78G  23% /sapmnt/PL0
:/vol2_XY0_PL0/q_dbdata           3.5G  1.9G  1.7G  52% /hana/data/PL0
:/vol1_XY0_PL0/q_sysfiles/db      100G   23G   78G  23% /usr/sap/PL0
:/vol3_XY0_PL0/PL0                1.0G  4.8M 1020M   1% /hana_backup/PL0/log
:/vol1_XY0_PL0/q_install          100G   23G   78G  23% /install
:/vol1_XY0_PL0/q_dbhome           100G   23G   78G  23% /sybase/PL0
:/vol3_XY0_PL0/HDB_PL0            1.0G  4.8M 1020M   1% /hana_backup/PL0/data
:/vol1_XY0_PL0/q_dblog            100G   23G   78G  23% /hana/log/PL0
:/vol1_XY0_PL0/q_sysfiles/hana    100G   23G   78G  23% /hana/shared/PL0
:/vol3_XY0_PL0/q_dbarch           1.0G  4.8M 1020M   1% /sybase/PL0/saparch_1
:/vol1_XY0_PL0/q_dbdump           100G   23G   78G  23% /sybase/PL0/shmdump
:/vol1_XY0_PL0/q_dblog            100G   23G   78G  23% /sybase/PL0/saplog_1

free memory in the system now
             total       used       free     shared    buffers     cached
Mem:        128922      65081      63841        607        579      28476
-/+ buffers/cache:      36025      92897
Swap:        20479         76      20403

output of uptime command
 11:10am  up 79 days  2:20,  1 user,  load average: 0.19, 3.69, 3.22

io statistics
Linux 3.12.74-60.64.107.1.17187.0.PTF.1126890-default (hec01v031100)    03/05/2020      _x86_64_        (40 CPU)

avg-cpu:  %user   %nice %system %iowait  %steal   %idle
           0.72    0.00    0.29    0.01    0.00   98.97

Device:            tps    kB_read/s    kB_wrtn/s    kB_read    kB_wrtn
xvda              1.39         2.06        20.03   14072349  136903676
xvdb             12.40         1.46        71.67    9971369  489811208
xvdc              0.69         0.09         8.26     623249   56442324
xvdd              0.07         0.38         0.28    2618305    1939448
xvde              0.63         1.66         4.22   11356101   28809664
xvdy              0.75         1.51         0.00   10313048          0
xvdz              0.19         1.16         2.43    7896116   16575008


Access data from file in linux scripts

instead of search for one particular word line by line in file using the below command in scripts

In manual way of running Linux command, more command is useful to search for key word.

The follow method can be used to search for a key word in file in script point of view.

  1. To grep error messages from log file.
                  grep -i error /usr/sap/sid/work/dev_disp.log
      2.To grep warming messages from log file.
                 grep -i warning /usr/sap/sid/work/dev_disp.log
      3. To grep  for fail  key word in the file.
                grep -i fail /usr/sap/sid/work/dev_disp.log


Output to file:To send output  of above cmd to one file use syntax as below:



grep  -i error /usr/sap/sid/work/dev_disp.log > /install/error.log

Tuesday, 11 February 2020

host connectivity check using Linux scripts using exit code


cat connect_check.sh
#!/bin/bash
HOST="google.com"
ping -c 4 $HOST
if [ "$?" -eq "1" ]                        #exit code test condition
then
echo " $HOST is reachable "
else
echo " $HOST unreachable "
fi




Output

./connect_check.sh
PING google.com (172.217.17.78) 56(84) bytes of data.
From 10.188.34.2 icmp_seq=1 Destination Host Unreachable
From 10.188.34.2 icmp_seq=2 Destination Host Unreachable
From 10.188.34.2 icmp_seq=3 Destination Host Unreachable
From 10.188.34.2 icmp_seq=4 Destination Host Unreachable

--- google.com ping statistics ---
4 packets transmitted, 0 received, +4 errors, 100% packet loss, time 2998ms

 google.com is reachable



Position parameters in linux scripts



#!/bin/bash
echo "check your employee details: "
echo " running '$0'"
echo " SAP employee id: '$1'"
echo " TCS employee id: '$2'"
echo " Contact number: '$3'"
echo " TCS cubic number: '$4'"
echo " Building : '$5'"
echo " location : '$6'"
echo " job role : '$7'"
echo " ODC: '$8'"
echo " Name: '$9'"
echo " date: '${10}'"   #from 10 th position parameter on wards 's{10}' is mandatory. 

Output

Hostname:/install # ./pp.sh CXX9350  1XX1350   8XXXXXX2 8 victor Bangalore BASIS 7 Naveen 11_02_2020
check your employee details:
 running './pp.sh'
 SAP employee id: 'CXX9350'
 TCS employee id: '1XX1350'
 Contact number: '8XXXXXX2 '
 TCS cubic number: '8'
 Building : 'victor'
 location : 'Bangalore'
 job role : 'BASIS'
 ODC: '7'
 Name: 'Naveen'
 date: '11_02_2020'


Friday, 7 February 2020

if condition in linux scripts


Example for if condition: