#!/bin/bash
#oracle_rman.sh/script run by oracle
week_daily=`date +%a`
date=`date +%Y%m%d`
bak_dir=/data/rman/$date
log=${bak_dir}/$date.log
case ${week_daily} in
"Sun")  level=0;;
"Mon")  level=2;;
"Tue")  level=2;;
"Wed")  level=1;;
"Thu")  level=2;;
"Fri")  level=2;;
"Sat")  level=2;;
esac
export level=$level
if [ ! -d ${bak_dir} ];then mkdir -p ${bak_dir};fi
source ~/.bash_profile
$ORACLE_HOME/bin/rman target / msglog=$log <<EOF
run{
configure retention policy to recovery window of 7 days;
allocate channel ch1 type disk;
allocate channel ch2 type disk;
backup incremental level $level database format '${bak_dir}/db_level($level)_%s_%p_%T_%d';
sql 'alter system archive log current';
backup archivelog all delete all input format '${bak_dir}/archlog_%s_%p_%T_%d';
backup current controlfile format '${bak_dir}/controlfile_%s_%p_%T_%d';
release channel ch1;
release channel ch2;
crosscheck backup;
crosscheck archivelog all;
report obsolete;
delete noprompt obsolete;
delete noprompt expired backup;}
exit;
EOF

之后su -  oracle执行crontab -e添加计划任务

0 4 * * * /bin/bash ~/oracle_rman.sh


另外如果是在windows下写的脚本还需执行dos2unix oracle_rman.sh =_=!

以下为一些学习笔记

rman清除archivelog

crosscheck archivelog all;--->校验日志的可用性

list expired archivelog all;--->列出所有失效的归档日志

delete expired archivelog all;

delete archivelog until sequence 16;--->删除log sequence为16及16之前的所有归档日志

delete archivelog all completed before 'sysdate-7';--->删除系统时间7天以前的归档日志,不会删除闪回区有效的归档日志

delete archivelog from time 'sysdate-1';--->注意这个命令,删除系统时间1天以内到现在的归档日志

delete noprompt archivelog all completed before 'sysdate';   --->该命令清除所有的归档日志

delete noprompt archivelog all;--->同上一命令


sql>archive log list;

alter database archivelog/noarchivelog;

alter database open;



使用report obsolete命令报告过期备份

report obsolete;

使用delete obsolete命令删除过期备份

delete obsolete;