Monday, June 5, 2017

Select client IP of a specific session in Oracle Database

You are an Oracle DBA. The most important questions that you have to answer your self when monitoring your Database: "what is the IP address a session is made from". The following is the solution for you.

--# select v$session for logon_time, username (if the session is living)
SELECT s.status, logon_time, s.sid, s.serial#, s.username, s.osuser,
    p.spid as os_server_process_id, s.port os_client_port, s.machine, p.terminal,
    s.program, s.sql_id, s.prev_sql_id --, s.*
FROM v$session s, v$process p
WHERE s.paddr = p.addr
    and s.username='U1'
    and s.machine='WORKGROUP\HOST-5A81BE'
order by status;

--# If your Server OS is Linux you can stop here by using command netstat -napt. You can using the os_server_process_id return from above SQL as the input for the comment.
# netstat -napt | grep <os_server_process_id>

--# session detail (if the session is living)
select * from v$session where sid=5714 and serial# =48901;
--detail of sql statement
select * from v$sql where sql_id='cyhpma1w9s6v1'

--# select program, module, machine, port column from DBA_HIST_ACTIVE_SESS_HISTORY (if the session is terminated)
select T1.program, T1.module, T1.machine, T1.port ,T1.*
from DBA_HIST_ACTIVE_SESS_HISTORY T1
where session_serial# = 48901 and session_id = 5714
    and user_id=(select user_id from dba_users where username='U1')  ;

--select the IP from COMMENT$TEXT of AUD$ table using PORT number, userid and hostname from above sql
select * from sys.aud$
where userid='U1' and terminal like '%HOST-5A81BE%' and ntimestamp#>trunc(sysdate-1)
    and comment$text like '%PORT=49339%';



Thursday, June 1, 2017

RMAN backup command example

RMAN> CONFIGURE CHANNEL DEVICE TYPE DISK FORMAT   "/backup/rman/full_devdb_%u_%s_%p" MAXPIECESIZE 2048 M;

RMAN> BACKUP DATABASE;
RMAN> BACKUP ARCHIVELOG ALL;
RMAN> BACKUP DATABASE PLUS ARCHIVELOG;
RMAN> BACKUP AS BACKUPSET DATABASE
RMAN> BACKUP AS COMPRESSED BACKUPSET DATABASE;
RMAN> BACKUP AS BACKUPSET DATABASE PLUS ARCHIVELOG;
RMAN>  BACKUP FORMAT '/u03/TEST/%d_DB_%T_%u_s%s_p%p' DATABASE PLUS ARCHIVELOG FORMAT '/u03/TEST/%d_ARC_%T_%u_s%s_p%p';
RMAN> BACKUP FORMAT '/u03/TEST/%d_DB_%T_%u_s%s_p%p' DATABASE CURRENT CONTROLFILE FORMAT '/u03/TEST/%d_CTR_%T_%u' SPFILE FORMAT '/u03/TEST/%d_S_%T_%u' PLUS ARCHIVELOG FORMAT '/u03/TEST/%d_ARC_%T_%u_s%s_p%p';
RMAN> BACKUP AS BACKUPSET TABLESPACE PRD01;
RMAN> BACKUP AS BACKUPSET TAG 'WEEEKLY_PRD01_TBLS_BK_ONLY' TABLESPACE PRD01;
RMAN> BACKUP AS COMPRESSED BACKUPSET TAG 'WEEEKLY_PRD01_TBLS_BK_ONLY' TABLESPACE PRD01;
RMAN> BACKUP DATAFILE '/u01/app/oradata/TEST/users01.dbf';
RMAN> BACKUP DATAFILE 4;
RMAN> BACKUP INCREMENTAL LEVEL 1 DATABASE;
RMAN> BACKUP INCREMENTAL LEVEL 1 CUMULATIVE DATABASE;
RMAN> BACKUP INCREMENTAL LEVEL 0 DATABASE;
RMAN> BACKUP AS COPY DATABASE;
RMAN> BACKUP AS COPY DATAFILE 4 FORMAT '/u01/app/oracle/copy/users01.dbf';

RMAN> CONFIGURE MAXSETSIZE TO 1019M;
RMAN> CONFIGURE CHANNEL DEVICE TYPE DISK MAXPIECESIZE 50M;
RMAN> backup database filesperset 2;
RMAN> backup tablespace sysaux section size 1M;
RMAN> CONFIGURE CHANNEL DEVICE TYPE DISK MAXOPENFILES 2 rate 50M;

RMAN> BACKUP AS COPY TAG 'BASE01' INCREMENTAL LEVEL 0 DATABASE;
RMAN> BACKUP TAG 'incr_update' INCREMENTAL LEVEL 1 DATABASE;
RMAN> RECOVER COPY OF DATABASE WITH TAG 'BASE01';

RMAN> BACKUP CURRENT CONTROLFILE;
RMAN> BACKUP SPFILE;

RMAN> LIST BACKUP SUMMARY;

Backup Terminology

Whole database backup: Includes all data files and at least one control file (Remember that all control files in a database are identical.)
Partial database backup: May include zero or more tablespaces and zero or more data files; may or may not include a control file
Full backup: Makes a copy of each data block that contains data and that is within the files being backed up
Incremental backup: Makes a copy of all data blocks that have changed since a previous backup. The Oracle database supports two levels of incremental backup (0 and 1). A level 1 incremental backup can be one of two types: cumulative or differential. A cumulative backup backs up all changes since the last level 0 backup. A differential backup backs up all changes since the last incremental backup (which could be either a level 0 or level 1 backup). Change Tracking with RMAN supports incremental backups.