RMAN Backup Using Shell Script
REQUIREMENT:
Configure shell script to take rman Full backup .
SCRIPT: ( rman_full_bkp.sh)
1
2
3 # $Header: rman_full_bkp.sh
4 # *====================================================================================+
5 # | Author - DBACLASS ADMIN TEAM
6 # | |
7 # +====================================================================================+
8 #|
9 # | DESCRIPTION
10 # | Take rman full backup(incremental level 0 )
11 # | PLATFORM
12 # | Linux/Solaris/HP-UX
13
14 # +===========================================================================+
15 #!/bin/bash
16 RMANBACKUP_MOUNTPOINT1=/u01/oracle/rman_bkp
17 PARALLELISM=4
18 MAXPIECESIZE=3g
19 export ORACLE_HOME=/u01/app/oracle/product/12.1.0/dbhome_1
20 export ORACLE_SID=TESTDB
21 export PATH=$ORACLE_HOME/bin:$PATH
22
23 fullBackup () {
24 rman log=/u01/oracle/rman_bkp/LOG/RMANFULLincr.log << EOF
25 connect target /
26 set echo on;
27 configure backup optimization on;
28 configure controlfile autobackup on;
29 configure controlfile autobackup format for device type disk to '$RMANBACKUP_MOUNTPOINT1/%F';
30 configure maxsetsize to unlimited;
31 configure device type disk parallelism $PARALLELISM;
32 run
33 {
34 allocate channel c1 type disk format '${RMANBACKUP_MOUNTPOINT1}/%I-%Y%M%D-%U' maxpiecesize ${MAXPIECESIZE};
35 allocate channel c2 type disk format '${RMANBACKUP_MOUNTPOINT1}/%I-%Y%M%D-%U' maxpiecesize ${MAXPIECESIZE};
36 allocate channel c3 type disk format '${RMANBACKUP_MOUNTPOINT1}/%I-%Y%M%D-%U' maxpiecesize ${MAXPIECESIZE};
37 allocate channel c4 type disk format '${RMANBACKUP_MOUNTPOINT1}/%I-%Y%M%D-%U' maxpiecesize ${MAXPIECESIZE};
38 backup as compressed backupset incremental level 0 check logical database plus archivelog;
39 release channel c1 ;
40 release channel c2 ;
41 release channel c3 ;
42 release channel c4 ;
43 }
44 configure backup optimization clear;
45 configure controlfile autobackup clear;
46 sql 'alter system archive log current';
47 exit
48 EOF
49 }
50
51 # Main
52
53 fullBackup
54
Give 755 permission
1
2
3 chmod 755 rman_full_bkp.sh
4
Configure script in crontab:
1
2
3 00 22 * * * "/u01/app/oracle/rman_full_bkp.sh" > /tmp/rmanarch.log
4
Shell script to monitor asm diskgroup usage
REQUIREMENT: Write a shell script, which will trigger a mail alert, if the utilization of the asm diskgroup reached 90 percent. SOLUTION: 1. Below is the shell script. Make
sure to update ORACLE_HOME, ORACLE_SID inside the shell script. cat /export/home/oracle/asm_dg.sh
1
2
3 export ORACLE_HOME=/oracle/app/oracle/product/12.1.0.2/dbhome_1
4 export ORACLE_SID=PRODDB1
5 export PATH=$ORACLE_HOME/bin:$PATH
6 logfile=/export/home/oracle/asm_dg.log
7 sqlplus -s "/as sysdba" > /dev/null << EOF spool $logfile
8 SET LINESIZE 150
9 SET PAGESIZE 9999
10 SET VERIFY off
11 COLUMN group_name
12 FORMAT a25 HEAD 'DISKGROUP_NAME'
13 COLUMN state FORMAT a11 HEAD 'STATE'
14 COLUMN type FORMAT a6 HEAD 'TYPE'
15 COLUMN total_mb FORMAT 999,999,999 HEAD 'TOTAL SIZE(GB)'
16 COLUMN free_mb FORMAT 999,999,999 HEAD 'FREE SIZE (GB)'
17 COLUMN used_mb FORMAT 999,999,999 HEAD 'USED SIZE (GB)'
18 COLUMN pct_used FORMAT 999.99 HEAD 'PERCENTAGE USED'
19
20 SELECT distinct name group_name , state state , type type ,
21 round(total_mb/1024) TOTAL_GB , round(free_mb/1024) free_gb ,
22 round((total_mb - free_mb) / 1024) used_gb ,
23 round((1- (free_mb / total_mb))*100, 2) pct_used from
24 v$asm_diskgroup where round((1- (free_mb / total_mb))*100, 2) > 90 ORDER BY name;
25 spool off
26 exit
27 EOF
28 count=`cat $logfile|wc -l`
29 #echo $count
30 if [ $count -ge 4 ];
31 then
32 mailx -s "ASM DISKGROUP REACHED 90% UTILIZATION" [email protected] < $logfile
33 fi
34
2. Give proper permission:
1
2
3 chmod 755 /export/home/oracle/asm_dg.sh
4
3. Configure in crontab:
1
2
3 0,15,30,45 * * * * /export/home/oracle/asm_dg.sh
4
Shell script to report failed login attempt in oracle
Requirement: Configure a shell script in crontab, that will send alert to DB support Team, in the case of any invalid login attempts in the database. 1. First, enable audit
for create session
1
2
3 SQL> audit create session;
4
5 Audit succeeded.
6
2. Final shell script Below script for any invalid login attempts in last 15 minutes. cat /export/home/oracle/invalid_log.sh
1
2
3 export ORACLE_HOME=/oracle/app/oracle/product/12.1.0/dbhome_1
4 export ORACLE_SID=SBIP18DB
5 export PATH=$ORACLE_HOME/bin:$PATH
6 logfile=/export/home/oracle/test.log
7 sqlplus -s "/as sysdba" > /dev/null << EOF
8 spool $logfile
9 set pagesize 1299
10 set lines 299
11 col username for a15
12 col userhost for a13
13 col timestamp for a39
14 col terminal for a23
15 SELECT username,userhost,terminal,to_char(timestamp,'DD/MM/YY HH24:MI:SS' ) "TIMESTAMP" ,
16 CASE
17 when returncode=1017 then 'INVALID-attempt'
18 when returncode=28000 then 'account locked'
19 end "FAILED LOGIN ACTION"
20 FROM dba_audit_session where timestamp > sysdate-1/9and returncode in (1017,28000);
21 spool off
22 exit
23 EOF
24 count=`cat $logfile|wc -l`
25 #echo $count
26 if [ $count -ge 4 ];
27 then
28 mailx -s "INVALID ATTEMPS IN DB " [email protected] < $logfile
29 fi
30
3. provide […]
shell script for file system alert
Below is script for sending notification ,when a mount point or filesystem crosses a threshold value. For solaris
1
2
3 #!/bin/sh
4
5 df -h | egrep -v '/system|/platform|/dev|/etc|lib' | awk '{print $6 " " $5}'|cut -d% -f1|while read fs val
6
7 do
8
9 if [ $val -ge 90 ]
10 then
11 echo "The $fs usage high $val% \n \n \n `df -h $fs`" | mailx -s "Filesystem $fs Usage high on Server `hostname`" [email protected]
12
13 fi
14 done
15
Put in crontab:
1
2
3 00 * * * * /usr/local/scripts/diskalert.sh
4
For monitoring zpool usage in solaris:
1
2
3 zpool list | awk '{print $5}'| grep -v CAP | cut -d% -f1| while read val
4
5 do
6
7 if [ $val -ge 80 ]
8 then
9 echo "The $fs usage high $val% \n \n \n `df -h $fs`" | mailx -s "Filesystem $fs Usage high on Server `hostname`" [email protected]
10
11 fi
12 done
13
Put in crontab as below:
1
2
3 00 * * * * /usr/local/scripts/zpoolusage.sh
4
Alert Log Rotation Script In Oracle
Day by day, alert log size will grow in Oracle database. So for housekeeping, we need to move the existing alert log to a backup location and
compress there. Upon moving the alert log, the database will create a fresh alert log automatically.
1. Below is the shell script.
WE need to define the ORACLE_HOME in the script. and ORACLE_SID will be passed as an argument while running the script.
1
2
3 # $Header: rotatealertlog.sh
4 # *====================================================================================+
5 # | AUTHOR : DBACLASS SUPPORT TEAM
6 # | |
7 # +====================================================================================+
8 #|
9 #!/bin/bash
10 echo ========================
11 echo Set Oracle Database Env
12 echo ========================
13
14 ORACLE_SID=$1; export ORACLE_SID
15 ORACLE_HOME=/oracle/app/oracle/product/12.1.0.2/dbhome_1
16 ORACLE_BASE=/oracle/app/oracle; export ORACLE_BASE
17 LD_LIBRARY_PATH=$ORACLE_HOME/lib:/usr/lib; export LD_LIBRARY_PATH
18 PATH=$ORACLE_HOME/bin:$PATH;export PATH
19 TO_DATE="20`date +%y%m%d`"; export TO_DATE
20
21 echo =======
22 echo Extract Alert log location
23 echo =======
24 export VAL_DUMP=$(${ORACLE_HOME}/bin/sqlplus -S /nolog < ${EALERTDB}
25 else
26 echo not found
27 fi
28
29 exit
30
2. Configure in crontab:
SCHEDULE – Weekly once
Here, we have passed the ORACLE_SID (PRODDB) as argument
1
2
3 00 22 * * 5 /u01/app/oracle/dbscripts/rotatealertlog.sh PRODDB
4
Tablespace Monitoring Shell Script
Below script can be configured in crontab to send a notification to the support DBAs in case tablespace usage crosses a threshold.
1. First, make the below .sql file, which will be used inside the shell script.
In this script we have defined the threshold as 90%. You can change it as per your requirement.
1
2
3 cat /export/home/oracle/Housekeeping/scripts/tablespace_alert.sql
4
5
6
7 set feedback off
8 set pagesize 70;
9 set linesize 2000
10 set head on
11 COLUMN Tablespace format a25 heading 'Tablespace Name'
12 COLUMN autoextensible format a11 heading 'AutoExtend'
13 COLUMN files_in_tablespace format 999 heading 'Files'
14 COLUMN total_tablespace_space format 99999999 heading 'TotalSpace'
15 COLUMN total_used_space format 99999999 heading 'UsedSpace'
16 COLUMN total_tablespace_free_space format 99999999 heading 'FreeSpace'
17 COLUMN total_used_pct format 9999 heading '%Used'
18 COLUMN total_free_pct format 9999 heading '%Free'
19 COLUMN max_size_of_tablespace format 99999999 heading 'ExtendUpto'
20 COLUM total_auto_used_pct format 999.99 heading 'Max%Used'
21 COLUMN total_auto_free_pct format 999.99 heading 'Max%Free'
22 WITH tbs_auto AS
23 (SELECT DISTINCT tablespace_name, autoextensible
24 FROM dba_data_files
25 WHERE autoextensible = 'YES'),
26 files AS
27 (SELECT tablespace_name, COUNT (*) tbs_files,
28 SUM (BYTES/1024/1024) total_tbs_bytes
29 FROM dba_data_files
30 GROUP BY tablespace_name),
31 fragments AS
32 (SELECT tablespace_name, COUNT (*) tbs_fragments,
33 SUM (BYTES)/1024/1024 total_tbs_free_bytes,
34 MAX (BYTES)/1024/1024 max_free_chunk_bytes
35 FROM dba_free_space
36 GROUP BY tablespace_name),
37 AUTOEXTEND AS
38 (SELECT tablespace_name, SUM (size_to_grow) total_growth_tbs
39 FROM (SELECT tablespace_name, SUM (maxbytes)/1024/1024 size_to_grow
40 FROM dba_data_files
41 WHERE autoextensible = 'YES'
42 GROUP BY tablespace_name
43 UNION
44 SELECT tablespace_name, SUM (BYTES)/1024/1024 size_to_grow
45 FROM dba_data_files
46 WHERE autoextensible = 'NO'
47 GROUP BY tablespace_name)
48 GROUP BY tablespace_name)
49 SELECT c.instance_name,a.tablespace_name Tablespace,
50 CASE tbs_auto.autoextensible
51 WHEN 'YES'
52 THEN 'YES'
53 ELSE 'NO'
54 END AS autoextensible,
55 files.tbs_files files_in_tablespace,
56 files.total_tbs_bytes total_tablespace_space,
57 (files.total_tbs_bytes - fragments.total_tbs_free_bytes
58 ) total_used_space,
59 fragments.total_tbs_free_bytes total_tablespace_free_space,
60 round(( ( (files.total_tbs_bytes - fragments.total_tbs_free_bytes)
61 / files.total_tbs_bytes
62 )
63 * 100
64 )) total_used_pct,
65 round(((fragments.total_tbs_free_bytes / files.total_tbs_bytes) * 100
66 )) total_free_pct
67 FROM dba_tablespaces a,v$instance c , files, fragments, AUTOEXTEND, tbs_auto
68 WHERE a.tablespace_name = files.tablespace_name
69 AND a.tablespace_name = fragments.tablespace_name
70 AND a.tablespace_name = AUTOEXTEND.tablespace_name
71 AND a.tablespace_name = tbs_auto.tablespace_name(+)
72 and (((files.total_tbs_bytes - fragments.total_tbs_free_bytes)/ files.total_tbs_bytes))* 100 > 90
73 order by total_free_pct;
74
2. Now prepare the shell script:
At the beginning of the script, we need to define the env variables like ORACLE_HOME, PATCH, LD_LIBRARY_PATH, ORACLE_SID.
Below is the final script(tablespace_threshold.ksh)
1
2
3 cat /export/home/oracle/Housekeeping/scripts/tablespace_threshold.ksh
4
5
6
7 #!/bin/sh
8 export ORACLE_HOME=/u01/app/oracle/product/12.1.0/dbhome_1
9 export PATH=$ORACLE_HOME/bin:$PATH
10 export LD_LIBRARY_PATH=$ORACLE_HOME/lib
11 export ORACLE_SID=PRODDB
12 cd /export/home/oracle/Housekeeping/scripts
13 logfile=/export/home/oracle/Housekeeping/scripts/Tablespace_alert.log
14 cnt1=`ps -ef|grep pmon|grep $ORACLE_SID|wc -l`
15 if [ $cnt1 -eq 1 ];
16 then
17 sqlplus -s "/as sysdba" > /dev/null << EOF
18 spool $logfile
19 @/export/home/oracle/Housekeeping/scripts/tablespace_alert.sql
20 spool off
21 exit
22 EOF
23 # If there are more then these two lines in the output file, mail it.
24 count=`cat $logfile|wc -l`
25 #echo $count
26 if [ $count -ge 4 ];
27 then
28 mailx -s "TABLESPACE ALERT FOR PROD DB " [email protected] <$logfile
29 fi
30 fi
31
3. Now configure in crontab:
1
2
3 0,15,30,45 * * * * /export/home/oracle/Housekeeping/scripts/tablespace_threshold.ksh > /export/home/oracle/Housekeeping/logs/ts_alert.log 2>&1
4
Shell Script For Monitoring Alert Log
Requirement:
Configure a shell script to monitor alert log for all the databases on a server once in every 15 min.And in the case of any ORA- error mail to the DBA TEAM.
Below script is prepared using the ADRCI utility of oracle 11g. It will monitor alert log for all the databases having same oracle base.
SCRIPT:(Adrci_alert_log.ksh)
1
2
3 ###########################################################################################
4 ###### ALERT LOG CHECKING VIA ADRCI #######################################################
5 ###### Author - DBACLASS ADMIN #######################################################
6 ###########################################################################################
7
8 LOG_DIR=/export/home/oracle/Housekeeping/logs/alert_log_check_daily.txt
9 adrci_homes=( $(adrci exec="show homes" | egrep -e rdbms ))
10 echo '#########################################################################################################' > $LOG_DIR
11 echo '###########################################ALERT LOG OUTPUT FOR LAST 15 MINUTES ###########################' >> $LOG_DIR
12 echo '##########################################################################################################' >> $LOG_DIR
13
14 for adrci_home in ${adrci_homes[@]}
15 do
16
17 echo ' '>>$LOG_DIR
18 echo '##########################################################################################################' >> $LOG_DIR
19 echo '##########################################################################################################' >> $LOG_DIR
20 echo ' '>>$LOG_DIR
21 echo $adrci_home' Alert Log' >> $LOG_DIR
22 adrci exec="set home ${adrci_home}; show alert -p \\\"message_text like '%ORA-%' and originating_timestamp > systimestamp-1/96\\\"" -term >> $LOG_DIR
23
24 done
25 num_errors=`grep -c 'ORA' $LOG_DIR`
26 if [ $num_errors != 0 ]
27 then
28
29 mailx -s "ORA- error found in alert Log of the server " [email protected] <$LOG_DIR
30
31 fi
32
Give 755 permission to the script
1
2
3 chmod 755 Adrci_alert_log.ksh
4
Configure the script in crontab:
1
2
3 0,15,30,45 * * * * /export/home/oracle/Housekeeping/scripts/Adrci_alert_log.ksh > /export/home/oracle/Housekeeping/logs/error_alert.log 2>&1
4
RMAN backup using shell script
REQUIREMENT: Configure shell script to take rman Full backup . SCRIPT: ( rman_full_bkp.sh)
1
2
3 # $Header: rman_full_bkp.sh
4 # *====================================================================================+
5 # | Author - DBACLASS ADMIN TEAM
6 # | |
7 # +====================================================================================+
8 #|
9 # | DESCRIPTION
10 # | Take rman full backup(incremental level 0 )
11 # | PLATFORM
12 # | Linux/Solaris/HP-UX
13
14 # +===========================================================================+
15 #!/bin/bash
16 RMANBACKUP_MOUNTPOINT1=/u01/oracle/rman_bkp
17 PARALLELISM=4
18 MAXPIECESIZE=3g
19 export ORACLE_HOME=/u01/app/oracle/product/12.1.0/dbhome_1
20 export ORACLE_SID=TESTDB
21 export PATH=$ORACLE_HOME/bin:$PATH
22
23 fullBackup () {
24 rman log=/u01/oracle/rman_bkp/LOG/RMANFULLincr.log << EOF
25 connect target /
26 set echo on;
27 configure backup optimization on;
28 configure controlfile autobackup on;
29 configure controlfile autobackup format for device type disk to '$RMANBACKUP_MOUNTPOINT1/%F';
30 configure maxsetsize to unlimited;
31 configure device type disk parallelism $PARALLELISM;
32 run
33 {
34 allocate channel c1 type disk format '${RMANBACKUP_MOUNTPOINT1}/%I-%Y%M%D-%U' maxpiecesize ${MAXPIECESIZE};
35 allocate channel c2 type disk format '${RMANBACKUP_MOUNTPOINT1}/%I-%Y%M%D-%U' maxpiecesize ${MAXPIECESIZE};
36 allocate channel c3 type disk format '${RMANBACKUP_MOUNTPOINT1}/%I-%Y%M%D-%U' maxpiecesize ${MAXPIECESIZE};
37 allocate channel c4 type disk format '${RMANBACKUP_MOUNTPOINT1}/%I-%Y%M%D-%U' maxpiecesize ${MAXPIECESIZE};
38 backup as compressed backupset incremental level 0 check logical database plus archivelog;
39 release channel c1 ;
40 release channel c2 ;
41 release channel c3 ;
42 release channel c4 ;
43 }
44 configure backup optimization clear;
45 configure controlfile autobackup clear;
46 sql 'alter system archive log current';
47 exit
48 EOF
49 }
50
51 # Main
52
53 fullBackup
54
Give 755 permission
1
2
3 chmod 755 rman_full_bkp.sh
4
Configure script in crontab:
1
2
3 00 22 * * * "/u01/app/oracle/rman_full_bkp.sh" > /tmp/rmanarch.log
4
For taking incremental backup,Just update the backup command with the below one. backup as compressed backupset incremental level 1 check logical database plus
archivelog;
Kill snipped sessions in oracle db
REQUIREMENT: Configure a shell script, to kill the snipped sessions in the database regularly. SHELL SCRIPT:
1
2
3 # $Header: kill_snipped.sh
4 # *====================================================================================+
5 # | Author - DBACLASS ADMIN TEAM
6 # | |
7 # +====================================================================================+
8 #|
9 # | DESCRIPTION
10 # | Kill the snipped session in database
11 # | PLATFORM
12 # | Linux/Solaris/HP-UX
13
14 # +===========================================================================+
15 #!/usr/bin/ksh
16 export ORACLE_HOME=/oracle/app/oracle/product/12.1.0.2/dbhome_1
17 export PATH=$ORACLE_HOME/bin:$PATH
18 export ORACLE_SID=TESTDB
19 export LOG_PATH=/export/home/oracle/testdb/LOG
20 rm -f $LOG_PATH/snip_kill.sql
21
22 sqlplus -s /nolog << EOF
23 connect system/oracle
24 select count(*) from v\$session where status='SNIPED';
25 spool $LOG_PATH/session_list.log append
26 set lines 200 pages 1500 long 99999999
27 alter session set nls_date_format='DD-MON-YYYY HH24:MI';
28 select sysdate from dual;
29 select sid,serial#,event,sql_id,last_call_et,username,status,machine,logon_time,process,substr(module,1,15) module from v\$session where status='SNIPED';
30 spool off;
31 set head off;
32 set feed off;
33 set pages 0;
34 spool $LOG_PATH/snip_kill.sql
35 select 'alter system kill session '''||sid||','||serial#||''' immediate;' from v\$session where status='SNIPED' ;
36 spool off;
37 @"$LOG_PATH/snip_kill.sql"
38 exit;
39 !
40 date
41
Give 755 permission.
1
2
3 chmod 755 kill_snipped.sh
4
Configure in crontab:
1
2
3 00,10,20,30,40,50 * * * * /export/home/oracle/testdb/snip_session.sh > /export/home/oracle/testdb/LOG/snip_session.log
4
expdp backup using shell script
Requirement: Configure a shell script, to take export backup of table daily at 3 PM and send the log to stake holders. Script: ( EXP_TAB_cmprss.sh)
1 # $Header: EXP_TAB_cmprss.sh
2 # *====================================================================================+
3 # | Author - DBACLASS ADMIN TEAM
4 # | |
5 # +====================================================================================+
6 #|
7 # | FILENAME
8 # | EXP_table_bkp_cmprss_dbaclass.sh
9 #|
10 # | DESCRIPTION
11 # | Daily Export backup script of a list of table
12 # | PLATFORM
13 # | Linux/Solaris
14
15 # +===========================================================================+
16 #!/bin/bash
17 echo Set Oracle Database Env
18 export ORACLE_SID=$1
19 export ORACLE_HOME=/oracle/app/oracle/product/12.1.0/dbhome_1
20 export LD_LIBRARY_PATH=$ORACLE_HOME/lib:/usr/lib
21 export PATH=$ORACLE_HOME/bin:$PATH:/usr/local/bin
22 export TIMESTAMP=`date +%a%d%b%Y`
23 export EXP_DIR=/export/home/oracle
24
25
echo =======
26
echo Export command
27
echo =======
28
echo $ORACLE_HOME
29
$ORACLE_HOME/bin/expdp \'/ as sysdba\' directory=DB_DUMPS dumpfile=expdp_tab_${TIMESTAMP}_%U.dmp logfile=expdp_log_${TIMESTAMP}.log tables=DBATEST.ORDER_TAB
30
PARALLEL=6 COMPRESSION=ALL
31
32
echo SEND MAIL TO STAKE HOLDERS
33
echo =======
34
mailx -s "$ORACLE_SID $TIMESTAMP Export backup logfile" [email protected] < $EXP_DIR/expdp_log_${TIMESTAMP}.log
35
echo Export completed at $TIMESTAMP
36
exit
37
38
give 755 permission
1
2
3 chmod 755 EXP_TAB_cmprss.sh
4
Now configure the script in crontab as below( pass the ORACLE_SID )
1
2
3 00 15 * * * /export/home/oracle/EXP_TAB_cmprss.sh TESTDB
4