Total Pageviews

Tuesday, February 18, 2020

How to trigger RMAN full backup and get notified by e-mail using powershell for window oracle server


We have below script at location F:\mc_back

F:\mc_back>dir *rman* 

 Volume in drive F is New Volume
 Volume Serial Number is D624-0C00
 Directory of F:\mc_back
17-Feb-20  05:17 PM             1,410 rman_backup_disk_compressed.rman 
17-Feb-20  05:30 PM               769 ps-fullbkp_rman.ps1
               2 File(s)          2,179 bytes
               0 Dir(s)   4,798,963,712 bytes free


Script 1): rman_backup_disk_compressed.rman

sql 'alter system archive log current';
sql "alter session set nls_date_format=''dd.mm.yyyy hh24:mi:ss''";
RUN
{
configure controlfile autobackup on;
set command id to 'DB11GOnlineBackupFull';
ALLOCATE CHANNEL c1 DEVICE TYPE disk;
ALLOCATE CHANNEL c2 DEVICE TYPE disk;
ALLOCATE CHANNEL c3 DEVICE TYPE disk;
ALLOCATE CHANNEL c4 DEVICE TYPE disk;
backup AS COMPRESSED BACKUPSET full database tag DB11G_FULL format 'G:\db11gbkp\%d_%T_%s_%p_FULL';
sql 'alter system archive log current';
backup tag DB11G_ARCHIVE format 'G:\db11gbkp\%d_%T_%s_%p_ARCHIVE' archivelog all delete all input;
backup tag DB11G_CONTROL current controlfile format 'G:\db11gbkp\%d_%T_%s_%p_CONTROL';
release channel c1;
release channel c2;
release channel c3;
release channel c4;
}
  
Script 2): ps-fullbkp_rman.ps1

set-executionpolicy remotesigned
start-job { Set-ExecutionPolicy RemoteSigned -Force } -RunAs32  

# Get script input params
$ORACLE_SID="db11g"
$LOG_DIR="F:\mc_back"
$BIN_DIR="F:\mc_back"

# Assemble other variables
$datetime = get-date -format "MMM.dd.yyyy-HH.m.ss"
$RMAN_TYPE="disk_backup"
$RMAN_LOG = "$LOG_DIR\$ORACLE_SID.$RMAN_TYPE.$datetime.rman.log"
# Start logging to file and then run RMAN command. Echo is an alias for Write-Output.
$newline = "Start Execution of $RMAN_TYPE backup on $ORACLE_SID"
echo $newline | out-file $RMAN_LOG
get-date | out-file $RMAN_LOG -append
 C:\app\ashwanik\product\11.2.0.4\dbhome_1\BIN\rman target sys/oracle_4U@db11g cmdfile= "$BIN_DIR\rman_backup_disk_compressed.rman" using $ORACLE_SID | out-file $RMAN_LOG –append

# Send email. Note that cat is an alias for Get-Content.
$body = cat $RMAN_LOG | out-string
send-mailmessage -to "oracleforensics4u@gmail.com" -from "oracleforensics4u@gmail.com" -body $body -subject "RMAN log output" -smtpserver smtp-apac.service.example.com

# Remove files older than 8 days.
# Note that ls is an alias for Get-ChildItem and del is an alias for Remove-Item.
# Also, ? is an alias for Where-Object and $_ is a built-in variable
# to refer to the collection of objects passed from the previous pipeline.
ls $LOG_DIR\$ORACLE_SID.$RMAN_TYPE.* |? {$_.LastWriteTime -lt (get-date).AddDays(-8)} | del


NOTE: smtp-apac.service.example.com is a smtp server details:


We can schedule these scripts for triggering RMAN backup at stipulate time as below:

Program/Script:



%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe 




Add Arguments:

-command & F:\mc_back\ps-fullbkp_rman.ps1

Start in:

F:\mc_back

No comments:

Post a Comment