Showing posts with label SAMPLE SHELL SCRIPTS FOR FILE SYSTEM ALERT ON ORACLE DB. Show all posts
Showing posts with label SAMPLE SHELL SCRIPTS FOR FILE SYSTEM ALERT ON ORACLE DB. Show all posts

Sunday, May 7, 2023

File System Free Space Automatically Shell Script Linux

 touch freethespace.sh
chmod 777 freethespace.sh
vim freethespace.sh
------------------------------------
#!/bin/bash
echo "Enter year:"
read year
echo "Enter month:"
read month
echo "Enter day:"
read day
rm -rf ${year}_${month}_${day}_*
#rm -rf $(date --date='1 days ago' '+%Y_%m_%d')*
================================================
To check file exists in current directory :
------------------------------------------------
#!/bin/bash
echo "Enter your file to be search:"
read file
if test -f $file 
then
echo "file does exists !"
else
echo "no such file found !"
fi
===============================================
ls -lrt | awk '{for(i=6;i<=NF;i++) printf("%s ",$i); printf("\n");}' | grep "Oct 18" | awk '{for(j=4;j<=NF;j++) printf("%s ",$j); printf("\n");}'
redo_551326936_2_657440.arc
redo_551326936_2_657441.arc
redo_551326936_1_758270.arc
redo_551326936_1_758271.arc
redo_551326936_1_758272.arc
redo_551326936_2_657442.arc
redo_551326936_1_758273.arc
redo_551326936_1_758274.arc
redo_551326936_1_758275.arc
redo_551326936_2_657443.arc
redo_551326936_1_758276.arc
redo_551326936_1_758277.arc
redo_551326936_2_657444.arc
redo_551326936_1_758278.arc
redo_551326936_1_758279.arc

ls -lrt | awk '{for(i=6;i<=NF;i++) printf("%s ",$i); printf("\n");}' | grep "Dec 22" | awk '{for(j=4;j<=NF;j++) printf("%s ",$j); printf("\n");}' | while read i; 
do rm -rf  $i ; done 
or 
ls -lrt *.arc | awk '{for(i=6;i<=NF;i++) printf("%s ",$i); printf("\n");}' | grep "Dec 23" | awk '{print $4}' | while read i; do rm -rf $i; done 

ads00
ls -lrt | head -n 500 | awk '{for(i=6;i<=NF;i++) printf("%s ",$i); printf("\n");}' | awk '{print $4}' | while read n; do rm -rf $n; done 
=========================================================================
for host and others to free up *.arc files 
freeup.sh
--------------------------------------
#!/bin/bash
target_dir=/pac/host/bkup/orabkup01/host/archivelog/
target_dir_no_of_files=$(ls -lrt /pac/host/bkup/orabkup01/host/archivelog/*.arc | wc -l)
if [ "${target_dir_no_of_files}" -gt "3000" ]
then
 echo "you can remove files"
 ls -lrt /pac/host/bkup/orabkup01/host/archivelog/*.arc | head -n 2000 | awk '{print $9}' | while read n; do rm -rf  $n; done
else
  echo "files less than 3000"
fi
=========================================================================

host:/pac/host/arch/host/archivelog/ $ echo `date --date='1 days ago' +'%Y_%m_%d'`
2022_12_08
---------------------------------------------------------------------------------------------------------
ls -lrt 2022_12_10/*.arc | head -n 10000 | awk '{print $9}' | while read n; do rm -rf  $n; done
-----------------------------------------------------------------------------------------------------------------------------
62G     2022_12_10
463G    2022_12_11
host:/pac/host/arch/host/archivelog/ $ ls -lrth `date --date='1 days ago' +'%Y_%m_%d'`/*.arc | wc -l
1453
host:/pac/host/arch/host/archivelog/ $ ls -lrt 2022_12_10/*.arc | wc -l
1453
host:/pac/host/arch/host/archivelog/ $ ls -lrth `date --date='0 days ago' +'%Y_%m_%d'`/*.arc | wc -l
10695
host:/pac/host/arch/host/archivelog/ $ echo `date --date='0 days ago' +'%Y_%m_%d'`
2022_12_11
pwd
ls -lrth /pac/host/arch/host/archivelog/`date --date='0 days ago' +'%Y_%m_%d'`/*.arc  2>/dev/null | wc -l
ls -lrth /pac/host/arch/host/archivelog/`date --date='1 days ago' +'%Y_%m_%d'`/*.arc  2>/dev/null | wc -l

/pac/host/arch/host/archivelog/`date --date='0 days ago' +'%Y_%m_%d'`/*.arc

=========================================================================
#!/bin/bash
target_dir=/pac/host/bkup/orabkup01/host/archivelog/
target_dir_no_of_files=$(ls -lrt /pac/host/bkup/orabkup01/host/archivelog/*.arc | wc -l)
if [ "$(ps -ef | grep ora_pmon_ | wc -l)" -gt "1" ]
then
if [ "${target_dir_no_of_files}" -gt "3000" ]
then
 echo "you can remove files"
 ls -lrt /pac/host/bkup/orabkup01/host/archivelog/*.arc | head -n 2000 | awk '{print $9}' | while read n; do rm -rf  $n; done
 df -h /pac |  mail -s "Space has been free now on host " example@gmail.com
else
  echo "files less than 3000"
fi
fi
=========================================================================
#!/bin/bash

target_dir_yesterday=/pac/host/arch/host/archivelog/$(date --date='1 days ago' +'%Y_%m_%d')
target_dir_today_no_of_files=$(ls -lrt /pac/host/arch/host/archivelog/$(date --date='0 days ago' +'%Y_%m_%d')/*.arc | wc -l)
#echo $target_dir_yesterday
#echo $target_dir_today_no_of_files
#echo $(ps -ef | grep ora_pmon_ | wc -l)
if [ "$(ps -ef | grep ora_pmon_ | wc -l)" -gt "1" ]
then
if [  "${target_dir_today_no_of_files}" -gt "10000"  ]
then
 echo "you can remove files"
 #to delete the archivelogs
 ls -lrt $target_dir_yesterday/*.arc 2>/dev/null | awk '{print $9}' | while read m; do rm -rf $m; done
 ls -lrt /pac/host/arch/host/archivelog/$(date --date='0 days ago' +'%Y_%m_%d')/*.arc | head -n 8000 | awk '{print $9}' | while read n;do rm -rf  $n; done
 # mail to respect groups
 df -h /pac |  mail -s "Space has been free now on host " example@gmail.com
else
  echo "files less than 3000"
fi
fi
=========================================================================
host:/pac/host/arch/host/archivelog/ $ df -h | awk '{print $5,$6}'
Use% Mounted
0% /dev
0% /dev/shm
11% /run
0% /sys/fs/cgroup
24% /
26% /boot
77% /usr/local/opt/oracle
1% /opt/app/ggs
72% /pac
13% /var
1% /tmp
78% /pac/prod_backup2
48% /pac/prod_backup
0% /run/user/0
0% /run/user/4264524
0% /run/user/3095009
0% /run/user/1563708
65% /nas/osd
host:/pac/host/arch/host/archivelog/ $ df -h | awk '{print $5,$6}' | awk '{print $2}' | grep /usr/local/opt/oracle
/usr/local/opt/oracle
host:/pac/host/arch/host/archivelog/ $ df -h | awk '{print $5,$6}' | awk '{print $2}' | grep /usr/local/opt/oracle | read a
host:/pac/host/arch/host/archivelog/ $ echo $a
/usr/local/opt/oracle
=========================================================================
df -h /pac | awk '{print $5,$6}' | grep "60" | awk '{print $1}'
60%
host:/pac/host/arch/host/archivelog/ $ cat ch.sh
#!/bin/bash
chk=$(df -h /pac | awk '{print $5,$6}' | grep "44" | awk '{print $1}' | grep -o "\w[[:digit:]]" )
if [[ "${chk}" -eq "44" ]]
then
 echo "/pac is 44% full"
fi
-----------------------------------------------------------------------------------------------------------------------------
for i in 24 25; do ls -lrth *.arc | awk '{for(i=6;i<=NF;i++) printf("%s ",$i); printf("\n");}' | grep "Dec $i" | awk '{print $4}' | while read n; do echo $n; done; done
=========================================================================
#!/bin/sh

check_trails_count=`ls -lrt /opt/app/ggs/trails/lt* | wc -l`
check_space=`df -h /opt/app/ggs | awk '{print $5}' | grep "[[:digit:]]" | cut -b1-2`
check_gg=`ps -ef | grep -i PARAM | grep -i -e "\.prm" | wc -l`
if [[ "${check_trails_count}" -gt "100" ]] &&  [[  "${check_space}" -gt "90" ]] && [[ "${check_gg}" -gt "0" ]] 
then
echo "yes"
ls -lrt /opt/app/ggs/trails/lt* | head -n 50 | awk '{print $9}' | while read n; do rm -rf $n; done
df -h /opt/app/ggs | mail -s "Free the Space on /opt/app/ggs on host" example@gmail.com
fi
location: /opt/app/ggs/trails/trails_free_up.sh 
=========================================================================
#!/bin/sh



count_arch=`ls -lrt /pac/lci8t1/arch/oraarch01/*.arc | wc -l`
check_oracle=`ps -ef | grep -i ora_pmon_${ORACLE_SID} | grep -v grep | grep -i pmon | wc -l`               # best command to find oracle is running or not #
if [[ "${count_arch}" -gt "500" ]] && [[ "${check_oracle}" -gt "0" ]]
then
echo "${count_arch}"
ls -lrt /pac/lci8t1/arch/oraarch01/*.arc | head -n 200 | awk '{print $9}' | while read n; do rm -rf $n; done;
df -h /pac | mail -s "Space has been free ${hostname} " example@gmail.com
fi
=========================================================================
Shell Script For Auto Backup old trail files and free the space in the mount point then sent mail
$ cat freeuptrails.sh
#!/bin/sh


PATH=/usr/bin:/sbin: ; export PATH    # exporting path for using TAR in this shell script otherwise tar won't work 


count_files=$(ls -lrth /opt/app/ggs/trails/hostname/lt* | wc -l)
nfiles=`echo "scale=1; 2/5" | bc`
files_count_delete=`echo "scale=0; $nfiles*$count_files" | bc`
result=`printf "%.0f\n" $files_count_delete`

echo $result
echo $count_files

files=$(ls -lrth /opt/app/ggs/trails/hostname/lt* | head -n $result )

disk_usage=$(df -h /opt/app/ggs/trails/hostname | awk '{print $5}' | grep -e "[[:digit:]]" | awk -F% '{print $1,$2}')

if [[ "${disk_usage}" -gt "90" ]]
then
        if test ${result} -lt ${count_files}
        then
               # check if old backup.tar.gz exists if yes then delete it
               bkp_file=/opt/app/ggs/trails/hostname/backup.tar.gz
               if test -f ${bkp_file}
               then
                   rm -f /opt/app/ggs/trails/hostname/backup.tar.gz
               fi
               # make backup directory for storing old trails files
                mkdir /opt/app/ggs/trails/hostname/backup

                # for loop to move old trails files to backup folder
                for i in ${files}
                do
                        mv $i /opt/app/ggs/trails/hostname/backup
                done



                # tar the backup folder
                 
                  tar -czf /opt/app/ggs/trails/hostname/backup.tar.gz -C /opt/app/ggs/trails/hostname backup  --remove-files
                wait
                echo "work is done"
                send=$(df -h /opt/app/ggs)
                sendone=$(echo "current trail files count:"; ls -lrth /opt/app/ggs/trails/hostname/lt* | wc -l)
                sendtwo=$(ls -lrth /opt/app/ggs/trails/hostname/backup.tar.gz)
                echo -e "$send\n$sendone\n$sendtwo" |  mailx -s "Space free now on hostname /opt/app/ggs" exampel@gmail.com
          fi

fi

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++



createuser.sh 
-----------------------------------------------------------------------------------------------------------------------------------------------
#!/bin/bash
echo "enter username:"
read user;
echo "enter default tablespace:"
read default_tablespace;
echo "password:"
read password;

sqlplus / as sysdba << EOF > log.txt
alter session set "_oracle_script"=true ;
create user ${user} identified by ${password} default tablespace ${default_tablespace} temporary tablespace temp quota unlimited on users;
grant dba to ${user};
exit;
EOF

sqlplus ${user}/${password} << EOF > ${user}.txt
show user;
exit;
EOF
################################################################################################################################################

userdrop.sh
---------------------------------------------------------------------------------
#!/bin/bash

echo "Enter username:"
read user
echo "password:"
read password

sqlplus / as sysdba << EOF > ${user}drop.txt
alter session set "_oracle_script"=true;
drop user ${user} cascade;
exit;
EOF
#########################################################################################################

createtablespace.sh
--------------------------------------------------------------------------
#!/bin/bash

echo "Enter tablespace_name:"
read tablespace_name
echo "path:"
read path
echo "Size:"
read size
echo "autoextend on/off"
read autoextend_on_off
echo "tablespace_file_no:"
read tablespace_file_no

sqlplus / as sysdba << EOF > ${tablespace_name}created.txt
create tablespace ${tablespace_name}
datafile '${path}${tablespace_name}_${tablespace_file_no}.dbf'
size ${size}
autoextend ${autoextend_on_off};
exit
EOF
###########################################################################################################

droptablespace.sh
----------------------------------------------------------------------------------------
#!/bin/bash


echo "Enter tablespace_name:"
read tablespace_name

sqlplus / as sysdba << EOF > ${tablespace_name}drop.txt
drop tablespace ${tablespace_name} including contents and datafiles;
exit;
EOF

########################################################################################################

mkscript.sh
---------------------------------------------------------------------------------------------
#!/bin/bash

echo "Enter script name:"
read script_name
echo "Enter permission"
read permission

touch ${script_name}
chmod ${permission} ${script_name}
##########################################################################################################

createcontrolfile.sql
----------------------------------------------------------------------------------------------------
create controlfile                 <-------------------------
set database db_name                                        |
logfile group 1 ('/u02/app/oracle/db_name/redo01_01.log',   |                    #only change: /u02/app/oracle/db_name remaining all remains as it is
                '/u02/app/oracle/db_name/redo01_02.log'),   |                    # alter database backup controlfile to trace;
        group 2 ('/u02/app/oracle/db_name/redo02_01.log',   |                    # adrci > show home then $ cd /rdbms/trace/ $ ls -lrt alert_${db_name}.trc - copy the content to c.sql
                '/u02/app/oracle/db_name/redo02_02.log'),   |                                                                                                |
        group 3 ('/u02/app/oracle/db_name/redo03_01.log',   |                                                                                                |
                '/u02/app/oracle/db_name/redo03_02.log')    |-------------------------------------------------------------------------------------------------
resetlogs                                                   | 
datafile '/u02/app/oracle/db_name/system01.dbf' size 30M,   |
         '/u02/app/oracle/db_name/sysaux01.dbf' size 50M,   |
         '/u02/app/oracle/db_name/users01.dbf' size 50M,    |
         '/u02/app/oracle/db_name/temp01.dbf' size 50M      |
maxlogfiles 50                                              |
maxlogmembers 3                                             |
maxloghistory 400                                           |
maxdatafiles 200                                            |
maxinstances 6                                              |
archivelog;                  <-------------------------------                               

############################################################################################################

freespace.sh
--------------------------------------------------------------------------------------------
echo "path"
read path
echo "year:"
read year
echo "month:"
read month
echo "day:"
read day
echo "find type option f or d"
read option
echo "days_of_files_to_kept"
read days_of_files_to_kept

rm -rf ${year}_${month}_${day}_*

# or can be used as below 
find ${path} -type ${option} -mtime +${days_of_files_to_kept} -exec rm {} +

####################################################################################################################

extract.sh --> scripts to get query results without going inside database
-----------------------------------------------------------------------------
#!/bin/bash

echo "Enter username:"
read user
echo "Enter password:"
read password
echo "Enter statement or query:"
read query

sqlplus ${user}/${password} << EOF > logon.txt
$query
exit;
EOF

#############################################################################################################################

switch.sh ---> scripts for listing of all directories and files
---------------------------------------------------------------------
#!/bin/bash

echo "Enter path:"
read path

for i in $path
do
echo "list of files or folders"
ls -lrth  ${i}
done

##############################################################################################################################

search.sh ----> scripts for 
-------------------------------------------------------
#!/bin/bash



count=$(wc -m file | tr -cd "[:digit:]")

i=1
while [[ $i -le $count ]]
do
cut -b $i file
      (( i++ ))
done

#############################################################################################################################
bkup script is running or not :
--------------------------------
#!/bin/bash


bkp_running=$(ps -ef | grep rman)

if [[ "$bkp_running" = "TRUE" ]]
then
echo "bkp is running"
else
echo "bkp is not running"
fi

###############################################

Enable OpenSSH on Windows 11

Step 1: Install OpenSSH Server You can do this via PowerShell (run as Administrator ): Check if it's already available: Get-WindowsCapab...