業(yè)務(wù)增長(zhǎng)發(fā)展迅猛网持,歸檔日志增長(zhǎng)太快尼斧,磁盤承受不了,所以準(zhǔn)備做個(gè)定時(shí)清理歸檔日志的定時(shí)任務(wù)
ORACLE 定時(shí)清理歸檔日志詳細(xì)過(guò)程
1撩穿、RM方式清理歸檔日志
清理歸檔日志磷支,可以人為的刪除已經(jīng)歸檔好的日志文件,然后再調(diào)用rman里面RMAN> crosscheck archivelog all;來(lái)去數(shù)據(jù)庫(kù)里面刪除歸檔記錄食寡;也可以通過(guò)ram調(diào)用delete命令來(lái)刪除歸檔日志雾狈。
2、使用rman清理歸檔日志
RMAN清除方式會(huì)自動(dòng)清除磁盤上的歸檔日志文件冻河,同時(shí)會(huì)釋放控制文件中對(duì)應(yīng)的歸檔日志的歸檔信息箍邮。
可以基于不同的條件來(lái)清除歸檔日志茉帅,如基于SCN叨叙,基于SEQUENCE,基于TIME等方式堪澎。
對(duì)于上述的三種方式又可以配合from, until, between .. and .. 等等子句來(lái)限定范圍擂错,方式靈活多變。
下面的命令用于校驗(yàn)歸檔日志的有效性樱蛤,列出無(wú)效的歸檔日志钮呀,以及以何種方式清除歸檔日志,列出幾種常用的:
delete noprompt archivelog until time "to_date('XXXX-XX-XX','YYYY-MM-DD')"; ---> 清理到某天日期之前的歸檔
delete noprompt archivelog until time "to_date('2016-09-14 18:00:00','YYYY-MM-DD hh24:mi:ss')"; ---> 清理到具體時(shí)分秒之前的歸檔日志
cross check archivelog all; --->校驗(yàn)日志的可用性
list expired archivelog all; --->列出所有失效的歸檔日志
delete archivelog until sequence 16; --->刪除log sequence為16及16之前的所有歸檔日志
delete archivelog all completed before 'sysdate-7'; --->刪除系統(tǒng)時(shí)間7天以前的歸檔日志昨凡,不會(huì)刪除閃回區(qū)有效的歸檔日志
delete archivelog all completed before 'sysdate - 1'; --->同上爽醋,1天以前的
delete archivelog from time 'sysdate-1'; --->注意這個(gè)命令,刪除系統(tǒng)時(shí)間1天以內(nèi)到現(xiàn)在的歸檔日志
delete noprompt archivelog all completed before 'sysdate'; --->該命令清除當(dāng)前所有的歸檔日志
delete noprompt archivelog all completed before 'sysdate-0'; --->該命令清除當(dāng)前所有的歸檔日志
delete noprompt archivelog all; --->同上一命令
3便脊、實(shí)現(xiàn)清理一個(gè)小時(shí)前的歸檔日志
執(zhí)行命令:delete force archivelog all completed before 'sysdate-1/24';
(1)查看當(dāng)前時(shí)間
[oracle@hch_test_121_90 ~]$ date
Tue Oct 11 11:20:20 CST 2016
[oracle@hch_test_121_90 ~]$
(2)查看當(dāng)前一個(gè)小時(shí)之前的歸檔日志蚂四,看到有一個(gè)o1_mf_1_2822_czrm1dj8_.arc,如下所示:
[oracle@hch_test_121_90 ~]$ ll /oracle/app/oracle/flash_recovery_area/POWERDES/archivelog/2016_10_11/ |more
total 3181880
-rw-r----- 1 oracle oinstall 41340416 Oct 11 10:19 o1_mf_1_2822_czrm1dj8_.arc
-rw-r----- 1 oracle oinstall 40434176 Oct 11 10:20 o1_mf_1_2823_czrm49l9_.arc
-rw-r----- 1 oracle oinstall 41180160 Oct 11 10:22 o1_mf_1_2824_czrm76nd_.arc
-rw-r----- 1 oracle oinstall 40340480 Oct 11 10:23 o1_mf_1_2825_czrm9xos_.arc
(3)開(kāi)始執(zhí)行刪除命令哪痰,按計(jì)劃刪除這一條歸檔日志
RMAN> delete force archivelog all completed before 'sysdate-1/24';
using target database control file instead of recovery catalog
allocated channel: ORA_DISK_1
channel ORA_DISK_1: SID=11 device type=DISK
List of Archived Log Copies for database with db_unique_name POWERDES
=====================================================================
Key Thrd Seq S Low Time
------- ---- ------- - ---------
1396 1 2822 A 11-OCT-16
Name: /oracle/app/oracle/flash_recovery_area/POWERDES/archivelog/2016_10_11/o1_mf_1_2822_czrm1dj8_.arc
Do you really want to delete the above objects (enter YES or NO)? YES # 輸入刪除命令
deleted archived log
archived log file name=/oracle/app/oracle/flash_recovery_area/POWERDES/archivelog/2016_10_11/o1_mf_1_2822_czrm1dj8_.arc RECID=1396 STAMP=924949164
Deleted 1 objects
RMAN>
4遂赠、自動(dòng)化腳本定時(shí)任務(wù)實(shí)現(xiàn)清理一個(gè)小時(shí)前的歸檔日志
(1)自動(dòng)化腳本
[root@hch_test_121_90 ~]# more archivelog_clear.sh
#!/bin/sh
BACK_DIR=/oracle/clear_archlog/data
export DATE=`date +%F`
echo " " >> $BACK_DIR/$DATE/rman_backup.log
echo `date '+%Y-%m-%d %H:%M:%S'` >> $BACK_DIR/$DATE/rman_backup.log
su - oracle -c "
mkdir -p $BACK_DIR/$DATE
rman log=$BACK_DIR/$DATE/rman_backup.log target / <<EOF
# delete force archivelog all completed before 'sysdate-1/24'; # 這里執(zhí)行清除歸檔日志,如果不想手動(dòng)輸入YES晌杰,則可以添加noprompt參數(shù)
delete force noprompt archivelog all completed before 'sysdate-1/24';
exit;
EOF
"
echo " " >> $BACK_DIR/$DATE/rman_backup.log
(2)crontab定時(shí)任務(wù)
[root@hch_test_121_90 ~]# crontab -l
10 */1 * * * sh /home/oracle/archivelog_clear.sh
(3)查看執(zhí)行后的日志
[root@hch_test_121_90 ~]# more /oracle/clear_archlog/data/2016-10-11/rman_backup.log
Recovery Manager: Release 11.2.0.1.0 - Production on Tue Oct 11 11:28:04 2016
Copyright (c) 1982, 2009, Oracle and/or its affiliates. All rights reserved.
connected to target database: POWERDES (DBID=3458668465)
RMAN>
using target database control file instead of recovery catalog
allocated channel: ORA_DISK_1
channel ORA_DISK_1: SID=137 device type=DISK
List of Archived Log Copies for database with db_unique_name POWERDES
=====================================================================
Key Thrd Seq S Low Time
------- ---- ------- - ---------
1397 1 2823 A 11-OCT-16
Name: /oracle/app/oracle/flash_recovery_area/POWERDES/archivelog/2016_10_11/o1_mf_1_2823_czrm49l9_.arc
1398 1 2824 A 11-OCT-16
Name: /oracle/app/oracle/flash_recovery_area/POWERDES/archivelog/2016_10_11/o1_mf_1_2824_czrm76nd_.arc
1399 1 2825 A 11-OCT-16
Name: /oracle/app/oracle/flash_recovery_area/POWERDES/archivelog/2016_10_11/o1_mf_1_2825_czrm9xos_.arc
1400 1 2826 A 11-OCT-16
Name: /oracle/app/oracle/flash_recovery_area/POWERDES/archivelog/2016_10_11/o1_mf_1_2826_czrmdqqy_.arc
1401 1 2827 A 11-OCT-16
Name: /oracle/app/oracle/flash_recovery_area/POWERDES/archivelog/2016_10_11/o1_mf_1_2827_czrmhksn_.arc
Do you really want to delete the above objects (enter YES or NO)? "exit;" is an invalid response - please re-enter.
Do you really want to delete the above objects (enter YES or NO)?
Error occurred getting response - assuming NO response
RMAN>
Recovery Manager complete.