#!/bin/bash
rm -fr /db02/dailybackup/”`date +%d-%b-%Y –date ‘2 days ago’`”
rm -fr /db02/dailybackup/”`date +%d-%b-%Y –date ‘2 days ago’`”.tgz
source /home/oracle/.bash_profile # Configurando a base de dados para o usuario oracle. ORACLE_HOME, ORACLE_SID, PATH etc.
LOG=/db02/dailybackup/logs/backup_DB_`date +%d-%b-%Y`.log # Novo log diario
touch $LOG #
echo ” ” >> $LOG
date >> $LOG
echo “###########” >> $LOG
echo ” ” >> $LOG
echo “Colocando Banco de Dados em Modo Backup.” >> $LOG
cd /db02/dailybackup
# retirado antes da linha spool 06032017
# set time on;
sqlplus “/as sysdba” <<EOF
spool temp1.log;
alter system archive log current;
alter database begin backup;
select count(*) from v\$backup where status=’ACTIVE’;
exit;
spool off;
EOF
echo ” ” >> $LOG
cat /db02/dailybackup/temp1.log >> $LOG
rm -fr /db02/dailybackup/temp1.log
echo ” ” >> $LOG
echo “Aguardando Backup do Banco de Dados.” >> $LOG
echo ” ” >> $LOG
echo “Copiando arquivos de backup do dia.” >> $LOG
mkdir /db02/dailybackup/`date +%d-%b-%Y` # Criando um backup do dia no diretorio.
cp -RH /u01/app/oracle/oradata /db02/dailybackup/`date +%d-%b-%Y`
echo ” ” >> $LOG
echo “Aguardando copias de arquivos.” >> $LOG
echo ” ” >> $LOG
date >> $LOG
echo ” ” >> $LOG
echo “Finalizando backup, saindo do modo backup e gerando logs.” >> $LOG
echo ” ” >> $LOG
# set time on;
cd /db02/dailybackup
sqlplus “/as sysdba” <<EOF
spool temp2.log;
alter system archive log current;
alter database end backup;
select count(*) from v\$backup where status=’ACTIVE’;
alter system archive log current;
alter system switch logfile;
alter system switch logfile;
alter system switch logfile;
exit;
spool off;
EOF
cat /db02/dailybackup/temp2.log >> $LOG
rm -rf /db02/dailybackup/temp2.log
echo ” ” >> $LOG
echo “Saindo do modo backup.” >> $LOG
echo ” ” >> $LOG
echo “Gerando arquivos de logs.” >> $LOG
cp -RH /u01/app/oracle/fast_recovery_area /db02/dailybackup/`date +%d-%b-%Y`
echo ” ” >> $LOG
echo “Arquivo de logs gerado.” >> $LOG
echo ” ” >> $LOG
echo “Compactando arquivos” >> $LOG
tar -czpvf /db02/dailybackup/`date +%d-%b-%Y`.tgz “/db02/dailybackup/`date +%d-%b-%Y`”
rm -fr /db02/dailybackup/`date +%d-%b-%Y`
echo “Backup Completo.” >> $LOG
date >> $LOG
echo ” ” >> $LOG
echo “###############” >> $LOG
# Vamos limpar o registros de backup antigos, com mais de 10 dias
if [ `date +%d` -eq 10 ] || [ `date +%d` -eq 20 ] || [ `date +%d` -eq 30 ] ; then
echo “Limpeza de backup antigos” >> $LOG
rman target / <<EOF
crosscheck backup;
crosscheck archivelog all;
delete noprompt expired backup;
delete noprompt expired archivelog all;
report obsolete recovery window of 7 days;
delete noprompt obsolete recovery window of 7 days
delete noprompt obsolete;
backup archivelog all delete input;
EOF
fi