RMAN正确地删除Archivelog以及设置有备库的归档删除策略

时间:2021-05-11 10:34:24

原文链接:http://blog.sina.com.cn/s/blog_64e166580100xks5.html

如何正确地删除Archivelog:

Archivelog并不能直接得从OS层直接物理删除,因为archivelog的相关信息是记录在controlfile中的,当物理删除后不会改变controlfile的设置。并且在查询相关的动态视图(例如v$archived_log)时,该部分日志仍然标注为存在。也就是说Oracle并不认为这些日志被删除了,所以在删除archivelog的时候,需要我们在其他地方做一些设置。 





一、使用RMAN清楚物理删除后的记录 

可以使用RMAN来删除archivelog,具体可以按以下步骤操作: 

1、物理删除archivelog 

2、进入RMAN 

3、crosscheck archivelog all; 

4、delete expired archivelog all; 

这样就在一些Oracle的记录中查不到相应的archivelog记录了。 





二、直接使用RMAN删除archivelog 

其实在RMAN中是可以使用命令直接将ARCHIVELOG删除的,命令如下: 

RMAN> DELETE ARCHIVELOG ALL COMPLETED BEFORE 'SYSDATE-7'; 

表示删除7天以前的所有archivelog,但是这个命令的缺点是无法对archivelog进行细致的划分。 





三、使用RMAN备份achivelog后删除 

这是一种最为稳妥的方法了,使用RMAN备份archivelog,备份后全部删除 

backup format '/u01/arch_%T_%s_%U' archivelog all delete input; 

或者 

backup database plus archivelog;





注:我在库上设置了Streams后,使用RMAN删除archivelog会导致报错RMAN-08137: WARNING: 

archive log not deleted as it is still needed. 不知道该如何解决,据说是10.2的一个bug,如果真

是bug的话,那只能在OS层设置删除,或用RMAN进行强制删除了。

10g之后的版本,Oracle在RMAN中内置增加了一项归档文件的删除策略,终于,DBA可以仅通过一项配置就

解决该问题了。该策略对应两个值:

APPLIED ON STANDBY:设置为该值时,当通过附加的DELETE INPUT子句删除Standby数据库仍需要的日志

时,会提示RMAN-08137错误。不过用户仍然可以手动地通过DELETE ARCHIVELOG方式删除。

NONE:设置为该值时,则不启用归档文件的删除策略。默认情况下就是NONE。

例如,启用APPLIED ON STANDBY:

RMAN> CONFIGURE ARCHIVELOG DELETION POLICY TO APPLIED ON STANDBY; 

new RMAN configuration parameters: 

CONFIGURE ARCHIVELOG DELETION POLICY TO APPLIED ON STANDBY; 

new RMAN configuration parameters are successfully stored 

RMAN> run

{

backup archivelog all delete input format 'ZHSVR044_%d_set=%s_piece=%p_date=%T.ARC.rmn';

}





都知道在controlfile中记录着每一个archivelog的相关信息,当然们在OS下把这些物理文件delete掉后,在我们的

controlfile中仍然记录着这些archivelog的信息,在oracle的OEM管理器中有可视化的日志展现出,当我们手工清除archive目录下的文件后,这些记录并没有被我们从controlfile中清除掉,也就是oracle并不知道这些文件已经不存在了!这时候我们要做手工的清除的话,下面我经过实验,可以尝试这种方法:

1. 进入rman  

2. connect target /

3. crosscheck archivelog all;

4. delete expired archivelog all;

这时候我们再去OEM中看就一定看不到,如果你的从来没有做过这个动作的话,我们可以比较从这个动作前的controlfile后动作后的controlfile的大小!





ORACLE正确删除归档并回收空间的方法

ORACLE正确删除归档并回收空间的方法

一个ORACLE归档日志经常满,表现为/oraarchive 这个文件空间占用100%大家一定抱怨ORACLE为何没有归档维护工具,很多人直接删除了事,错了,ORACLE有,而且很智能,可以正确的删除归档和FLASHBACK,不过切记,ORACLE归档日志对于ORACLE的数据恢复和备份非常重要,不到万不得已不要删除归档日志。

删除归档日志的过程

以ORACLE用户身份登录到数据库服务器主机或通过网络连接

进入ORACLE数据备份工具

rman target/

或rman target/@orcl

在命令窗口里面执行

DELETE ARCHIVELOG ALL COMPLETED BEFORE 'SYSDATE-7';

说明

SYSDATA-7,表明当前的系统时间7天前,before关键字表示在7天前的归档日志,如果使用了闪回功能,也会删除闪回的数据。

同样道理,也可以删除从7天前到现在的全部日志,不过这个命令要考虑清楚,做完这个删除,最好马上进行全备份数据库

DELETE ARCHIVELOG FROM TIME 'SYSDATE-7';





UNIX/LINUX下也可以通过FIND找到7天前的归档数据,使用EXEC子操作删除





find /oraarchive -xdev -mtime +7 -name "*.dbf" -exec rm -f {} ;

这样做仍然会在RMAN里留下未管理的归档文件

仍需要在RMAN里执行下面2条命令

crosscheck archivelog all;

delete expired archivelog all;

所以还不如上面的方法好用,不过用FIND的好处就是,可以在条件上,和EXEC子项上做很多操作,实现更复杂的功能

3.简要介绍一下report obsolete命令

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

RMAN> report obsolete;

RMAN retention policy will be applied to the command

RMAN retention policy is set to redundancy 1

Report of obsolete backups and copies

Type                 Key    Completion Time    Filename/Handle

-------------------- ------ ------------------ --------------------

Backup Set           125    01-NOV-04

Backup Piece       125    01-NOV-04          /data1/oracle/orabak/full_1_541045804

Backup Set           131    04-NOV-04

Backup Piece       131    04-NOV-04          /data1/oracle/orabak/full_AVATAR2_20041104_131

....

Backup Set           173    06-DEC-04

Backup Piece       173    06-DEC-04          /data1/oracle/orabak/full_AVATAR2_20041206_173

Backup Set           179    11-DEC-04

Backup Piece       179    11-DEC-04          /data1/oracle/orabak/arch544588206.arc

.....

Backup Piece       189    17-DEC-04          /data1/oracle/orabak/arch545106606.arc

Backup Set           190    17-DEC-04

Backup Piece       190    17-DEC-04          /data1/oracle/orabak/arch545106665.arc

Backup Set           191    20-DEC-04

Backup Piece       191    20-DEC-04          /data1/oracle/orabak/arch_AVATAR2_20041220_194

Archive Log          2973   20-DEC-04          /opt/oracle/oradata/avatar2/archive/1_2985.dbf

Archive Log          2971   20-DEC-04          /opt/oracle/oradata/avatar2/archive/1_2984.dbf

.....

Archive Log          2705   17-DEC-04          /opt/oracle/oradata/avatar2/archive/1_2717.dbf

Archive Log          2704   17-DEC-04          /opt/oracle/oradata/avatar2/archive/1_2716.dbf

Archive Log          2703   17-DEC-04          /opt/oracle/oradata/avatar2/archive/1_2715.dbf

Archive Log          2702   17-DEC-04          /opt/oracle/oradata/avatar2/archive/1_2714.dbf

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

RMAN> delete obsolete;

RMAN retention policy will be applied to the command

RMAN retention policy is set to redundancy 1

using channel ORA_DISK_1

Deleting the following obsolete backups and copies:

Type                 Key    Completion Time    Filename/Handle

-------------------- ------ ------------------ --------------------

Backup Set           125    01-NOV-04

Backup Piece       125    01-NOV-04          /data1/oracle/orabak/full_1_541045804

....

Archive Log          2704   17-DEC-04          /opt/oracle/oradata/avatar2/archive/1_2716.dbf

Archive Log          2703   17-DEC-04          /opt/oracle/oradata/avatar2/archive/1_2715.dbf

Archive Log          2702   17-DEC-04          /opt/oracle/oradata/avatar2/archive/1_2714.dbf

Do you really want to delete the above objects (enter YES or NO)? yes

deleted backup piece

backup piece handle=/data1/oracle/orabak/full_AVATAR2_20041206_173 recid=173 stamp=544156241

.....

deleted archive log

archive log filename=/opt/oracle/oradata/avatar2/archive/1_2715.dbf recid=2703 stamp=545108268

deleted archive log

archive log filename=/opt/oracle/oradata/avatar2/archive/1_2714.dbf recid=2702 stamp=545107659

Deleted 286 objects





RMAN> crosscheck archivelog all;

released channel: ORA_DISK_1

allocated channel: ORA_DISK_1

channel ORA_DISK_1: sid=19 devtype=DISK

specification does not match any archive log in the recovery catalog





5 在rman中用crosscheck检查归档日志,2个归档日志都是失败的:

RMAN> crosscheck archivelog all;

释放的通道: ORA_DISK_1

分配的通道: ORA_DISK_1

通道 ORA_DISK_1: sid=14 devtype=DISK

对归档日志的验证失败

存档日志文件名 =D:ORACLEORADATATESTARCHIVE1_47.DBF 记录 ID=1 时间戳 =572866

683

对归档日志的验证失败

存档日志文件名 =D:ORACLEORADATATESTARCHIVE11_48.DBF 记录 ID=2 时间戳 =57286

6931

已交叉检验的 2 对象

6 试着同步一下,看行不行,结果不行,crosscheck还是失败:

RMAN> resync catalog;

正在启动全部恢复目录的 resync

完成全部 resync

RMAN> crosscheck archivelog all;

释放的通道: ORA_DISK_1

分配的通道: ORA_DISK_1

通道 ORA_DISK_1: sid=14 devtype=DISK

对归档日志的验证失败

存档日志文件名 =D:ORACLEORADATATESTARCHIVE1_47.DBF 记录 ID=1 时间戳 =572866

683

对归档日志的验证失败

存档日志文件名 =D:ORACLEORADATATESTARCHIVE11_48.DBF 记录 ID=2 时间戳 =57286

6931

已交叉检验的 2 对象

7 用list expired看看是否有失效的archive log,证明没有失效的archive log:

RMAN> list expired archivelog all;

说明与恢复目录中的任何存档日志均不匹配

8 更改语言环境试试,结果再次crosscheck,2个archive log 都成功了:

RMAN> exit





恢复管理器完成。

C:>set nls_lang=american_america.zhs16gbk

C:>rman catalogrman/rman@safetarget /

Recovery Manager: Release 9.2.0.1.0 - Production

Copyright (c) 1995, 2002, Oracle Corporation. All rights reserved.

connected to target database: TEST (DBID=1870953724)

connected to recovery catalog database

RMAN> crosscheck archivelog all;

allocated channel: ORA_DISK_1

channel ORA_DISK_1: sid=9 devtype=DISK

validation succeeded for archived log

archive log filename=D:ORACLEORADATATESTARCHIVE1_47.DBF recid=1 stamp=57286

6683

validation succeeded for archived log

archive log filename=D:ORACLEORADATATESTARCHIVE11_48.DBF recid=2 stamp=5728

66931

Crosschecked 2 objects





删除N天以前的归档日志,有两个命令:

delete archivelog until time 'sysdate-N'

delete archivelog all completed before 'sysdate-N'

 

看下两者的区别:

RMAN> list archivelog until time 'sysdate-1';

List of Archived Log Copies

Key     Thrd Seq     S Low Time   Name

------- ---- ------- - ---------- ----

7       1    5       A 13-7月 -11 +RECOVERY/edu/archivelog/2011_07_13/thread_1_seq_5.259.756405771

10      1    6       A 13-7月 -11 +RECOVERY/edu/archivelog/2011_07_14/thread_1_seq_6.263.756453607

16      1    7       A 14-7月 -11 +RECOVERY/edu/archivelog/2011_07_14/thread_1_seq_7.268.756467343

17      1    8       A 14-7月 -11 +RECOVERY/edu/archivelog/2011_07_14/thread_1_seq_8.269.756467345

18      1    9       A 14-7月 -11 +RECOVERY/edu/archivelog/2011_07_14/thread_1_seq_9.270.756467399

19      1    10      A 14-7月 -11 +RECOVERY/edu/archivelog/2011_07_14/thread_1_seq_10.271.756467403

21      1    11      A 14-7月 -11 +RECOVERY/edu/archivelog/2011_07_14/thread_1_seq_11.272.756467511

22      1    12      A 14-7月 -11 +RECOVERY/edu/archivelog/2011_07_14/thread_1_seq_12.274.756467515

23      1    13      A 14-7月 -11 +RECOVERY/edu/archivelog/2011_07_14/thread_1_seq_13.275.756467521

25      1    14      A 14-7月 -11 +RECOVERY/edu/archivelog/2011_07_14/thread_1_seq_14.276.756467527

26      1    15      A 14-7月 -11 +RECOVERY/edu/archivelog/2011_07_14/thread_1_seq_15.278.756467635

27      1    16      A 14-7月 -11 +RECOVERY/edu/archivelog/2011_07_14/thread_1_seq_16.279.756484801

29      1    17      A 14-7月 -11 +RECOVERY/edu/archivelog/2011_07_15/thread_1_seq_17.256.756576859

5       2    2       A 13-7月 -11 +RECOVERY/edu/archivelog/2011_07_13/thread_2_seq_2.257.756405175

8       2    3       A 13-7月 -11 +RECOVERY/edu/archivelog/2011_07_14/thread_2_seq_3.260.756442807

9       2    4       A 14-7月 -11 +RECOVERY/edu/archivelog/2011_07_14/thread_2_seq_4.261.756442823

11      2    5       A 14-7月 -11 +RECOVERY/edu/archivelog/2011_07_14/thread_2_seq_5.262.756453607

12      2    6       A 14-7月 -11 +RECOVERY/edu/archivelog/2011_07_14/thread_2_seq_6.264.756453623

13      2    7       A 14-7月 -11 +RECOVERY/edu/archivelog/2011_07_14/thread_2_seq_7.265.756467273

14      2    8       A 14-7月 -11 +RECOVERY/edu/archivelog/2011_07_14/thread_2_seq_8.266.756467279

15      2    9       A 14-7月 -11 +RECOVERY/edu/archivelog/2011_07_14/thread_2_seq_9.267.756467279

20      2    10      A 14-7月 -11 +RECOVERY/edu/archivelog/2011_07_14/thread_2_seq_10.273.756467511

24      2    11      A 14-7月 -11 +RECOVERY/edu/archivelog/2011_07_14/thread_2_seq_11.277.756467533

28      2    12      A 14-7月 -11 +RECOVERY/edu/archivelog/2011_07_15/thread_2_seq_12.280.756566433

最大sequence为17和12

再看:

RMAN> list archivelog all completed before 'sysdate-1';





List of Archived Log Copies

Key     Thrd Seq     S Low Time   Name

------- ---- ------- - ---------- ----

7       1    5       A 13-7月 -11 +RECOVERY/edu/archivelog/2011_07_13/thread_1_seq_5.259.756405771

10      1    6       A 13-7月 -11 +RECOVERY/edu/archivelog/2011_07_14/thread_1_seq_6.263.756453607

16      1    7       A 14-7月 -11 +RECOVERY/edu/archivelog/2011_07_14/thread_1_seq_7.268.756467343

17      1    8       A 14-7月 -11 +RECOVERY/edu/archivelog/2011_07_14/thread_1_seq_8.269.756467345

18      1    9       A 14-7月 -11 +RECOVERY/edu/archivelog/2011_07_14/thread_1_seq_9.270.756467399

19      1    10      A 14-7月 -11 +RECOVERY/edu/archivelog/2011_07_14/thread_1_seq_10.271.756467403

21      1    11      A 14-7月 -11 +RECOVERY/edu/archivelog/2011_07_14/thread_1_seq_11.272.756467511

22      1    12      A 14-7月 -11 +RECOVERY/edu/archivelog/2011_07_14/thread_1_seq_12.274.756467515

23      1    13      A 14-7月 -11 +RECOVERY/edu/archivelog/2011_07_14/thread_1_seq_13.275.756467521

25      1    14      A 14-7月 -11 +RECOVERY/edu/archivelog/2011_07_14/thread_1_seq_14.276.756467527

26      1    15      A 14-7月 -11 +RECOVERY/edu/archivelog/2011_07_14/thread_1_seq_15.278.756467635

27      1    16      A 14-7月 -11 +RECOVERY/edu/archivelog/2011_07_14/thread_1_seq_16.279.756484801

5       2    2       A 13-7月 -11 +RECOVERY/edu/archivelog/2011_07_13/thread_2_seq_2.257.756405175

8       2    3       A 13-7月 -11 +RECOVERY/edu/archivelog/2011_07_14/thread_2_seq_3.260.756442807

9       2    4       A 14-7月 -11 +RECOVERY/edu/archivelog/2011_07_14/thread_2_seq_4.261.756442823

11      2    5       A 14-7月 -11 +RECOVERY/edu/archivelog/2011_07_14/thread_2_seq_5.262.756453607

12      2    6       A 14-7月 -11 +RECOVERY/edu/archivelog/2011_07_14/thread_2_seq_6.264.756453623

13      2    7       A 14-7月 -11 +RECOVERY/edu/archivelog/2011_07_14/thread_2_seq_7.265.756467273

14      2    8       A 14-7月 -11 +RECOVERY/edu/archivelog/2011_07_14/thread_2_seq_8.266.756467279

15      2    9       A 14-7月 -11 +RECOVERY/edu/archivelog/2011_07_14/thread_2_seq_9.267.756467279

20      2    10      A 14-7月 -11 +RECOVERY/edu/archivelog/2011_07_14/thread_2_seq_10.273.756467511

24      2    11      A 14-7月 -11 +RECOVERY/edu/archivelog/2011_07_14/thread_2_seq_11.277.756467533

 

详细看下 这些日志的区别:

QL> select THREAD#,sequence#,FIRST_TIME,NEXT_TIME,COMPLETION_TIME  

  2  from v$archived_log order by thread#,sequence#;

   THREAD#  SEQUENCE# FIRST_TIME        NEXT_TIME         COMPLETION_TIME

---------- ---------- ----------------- ----------------- -----------------

         1          1 20110713 14:57:58 20110713 14:58:42 20110713 14:58:49

         1          2 20110713 14:58:42 20110713 14:59:00 20110713 14:59:18

         1          3 20110713 14:59:00 20110713 15:02:31 20110713 15:02:36

         1          4 20110713 15:02:31 20110713 16:32:50 20110713 16:32:57

         1          5 20110713 16:32:50 20110713 16:41:40 20110713 16:42:51

         1          6 20110713 16:41:40 20110714 06:00:07 20110714 06:00:08

         1          7 20110714 06:00:07 20110714 09:26:57 20110714 09:49:04

         1          8 20110714 09:26:57 20110714 09:46:55 20110714 09:49:04

         1          9 20110714 09:46:55 20110714 09:49:58 20110714 09:50:06

         1         10 20110714 09:49:58 20110714 09:50:02 20110714 09:50:09

         1         11 20110714 09:50:02 20110714 09:51:49 20110714 09:51:59

   THREAD#  SEQUENCE# FIRST_TIME        NEXT_TIME         COMPLETION_TIME

---------- ---------- ----------------- ----------------- -----------------

         1         12 20110714 09:51:49 20110714 09:51:55 20110714 09:52:05

         1         13 20110714 09:51:55 20110714 09:52:01 20110714 09:52:10

         1         14 20110714 09:52:01 20110714 09:52:07 20110714 09:52:13

         1         15 20110714 09:52:07 20110714 09:53:54 20110714 09:53:59

         1         16 20110714 09:53:54 20110714 14:40:01 20110714 14:40:05

         1         17 20110714 14:40:01 20110715 16:14:19 20110715 16:14:20

         1         18 20110715 16:14:19 20110715 16:15:46 20110715 16:15:47

         2          1 20110713 15:14:43 20110713 16:15:41 20110713 16:17:14

         2          2 20110713 16:15:41 20110713 16:32:50 20110713 16:32:54

         2          3 20110713 16:32:50 20110714 03:00:07 20110714 03:00:12

         2          4 20110714 03:00:07 20110714 03:00:23 20110714 03:00:27

   THREAD#  SEQUENCE# FIRST_TIME        NEXT_TIME         COMPLETION_TIME

---------- ---------- ----------------- ----------------- -----------------

         2          5 20110714 03:00:23 20110714 06:00:06 20110714 06:00:11

         2          6 20110714 06:00:06 20110714 06:00:22 20110714 06:00:27

         2          7 20110714 06:00:22 20110714 06:00:37 20110714 09:47:56

         2          8 20110714 06:00:37 20110714 09:26:56 20110714 09:48:05

         2          9 20110714 09:26:56 20110714 09:47:56 20110714 09:48:05

         2         10 20110714 09:47:56 20110714 09:51:51 20110714 09:51:52

         2         11 20110714 09:51:51 20110714 09:52:12 20110714 09:52:12

         2         12 20110714 09:52:12 20110715 13:20:33 20110715 13:20:37

区别已经很明显:

 list archivelog until time 'sysdate-1'; 此时是按照归档日志开始时间即fisrt_time作为截止时间

 list archivelog all completed before 'sysdate-1' 是按照归档日志完成时间即completion_time作为截止时间

试想如果删除归档日志,还是采用delete archivelog all completed before 'sysdate-N'