Oracle Live

07/08/2013

RMAN Incremental

Filed under: Scripts — mogukiller @ 10:15 am
Tags:

Script muy completo para realizar los backups por rman, tanto full como incrementales. Pudiendo parametrizar el día de la semana en el que realizar el backup full.

#!/bin/bash
#set -x
#------------------------------------------------------------------------------------
# Script: SCRIPT_Rman_Incremental.sh
# Autor:  lmds
# Fecha:  2013/07/25
# Tested: Oracle 11.2.0.3
#
# Parametros
# 	-d DATABASE		Base de datos. NONE by default
# 	-p PATH			Path donde dejaré el rman. NONE by default.
#	-full			Independientemente del dia de la semana ejecuta un rman level 0
#
# Informacion:
#		- La variable DIAS_FULL_BACKUP contiene los dias de la semana  en los que se hara
#		  un rman FULL de la base de datos.
#		
#-----------------------------------------------------------------------------------

fec=`date +%Y%m%d_%H%M`
fich_log=backup_${fec}.log
fich_tmp=backup_tmp.log

PARALELISMO=2
FULL_BACKUP="FALSE"
DIAS_FULL_BACKUP="3,6" #Miercoles y Domingo 

# -------------------------------------------------------
# Funciones
# -------------------------------------------------------

Revisar_Error () 
{
	egrep "ORA-|ERROR|error|Error|RMAN-" ${fich_tmp} > /dev/null
	[ "$?" -ne 0 ] && return 0
	echo -e "\n" >>$fich_log
	echo -e "  -------------------------------------------">>$fich_log
	echo -e "  ERROR `date +%Y/%m/%d" "%T`">>$fich_log
	echo -e "  -------------------------------------------">>$fich_log
	echo -e "\n" >>$fich_log
	cat ${fich_tmp} >>$fich_log
	echo -e "\n" >>$fich_log
}

Limpia_Directorios ()
{
	# Limpia directorios vacios
	if [ -d $PATH_RMAN ]; then
        find $PATH_RMAN -type d -name '*_*_*' -maxdepth 1 2>/dev/null| while read dir
        do
            hayBkp=$(find $dir -name '*.bck'|wc -l)
            if [ $hayBkp -eq 0 ]; then
				# Check if is not a diferent directory than current
                [ $(basename $PATH_RMAN) != $(basename $dir) ] && \rm -rf $dir
            fi
        done
    fi
}

#---------------------
# main
#---------------------

# Comprobamos los parametros

DATABASE="NONE"
PATH_RMAN="NONE"

until [ -z "$1" ] 
do
	case "$1" in
		'-d')
			shift
			DATABASE=$1
		;;
		'-p')
			shift
			PATH_RMAN=$1
		;;
		'-full')
			FULL_BACKUP="TRUE"
	esac
	shift
done

if [ "$DATABASE" = "NONE" ]; then
	if [ -n "$ORACLE_SID" ]; then
		DATABASE=$ORACLE_SID
	else
		if [ `ps -fea | grep -v grep | grep pmon| wc -l` -ne 1 ]; then
			DATABASE=`ps -fea | grep -v grep | grep pmon| awk '{print $8}' | cut -d_ -f3`
		else
			echo "ERROR: DATABASE Unknown" | tee -a $fich_log
			exit 1
		fi
	fi
else	
	if [ `ps -fea | grep -v grep | grep pmon_${DATABASE} | wc -l` -ne 1 ]; then
		echo "ERROR: DATABASE Unknow" | tee -a $fich_log
		exit 1
	fi
fi

export ORACLE_SID=$DATABASE

echo -e "\n" | tee -a $fich_log
echo -e "  -------------------------------------------"| tee -a $fich_log
echo -e "  RMAN $DATABASE  `date +%Y/%m/%d" "%T`"| tee -a $fich_log
echo -e "  -------------------------------------------"| tee -a $fich_log

# Comprobamos conexion con la DB
sqlplus -s / as sysdba <<[EOF] >/dev/null
	SELECT SYSDATE FROM DUAL;
[EOF]
[ "$?" -ne 0 ] && (echo "ERROR $DATABASE is not accesible" | tee -a $fich_log; exit 1)

echo " [`date +%T`]  Connected to $DATABASE .. [OK]" | tee -a $fich_log

if [ "$PATH_RMAN" = "NONE" ]; then
#Comprobamos si esta configurada la recovery_area
sqlplus -s / as sysdba <<[EOF] >$fich_tmp
	SET HEADING OFF
	SELECT 'export PATH_RMAN='||VALUE||'/backup' FROM V\$PARAMETER WHERE NAME LIKE 'db_recovery_file_dest';
[EOF]

. ./$fich_tmp

else
	[ ! -d "$PATH_RMAN" ] && ("ERROR: PATH $PATH_RMAN doesn't exits" | tee -a $fich_log; exit 1)
	PATH_RMAN="${PATH_RMAN}"
fi

[ ! -d "$PATH_RMAN" ] && mkdir -p $PATH_RMAN

echo " [`date +%T`]  Directory $PATH_RMAN created .. [OK]" | tee -a $fich_log

# -------------------------------------------------------
# Limpiamos de backups antiguos
# -------------------------------------------------------

echo " [`date +%T`]  Limpiamos Backups y archiverlog antiguos ..." | tee -a $fich_log
rman nocatalog <<[EOF] 2>&1 | tee -i fich_tmp
connect target /
run{
	DELETE NOPROMPT FORCE OBSOLETE;
	CROSSCHECK BACKUP;
	DELETE NOPROMPT FORCE EXPIRED BACKUP;
	CROSSCHECK ARCHIVELOG ALL;
	DELETE NOPROMPT FORCE EXPIRED ARCHIVELOG ALL;
}
exit
[EOF]

Revisar_Error; ([ $? -eq 0 ] &&  echo " [`date +%T`]  Limpiamos Backups y archiverlog antiguos [OK]" || echo " [`date +%T`]  Limpiamos Backups y archiverlog antiguos [NOK]") | tee -a $fich_log

Limpia_Directorios

# Configuramos los canales

>ch_allocate.tmp
>ch_release.tmp
for id_channel in $(seq ${PARALELISMO}); do
	echo "ALLOCATE CHANNEL ch${id_channel} DEVICE TYPE DISK MAXPIECESIZE 1G;" >>ch_allocate.tmp
	echo "RELEASE CHANNEL ch${id_channel};" >>ch_release.tmp
done

# -------------------------------------------------------
# BACKUP archivelog antes del backup de la base de datos
# -------------------------------------------------------

echo " [`date +%T`]  Backup archivelog PRE ..." | tee -a $fich_log
dir_rman="rman_arch_pre_${fec}"
[ ! -d "${PATH_RMAN}/${dir_rman}" ] && mkdir -p "${PATH_RMAN}/${dir_rman}"
rman nocatalog <<[EOF] 2>&1 | tee -i $fich_tmp
connect target /
run{
	@ch_allocate.tmp
	BACKUP AS COMPRESSED BACKUPSET ARCHIVELOG UNTIL TIME 'SYSDATE-1/24' FORMAT '${PATH_RMAN}/${dir_rman}/arch_backup_%U.bck' TAG 'ARCH_PRE_${fec}' DELETE INPUT;
	@ch_release.tmp
}
exit
[EOF]
 
Revisar_Error; ([ $? -eq 0 ] &&  echo " [`date +%T`]  Backup archivelog PRE [OK]" || echo " [`date +%T`]  Backup archivelog PRE [NOK]") | tee -a $fich_log

# -------------------------------------------------------
# BACKUP base de datos
# -------------------------------------------------------

# Indicamos que tipo de backup se hace

tipo_incremental=1
tipo="INC"

dia_semana=`date +%w`
echo $DIAS_FULL_BACKUP | grep $dia_semana
[ $? -eq 0 ] && tipo_incremental=0

if [ "$FULL_BACKUP" = "TRUE" ] || [ "$tipo_incremental" -eq 0 ]; then
	tipo_incremental=0
	tipo="FULL"
fi

echo " [`date +%T`]  Backup database level $tipo_incremental ..." | tee -a $fich_log
dir_rman="rman_${tipo}_${fec}"
[ ! -d "${PATH_RMAN}/${dir_rman}" ] && mkdir -p "${PATH_RMAN}/${dir_rman}"

rman nocatalog <<[EOF] 2>&1 | tee -i $fich_tmp
connect target /
run{
	@ch_allocate.tmp
	BACKUP AS COMPRESSED BACKUPSET INCREMENTAL LEVEL $tipo_incremental DATABASE FORMAT '${PATH_RMAN}/${dir_rman}/${tipo}_backup_%U.bck' TAG 'DB_${tipo}_${fec}';
	COPY CURRENT CONTROLFILE FOR STANDBY TO '${PATH_RMAN}/${dir_rman}/sby_control01.ctl' TAG 'CF_STB';
	COPY CURRENT CONTROLFILE TO '${PATH_RMAN}/${dir_rman}/pri_control01.ctl' TAG 'CF_PRI';
	SQL 'ALTER SYSTEM ARCHIVE LOG CURRENT';
	BACKUP AS COMPRESSED BACKUPSET ARCHIVELOG ALL FORMAT '${PATH_RMAN}/${dir_rman}/arc_backup_%U.bck' TAG 'ARCH_POST_${fec}';
	@ch_release.tmp
}
exit
[EOF]

Revisar_Error; ([ $? -eq 0 ] &&  echo " [`date +%T`]  Backup database [OK]" || echo " [`date +%T`]  Backup database [NOK]") | tee -a $fich_log

# -------------------------------------------------------
# Miramos estado de los RMAN
# -------------------------------------------------------

echo " [`date +%T`]  Estado de los backup ..." | tee -a $fich_log
echo -e "\n"
export NLS_DATE_FORMAT='YYYY/MM/DD HH24:MI:SS'
rman nocatalog <<[EOF] 2>&1 | tee -a $fich_log
connect target /
	LIST BACKUP BY FILE TAG 'ARCH_PRE_${fec}';
	LIST BACKUP BY FILE TAG 'DB_${tipo}_${fec}';
	LIST BACKUP BY FILE TAG 'ARCH_POST_${fec}';
exit
[EOF]

echo -e "\n"
echo " [`date +%T`]  FIN del Backup" | tee -a $fich_log

Dejar un comentario »

Aún no hay comentarios.

RSS feed for comments on this post. TrackBack URI

Responder

Introduce tus datos o haz clic en un icono para iniciar sesión:

Logo de WordPress.com

Estás comentando usando tu cuenta de WordPress.com. Cerrar sesión / Cambiar )

Imagen de Twitter

Estás comentando usando tu cuenta de Twitter. Cerrar sesión / Cambiar )

Foto de Facebook

Estás comentando usando tu cuenta de Facebook. Cerrar sesión / Cambiar )

Google+ photo

Estás comentando usando tu cuenta de Google+. Cerrar sesión / Cambiar )

Conectando a %s

Blog de WordPress.com.

A %d blogueros les gusta esto: