How to create and use alias command in linux for DBA related task

An alias is a short cut command for long command. We may type short alias name to run a longer command or sql script file.


$ alias shortName="custom command here"


Aliases can be creates as temporary or permanent. A temporary alias will use for existing session only and permanent alias will work any time for quick execution of command.


Temporary alias:-

$ alias  space='df -h'

$alias lg='sqlplus apps/apps'

To run the command not need to type log command just type lg to login in oracle database using apps credential.

[appltest@apps scripts]$ lg

SQL*Plus: Release 10.1.0.5.0 - Production on Fri Sep 25 19:32:36 2020

Copyright (c) 1982, 2005, Oracle.  All rights reserved.

Connected to:

Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production

With the Partitioning, OLAP, Data Mining and Real Application Testing options

SQL> show user

USER is "APPS"

Permanent alias:-

To keep permanent alias we need to add below in .bash_profile.

$vi .bash_profile

. /oracle/apps/apps_st/appl/APPS*.env

alias stopall='$ADMIN_SCRIPTS_HOME/adstpall.sh apps/***** ;sleep 20;pkill -fu applmgr'

alias startall='$ADMIN_SCRIPTS_HOME/adstrtal.sh apps/*******'

:wg!



In case you need to shutdown the ebs . Here no need to execute environment variable , login and run adstrtall.sh. You just type stopall and startall as below.


$stopall   ---> To shutdown EBS services.
$startall  ---> To start EBS services.


How to transfer file between local and remote host using scp

SCP stand for secure copy protocol.Its network protocol based on ssh (secure shell) that allow to transfer file/directory from remote  local,local to remote host and remote to remote host. 


Additionally you can use scp command in batch file or shell script to automate the file transfer to full fill your daily,weekly,monthly and yearly task to eliminate the manual process. 


#Copy file from remote host to local host.

scp username@remote_host:/remote/file.txt /local/file.txt


#Copy directory from remote host to local host.

scp username@remote_host:/remote/directory /local/directory


#Copy file from local host to remote host.

scp /local/file.txt username@remote_host:/remote/file.txt


#Copy directory from local host to remote host.

scp /local/directory username@remote_host:/remote/directory


#Copy file from remote host to remote host.

scp username1@remote_host1:/remote/file.txt username2@remote_host2:/remote/file.txt


#Copy directory from remote host to remote host.

scp username1@remote_host1:/remote/directory username2@remote_host2:/remote/directory


While doing the manual copy using scp, after press enter it will prompt for password.




To automate the file transfer on linux,unix and solaris server just prepare the shell script file(filetransfer.sh) as below given example. After that schedule it via cronjob.

Before going to schedule shell script ( filetransfer.sh), need to be generate a public and private key pair between both host for password less login(without prompting password).


Shell Script:-

#!/bin/ksh

cd /data01/scripts/csvfile

CURR_DT=`date  --date="yesterday"  +%d-%m-%Y`

CSV_FILE1=leave_$CURR_DT.csv

Scriptfile=/data01/scripts/tadacsvfilefromftp.sh

export Scriptfile


scp root@192.168.*.*:/home/eemployee/data/$CSV_FILE1 /data01/scripts/csvfile/$CSV_FILE1

cd /data01/scripts/csvfile

#Check CSV file exist on server.

if [[  -f $CSV_FILE1 ]]; then

echo "LEAVE File Rceiving Process was Successful  and '$CSV_FILE1' exist"

else

echo "Leave File Receiving Process Failed '$CSV_FILE1' Does not exist at `hostname`. Please contact System Administrator! $Scriptfile" | mailx -s "CSV File Receiving Failed from FTP Server" abc@**.com,support@***.com

fi


Compress full, incremental backup using rman

Basically Backup compression is the feature of Oracle Database which offer more benefit. We can take the oracle rman backup as normal or in compress mode. Restoration process of compressed backup will same as uncompressed backup.


Full compress RMAN Backup:- 

RUN

{

CROSSCHECK ARCHIVELOG ALL;

sql 'alter system archive log current';

BACKUP AS compressed backupset filesperset 8 DATABASE FORMAT '$destnation/DB_%d_%p_%T_%s.rbkp' TAG DAILY_HOT_BACKUP;

sql 'alter system archive log current';

BACKUP AS compressed backupset ARCHIVELOG ALL NOT BACKED UP 1 TIMES FORMAT '$destnation/ARCBAK_%d_%p_%T_%s.rbkp' TAG ARCHIVE_BKP;

BACKUP AS compressed backupset CURRENT CONTROLFILE FORMAT '$destnation/CNT_%d_%p_%T_%s.rbkp' TAG CONTROL_FILE;

}


INCREMENTAL LEVEL 0 BACKUP:-

RUN {

BACKUP AS COMPRESSED BACKUPSET INCREMENTAL LEVEL 0
DEVICE TYPE DISK
TAG = 'DAILY_INC_FULL' FORMAT '/venusBackup/RMAN_HOTBACKUP/PROD_DB/%d_DB_%u_%s_%p' database;

#*** DAILY CONTROLFILE BACKUP

BACKUP AS BACKUPSET DEVICE TYPE DISK TAG = 'DAILY_CONTROLFILE' FORMAT '/venusBackup/RMAN_HOTBACKUP/PROD_DB/%d_CTL_%u_%s_%p'
CURRENT CONTROLFILE;

#***** DAILY SPFILE BACKUP

BACKUP AS BACKUPSET DEVICE TYPE DISK TAG = 'DAILY SPFILE'
FORMAT '/venusBackup/RMAN_HOTBACKUP/PROD_DB/%d_SPFILE_%u_%s_%p'
SPFILE;

#*******ARCHIVELOG BACKUP****

sql 'alter system archive log current';
BACKUP AS COMPRESSED BACKUPSET INCREMENTAL LEVEL 0 DEVICE TYPE DISK TAG = 'DAILY_INCR_CUM_ARCHIVEDLOG'
FORMAT '/venusBackup/RMAN_HOTBACKUP/PROD_DB/%d_ARC_%u_%s_%p'
ARCHIVELOG ALL;

#*****Remove backuped archivelog before 6 day's******#
DELETE ARCHIVELOG UNTIL TIME 'sysdate-6' BACKED UP 1 TIMES TO DEVICE TYPE DISK;
}


INCREMENTAL LEVEL 1 BACKUP:-

RUN {

BACKUP AS COMPRESSED BACKUPSET INCREMENTAL LEVEL 1 DEVICE TYPE DISK TAG = 'DAILY_INC_L1_BACKUP' FORMAT '/venusBackup/RMAN_HOTBACKUP/PROD_DB/%d_DB_%u_%s_%p' database;

#*** DAILY CONTROLFILE BACKUP 

BACKUP AS BACKUPSET DEVICE TYPE DISK TAG = 'DAILY_CONTROLFILE' FORMAT '/venusBackup/RMAN_HOTBACKUP/PROD_DB/%d_CTL_%u_%s_%p' CURRENT CONTROLFILE;

#***** DAILY SPFILE BACKUP

BACKUP AS BACKUPSET DEVICE TYPE DISK TAG = 'DAILY SPFILE' FORMAT '/venusBackup/RMAN_HOTBACKUP/PROD_DB/%d_SPFILE_%u_%s_%p' SPFILE;

#*******ARCHIVELOG BACKUP****

sql 'alter system archive log current';

BACKUP AS COMPRESSED BACKUPSET INCREMENTAL LEVEL 1 

DEVICE TYPE DISK TAG = 'DAILY_INCR_L1_ARCHIVEDLOG' FORMAT '/venusBackup/RMAN_HOTBACKUP/PROD_DB/%d_ARC_%u_%s_%p' ARCHIVELOG ALL;

#*****Remove backuped archivelog before 6 day's******#

DELETE ARCHIVELOG UNTIL TIME 'sysdate-6' BACKED UP 1 TIMES TO DEVICE TYPE DISK;

}

Recompile invalid objects in oracle

Sometimes Oracle objects package,procedure,function,trigger and views become invalid when dependent object has changed. 

Check invalid objects in oracle database.

SQL>select owner,object_name,status,object_type from dba_objects where status='INVALID';

SQL>select object_name,status from dba_objects where object_name like 'JA%' and status ='INVALID';


There are several methods for recompiling invalid schema objects.

Manual approach:-Manual approach suitable if you have small number of invalid schema objects. 

SQL>ALTER PACKAGE package_name COMPILE;

SQL>ALTER PACKAGE package_name COMPILE BODY;

SQL>ALTER PROCEDURE procedure_name COMPILE;

SQL>ALTER FUNCTION function_name COMPILE;

SQL>ALTER TRIGGER trigger_name COMPILE;

SQL>ALTER VIEW view_name COMPILE;


DBMS_DDL:-Another approach is DBMS_DDL package to recompile the invalid object.


EXEC DBMS_DDL.alter_compile('PACKAGE', 'SCHEMA_NAME', 'PACKAGE_NAME');

EXEC DBMS_DDL.alter_compile('PACKAGE BODY', 'SCHEMA_NAME', 'PACKAGE_NAME');

EXEC DBMS_DDL.alter_compile('PROCEDURE', 'SCHEMA_NAME', 'PROCEDURE_NAME');

EXEC DBMS_DDL.alter_compile('FUNCTION', 'SCHEMA_NAME', 'FUNCTION_NAME');

EXEC DBMS_DDL.alter_compile('TRIGGER', 'SCHEMA_NAME', 'TRIGGER_NAME');


UTL_RECOMP :- UTL_RECOMP is useful after a major version upgrade that typically invalidates all PL/SQL objects.

-- Schema level.
SQL>EXEC UTL_RECOMP.recomp_serial('APPS');
SQL>EXEC UTL_RECOMP.recomp_parallel(4, 'APPS');

-- Database level.
SQL>EXEC UTL_RECOMP.recomp_serial();
SQL>EXEC UTL_RECOMP.recomp_parallel(4);

-- Using job_queue_processes value.
SQL>EXEC UTL_RECOMP.recomp_parallel();
SQL>EXEC UTL_RECOMP.recomp_parallel(NULL, 'APPS');


DBMS_UTILITY.compile_schema:-The COMPILE_SCHEMA procedure in the DBMS_UTILITY package compiles all procedures, functions, packages, and triggers in the specified schema.


SQL>EXEC DBMS_UTILITY.compile_schema(schema => 'SCOTT', compile_all => false);


UTLRP.SQL:-As we know that oracle recommended utlrp.sql script to recompile all invalid objects.

To run the utlrp.sql script start the Sql*Plus as below.

[oracle@**** ~]$ sqlplus / as sysdba

SQL*Plus: Release 11.2.0.4.0 Production on Thu Sep 3 13:36:40 2020

Copyright (c) 1982, 2013, Oracle.  All rights reserved.

Connected to:

Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production

With the Partitioning, OLAP, Data Mining and Real Application Testing options

SQL> @$ORACLE_HOME/rdbms/admin/utlrp.sql

TIMESTAMP

--------------------------------------------------------------------------------

COMP_TIMESTAMP UTLRP_BGN  2020-09-03 13:36:51

DOC>   The following PL/SQL block invokes UTL_RECOMP to recompile invalid

DOC>   objects in the database. Recompilation time is proportional to the

DOC>   number of invalid objects in the database, so this command may take

DOC>   a long time to execute on a database with a large number of invalid

DOC>   objects.

DOC>

DOC>   Use the following queries to track recompilation progress:

DOC>

DOC>   1. Query returning the number of invalid objects remaining. This

DOC>      number should decrease with time.

DOC>         SELECT COUNT(*) FROM obj$ WHERE status IN (4, 5, 6);

DOC>

DOC>   2. Query returning the number of objects compiled so far. This number

DOC>      should increase with time.

DOC>         SELECT COUNT(*) FROM UTL_RECOMP_COMPILED;

DOC>

DOC>   This script automatically chooses serial or parallel recompilation

DOC>   based on the number of CPUs available (parameter cpu_count) multiplied

DOC>   by the number of threads per CPU (parameter parallel_threads_per_cpu).

DOC>   On RAC, this number is added across all RAC nodes.

DOC>

DOC>   UTL_RECOMP uses DBMS_SCHEDULER to create jobs for parallel

DOC>   recompilation. Jobs are created without instance affinity so that they

DOC>   can migrate across RAC nodes. Use the following queries to verify

DOC>   whether UTL_RECOMP jobs are being created and run correctly:

DOC>

DOC>   1. Query showing jobs created by UTL_RECOMP

DOC>         SELECT job_name FROM dba_scheduler_jobs

DOC>            WHERE job_name like 'UTL_RECOMP_SLAVE_%';

DOC>

DOC>   2. Query showing UTL_RECOMP jobs that are running

DOC>         SELECT job_name FROM dba_scheduler_running_jobs

DOC>            WHERE job_name like 'UTL_RECOMP_SLAVE_%';

DOC>#

PL/SQL procedure successfully completed.

TIMESTAMP

--------------------------------------------------------------------------------

COMP_TIMESTAMP UTLRP_END  2020-09-03 13:37:13

DOC> The following query reports the number of objects that have compiled

DOC> with errors.

DOC>

DOC> If the number is higher than expected, please examine the error

DOC> messages reported with each object (using SHOW ERRORS) to see if they

DOC> point to system misconfiguration or resource constraints that must be

DOC> fixed before attempting to recompile these objects.

DOC>#

OBJECTS WITH ERRORS

-------------------

                  0

DOC> The following query reports the number of errors caught during

DOC> recompilation. If this number is non-zero, please query the error

DOC> messages in the table UTL_RECOMP_ERRORS to see if any of these errors

DOC> are due to misconfiguration or resource constraints that must be

DOC> fixed before objects can compile successfully.

DOC>#

ERRORS DURING RECOMPILATION

---------------------------

                          0

Function created.

PL/SQL procedure successfully completed.

Function dropped.

...Database user "SYS", database schema "APEX_040200", user# "371" 13:37:20

...Compiled 0 out of 2994 objects considered, 0 failed compilation 13:37:21

...263 packages

...255 package bodies

...452 tables

...11 functions

...16 procedures

...3 sequences

...457 triggers

...1320 indexes

...207 views

...0 libraries

...6 types

...0 type bodies

...0 operators

...0 index types

...Begin key object existence check 13:37:21

...Completed key object existence check 13:37:21

...Setting DBMS Registry 13:37:21

...Setting DBMS Registry Complete 13:37:21

...Exiting validate 13:37:21

PL/SQL procedure successfully completed.

SQL>


Once you run the utlrp.sql script oracle automatically recompile all invalid objects.


Shell script to File transfer file on remote system using ftp

File Transfer Protocol (FTP) is a standard network protocol used to transfer files to and from a remote network.

To transfer file using ftp,we require ftp server, user account and ftp client .

Below is the procedure to 'copy file using file transfer protocol (FTP) shell scrip' and we will see 'How to Use Linux FTP Command to Transfer Files'. In this shell script(filetoftp.sh) we will receive notification on mail if file transfer(ftp) fail due to any reason.

Manually to get and put the file on one server to another server(remote server):-

ftp -n 192.168.11.10
ftp>quote USER employee
ftp>quote PASS ******
230 OK. Current restricted directory is /
ftp> cd /data01
ftp> lcd /backup01    --> change local directory on local server.
ftp> mget ***.csv     -->to get the file from ftp server to local server.
ftp> mput ****.csv   -->to transfer file from local to ftp server.


Automatically transfer the file on remote server using shell script:- 
/data01/crontab_scripts/tada_schedule/filetoftp.sh

#!/bin/sh
# Created by Sajid Quamer
#CSV file to be transfer from db server to ftp server
cd /data05/TADA_Master_curr
CURR_DT=`date +%d-%m-%Y`
export CURR_DT
CSV_FILE1=TADA_CITY_MASTER_$CURR_DT.csv
CSV_FILE2=TADA_POLICY_MASTER_$CURR_DT.csv
CSV_FILE3=TADA_EMPLOYEES_MASTER_$CURR_DT.csv
CSV_FILE4=Leave_pending_$CURR_DT.csv
CSV_FILE5=CONTRACT_DATA_$CURR_DT.csv
CSV_FILE6=BILL_DATA_$CURR_DT.csv
Scriptfile=/data01/crontab_scripts/tada_schedule/tadatoftp.sh
export CSV_FILE1
export CSV_FILE2
export CSV_FILE3
export CSV_FILE4
export CSV_FILE5
export CSV_FILE6
export Scriptfile

ftp -n << 'EOF'
open 192.168.11.10
quote USER *******
quote PASS *******
cd /employees
prompt no
lcd /data05/TADA_Master_curr
mput TADA_CITY_MASTER*
mput TADA_POLICY_MASTER*
mput TADA_EMPLOYEES_MASTER*
mput Leave_pending_*
bye
EOF
echo "done"


#check if a file exists or not
if [[  -f $CSV_FILE1 &&   -f $CSV_FILE2  &&  -f $CSV_FILE3 && -f $CSV_FILE4 ]]; then
echo "File Transfer Process was Successful  and '$CSV_FILE1' exist"
else
echo "FTP File Transfer Process Failed '$CSV_FILE1' Does not exist at `hostname`. Please contact System Adminis
trator! $Scriptfile" | mailx -s "File Transfer Failed" abc@gmail.com, acb@gmail.com
fi

#check if a file exists or not
if [[  -f $CSV_FILE1 &&   -f $CSV_FILE2  &&  -f $CSV_FILE3 && -f $CSV_FILE4 ]]; then
echo "File Transfer Process was Successful  and '$CSV_FILE2' exist"
else
echo "FTP File Transfer Process Failed '$CSV_FILE2' Does not exist at `hostname`. Please contact System Adminis
trator! $Scriptfile" | mailx -s " File Transfer Failed" abc@gmail.com, acb@gmail.com
fi

if [[  -f $CSV_FILE1 &&   -f $CSV_FILE2  &&  -f $CSV_FILE3 && -f $CSV_FILE4 ]]; then
echo "File Transfer Process was Successful  and '$CSV_FILE3' exist"
else
echo "FTP File Transfer Process Failed '$CSV_FILE3' Does not exist at `hostname`. Please contact System Adminis
trator! $Scriptfile" | mailx -s " File Transfer Failed" abc@gmail.com, acb@gmail.com
fi

if [[  -f $CSV_FILE1 &&   -f $CSV_FILE2  &&  -f $CSV_FILE3 && -f $CSV_FILE4 ]]; then
echo "File Transfer Process was Successful  and '$CSV_FILE4' exist"
else
echo "FTP File Transfer Process Failed '$CSV_FILE4' Does not exist at `hostname`. Please contact System Adminis
trator! $Scriptfile" | mailx -s "File Transfer Failed" abc@gmail.com, acb@gmail.com
fi


cd /data05/TADA_Master_curr
rm -rf $CSV_FILE1
rm -rf $CSV_FILE2
rm -rf $CSV_FILE3
rm -rf $CSV_FILE4