1. 定时任务制定
$ crontab -l
#delete archivelog before 7 days
00 02 * * * sh /home/oracle/tkdba/del_arch_everyday.sh
2. 脚本内容
脚本1:将包含$(date +%F)写到脚本中
$ cat /home/oracle/tkdba/del_arch_everyday.sh
sh /home/oracle/tkdba/del_arch.sh >> /home/oracle/tkdba/logs/del_arch.log.$(date +%F) 2>&1
脚本2:清理7天前的归档日志
$ cat /home/oracle/tkdba/del_arch.sh
!/bin/bash
. /home/oracle/.bash_profile
function getTime(){
CT="date +'%F %T'
"
}
maintenance database archivelog
START=date +%s
getTime
echo "${CT} [INFO] Start maintenance database: ${ORACLE_SID} ..."
echo '=================================================='
rman target / <<EOF
run{
crosscheck backup;
crosscheck archivelog all;
delete noprompt expired archivelog all;
delete noprompt archivelog all completed before 'sysdate-6';
delete noprompt obsolete;
}
quit;
EOF
echo '=================================================='
DELTA=$(($(date +%s) - ${START}))
getTime
echo "${CT} [INFO] Maintenance database ${ORACLE_SID} done, duration: $((${DELTA}/3600))hour $((${DELTA}%3600/60))minute $((${DELTA}%60))second. "