我们在备份Oracle数据库的时候习惯性的在备份之前,会执行下ALTER SYSTEM SWITCH LOGFILE或者ALTER SYSTEM ARCHIVE LOG CURRENT,那么这两个命令到底是做什么的,Oracle11.2.0.4版本中什么情况下需要执行,什么情况下不需要执行呢?
一:首先说下ALTER SYSTEM SWITCH LOGFILE和ALTER SYSTEM ARCHIVE LOG CURRENT
rman不会备份online redo log和standby redo log ,所以这就是为啥在做Oracle备份的时候,需要执行alter system archive log current; 或者alter system switch logfile这类的操作,这样把当前在redo log文件中的最新redo及时进行归档,
ALTER SYSTEM SWITCH LOGFILE 对单实例数据库或RAC中的当前实例执行日志切换,立刻返回结果,除非切换的时候现在所有的redo logfile group都没有归档完成,此时会等待下一个redo logfile group归档完成后,才可以切换成current的redo logfile;
ALTER SYSTEM ARCHIVE LOG CURRENT 会对数据库中的所有实例执行日志切换,并且归档下当前current状态的redo logfile日志,会等待当前current状态的redo logfile日志归档完毕才会返回结果;
由此可见:当你想备份到完整的归档日志的时候,数据库应该执行ALTER SYSTEM ARCHIVE LOG CURRENT操作,或者应该执行多次ALTER SYSTEM SWITCH LOGFILE(有几组online redo group就执行几次切换),才能保证归档日志中包含完整的备份数据文件期间的所有redo!
官方文档关于这两个命令的解释:
ALTER SYSTEM SWITCH LOGFILE ;
SWITCH LOGFILE Clause
The SWITCH LOGFILE clause lets you explicitly force Oracle to begin writing to a new redo log file group, regardless of whether the files in the current redo log file group are full. When you force a log switch, Oracle begins to perform a checkpoint but returns control to you immediately rather than when the checkpoint is complete. To use this clause, your instance must have the database open.
ALTER SYSTEM ARCHIVE LOG CURRENT ;
CURRENT Clause
Specify CURRENT to manually archive the current redo log file group of the specified thread, forcing a log switch. If you omit the THREAD parameter, then Oracle archives all redo log file groups from all enabled threads, including logs previous to current logs. You can specify CURRENT only when the database is open.
二、Oracle 11g备份归档的方式:
1、备份归档日志方式:
1)单独备份归档日志:backup archivelog all
2)在执行备库时一起备份归档日志:backup database plus archivelog;
2、这两种方式有什么区别呢?
1)运行backup archivelog all 命令时执行的步骤:
1.alter system archive log current; 归档当前日志
2.backup archivelog all ; 备份所有归档日志
2)而运行backup database plus archivelog,的运行步骤是:
1.alter system archive log current; 归档当前日志
2.backup archivelog all; 备份所有归档日志
3.backup database; 备份数据库
4.alter system archive log current; 归档当前日志
5.backup archivelog recently generated ; 备份刚生成的归档日志
具体验证如下:
1)首先 backup archivelog all,你在主库备份的时候,可以看到如下的关键字current log archived,表明在备份归档的时候,会自动执行alter system archive log current;
RMAN> backup archivelog all format '/home/backup/liuwenhe/archivelog%U.bak';
Starting backup at 02-JUL-22
current log archived ----此处表明备份开始的时候执行了alter system archive log current;
using channel ORA_DISK_1
channel ORA_DISK_1: starting archived log backup set
channel ORA_DISK_1: specifying archived log(s) in backup set
input archived log thread=1 sequence=866 RECID=1722 STAMP=1108998018
input archived log thread=1 sequence=867 RECID=1724 STAMP=1109027795
input archived log thread=1 sequence=868 RECID=1725 STAMP=1109028305
input archived log thread=1 sequence=869 RECID=1727 STAMP=1109028364
input archived log thread=1 sequence=870 RECID=1729 STAMP=1109028396
input archived log thread=1 sequence=871 RECID=1731 STAMP=1109028591
input archived log thread=1 sequence=872 RECID=1733 STAMP=1109029397
channel ORA_DISK_1: starting piece 1 at 02-JUL-22
channel ORA_DISK_1: finished piece 1 at 02-JUL-22
piece handle=/home/backup/liuwenhe/archivelogq611ksgl_1_1.bak tag=TAG20220702T234317 comment=NONE
channel ORA_DISK_1: backup set complete, elapsed time: 00:00:01
Finished backup at 02-JUL-22
2)当你backup archivelog all,在dg库备份的时候,发现报错如下,说明当你在dg库尝试备份的时候会尝试去主库执行alter system archive log current;
RMAN> backup archivelog all format '/home/backup/liuwenhe/archivelog%U.bak';
Starting backup at 02-JUL-22
RMAN-06820: WARNING: failed to archive current log at primary database ----在dg库执行备份归
档的操作,会尝试去主库执行alter system archive log current;--------------
ORACLE error from target database:
ORA-17629: Cannot connect to the remote database server
ORA-17627: ORA-00942: table or view does not exist
using channel ORA_DISK_1
channel ORA_DISK_1: starting archived log backup set
channel ORA_DISK_1: specifying archived log(s) in backup set
input archived log thread=1 sequence=857 RECID=850 STAMP=1108688404
input archived log thread=1 sequence=858 RECID=851 STAMP=1108739104
input archived log thread=1 sequence=859 RECID=852 STAMP=1108740245
input archived log thread=1 sequence=860 RECID=853 STAMP=1108740951
input archived log thread=1 sequence=861 RECID=854 STAMP=1108861301
input archived log thread=1 sequence=862 RECID=855 STAMP=1108861301
input archived log thread=1 sequence=863 RECID=856 STAMP=1108915207
input archived log thread=1 sequence=864 RECID=857 STAMP=1108947602
input archived log thread=1 sequence=865 RECID=858 STAMP=1108947603
input archived log thread=1 sequence=866 RECID=859 STAMP=1108998019
input archived log thread=1 sequence=867 RECID=860 STAMP=1109027795
input archived log thread=1 sequence=868 RECID=861 STAMP=1109028305
input archived log thread=1 sequence=869 RECID=862 STAMP=1109028364
input archived log thread=1 sequence=870 RECID=863 STAMP=1109028397
input archived log thread=1 sequence=871 RECID=864 STAMP=1109028591
input archived log thread=1 sequence=872 RECID=865 STAMP=1109029397
channel ORA_DISK_1: starting piece 1 at 02-JUL-22
channel ORA_DISK_1: finished piece 1 at 02-JUL-22
piece handle=/home/backup/liuwenhe/archivelog4k11kskp_1_1.bak tag=TAG20220702T234529 comment=NONE
channel ORA_DISK_1: backup set complete, elapsed time: 00:00:07
Finished backup at 02-JUL-22
3)在主库执行backup database的时候加上plus archivelog关键字,如下所示
BACKUP AS COMPRESSED BACKUPSET DATABASE FORMAT "/home/backup/orcldg/backup/rman/data/db%U" plus archivelog;
RMAN> BACKUP AS COMPRESSED BACKUPSET DATABASE FORMAT "/home/backup/liuwenhe/_db_%U" plus archivelog;
Starting backup at 02-JUL-22
current log archived------- 此处表明备份开始的时候执行了alter system archive log current;
using channel ORA_DISK_1
channel ORA_DISK_1: starting compressed archived log backup set --开始备份归档日志
channel ORA_DISK_1: specifying archived log(s) in backup set
input archived log thread=1 sequence=866 RECID=1722 STAMP=1108998018
input archived log thread=1 sequence=867 RECID=1724 STAMP=1109027795
input archived log thread=1 sequence=868 RECID=1725 STAMP=1109028305
input archived log thread=1 sequence=869 RECID=1727 STAMP=1109028364
input archived log thread=1 sequence=870 RECID=1729 STAMP=1109028396
channel ORA_DISK_1: starting piece 1 at 02-JUL-22
channel ORA_DISK_1: finished piece 1 at 02-JUL-22
piece handle=/u01/app/oracle/fast_recovery_area/ORCL/backupset/2022_07_02/o1_mf_annnn_TAG20220702T232636_kd0rxdyb_.bkp tag=TAG20220702T232636 comment=NONE
channel ORA_DISK_1: backup set complete, elapsed time: 00:00:07
Finished backup at 02-JUL-22
Starting backup at 02-JUL-22
using channel ORA_DISK_1
channel ORA_DISK_1: starting compressed full datafile backup set ---开始备份数据文件
channel ORA_DISK_1: specifying datafile(s) in backup set
input datafile file number=00007 name=/home/oradata/orcl/otc01.dbf
input datafile file number=00006 name=/home/oradata/orcl/htsl.dbf
input datafile file number=00002 name=/home/oradata/orcl/sysaux01.dbf
input datafile file number=00001 name=/home/oradata/orcl/system01.dbf
input datafile file number=00005 name=/home/oradata/orcl/example01.dbf
input datafile file number=00003 name=/home/oradata/orcl/undotbs01.dbf
input datafile file number=00004 name=/home/oradata/orcl/users01.dbf
channel ORA_DISK_1: starting piece 1 at 02-JUL-22
channel ORA_DISK_1: finished piece 1 at 02-JUL-22
piece handle=/home/backup/liuwenhe/_db_q011krhk_1_1 tag=TAG20220702T232644 comment=NONE
channel ORA_DISK_1: backup set complete, elapsed time: 00:03:05
channel ORA_DISK_1: starting compressed full datafile backup set
channel ORA_DISK_1: specifying datafile(s) in backup set
including current control file in backup set
including current SPFILE in backup set
channel ORA_DISK_1: starting piece 1 at 02-JUL-22
channel ORA_DISK_1: finished piece 1 at 02-JUL-22
piece handle=/home/backup/liuwenhe/_db_q111krnd_1_1 tag=TAG20220702T232644 comment=NONE
channel ORA_DISK_1: backup set complete, elapsed time: 00:00:01
Finished backup at 02-JUL-22
Starting backup at 02-JUL-22
current log archived ----此处表明在备份完数据文件后,又执行了一次alter system archive log current
using channel ORA_DISK_1
channel ORA_DISK_1: starting compressed archived log backup set---备份最新的归档
channel ORA_DISK_1: specifying archived log(s) in backup set
input archived log thread=1 sequence=871 RECID=1731 STAMP=1109028591
channel ORA_DISK_1: starting piece 1 at 02-JUL-22
channel ORA_DISK_1: finished piece 1 at 02-JUL-22
piece handle=/u01/app/oracle/fast_recovery_area/ORCL/backupset/2022_07_02/o1_mf_annnn_TAG20220702T232951_kd0s3h9m_.bkp tag=TAG20220702T232951 comment=NONE
channel ORA_DISK_1: backup set complete, elapsed time: 00:00:01
Finished backup at 02-JUL-22
4)在主库不加plus archivelog参数备份数据库,具体如下所示,发现没有执行alter system archive log current;相关操作!
RMAN> BACKUP AS COMPRESSED BACKUPSET DATABASE FORMAT "/home/backup/liuwenhe/_db_%U";
Starting backup at 02-JUL-22
using channel ORA_DISK_1
channel ORA_DISK_1: starting compressed full datafile backup set
channel ORA_DISK_1: specifying datafile(s) in backup set
input datafile file number=00007 name=/home/oradata/orcl/otc01.dbf
input datafile file number=00006 name=/home/oradata/orcl/htsl.dbf
input datafile file number=00002 name=/home/oradata/orcl/sysaux01.dbf
input datafile file number=00001 name=/home/oradata/orcl/system01.dbf
input datafile file number=00005 name=/home/oradata/orcl/example01.dbf
input datafile file number=00003 name=/home/oradata/orcl/undotbs01.dbf
input datafile file number=00004 name=/home/oradata/orcl/users01.dbf
channel ORA_DISK_1: starting piece 1 at 02-JUL-22
channel ORA_DISK_1: finished piece 1 at 02-JUL-22
piece handle=/home/backup/liuwenhe/_db_q411ks59_1_1 tag=TAG20220702T233713 comment=NONE
channel ORA_DISK_1: backup set complete, elapsed time: 00:03:05
channel ORA_DISK_1: starting compressed full datafile backup set
channel ORA_DISK_1: specifying datafile(s) in backup set
including current control file in backup set
including current SPFILE in backup set
channel ORA_DISK_1: starting piece 1 at 02-JUL-22
channel ORA_DISK_1: finished piece 1 at 02-JUL-22
piece handle=/home/backup/liuwenhe/_db_q511ksb2_1_1 tag=TAG20220702T233713 comment=NONE
channel ORA_DISK_1: backup set complete, elapsed time: 00:00:01
Finished backup at 02-JUL-22
5)同样在dg库执行plus archivelog方式的备份时,也会自动尝试去主库执行alter system archive log current操作:
BACKUP AS COMPRESSED BACKUPSET DATABASE FORMAT "/home/backup/orcldg/backup/rman/data/db%U" plus archivelog;
RMAN> BACKUP AS COMPRESSED BACKUPSET DATABASE FORMAT "/home/backup/orcldg/backup/rman/data/db%U" plus archivelog;
Starting backup at 02-JUL-22
RMAN-06820: WARNING: failed to archive current log at primary database ---尝试去主库执行alter system archive log current
ORACLE error from target database:
ORA-17629: Cannot connect to the remote database server
ORA-17627: ORA-00942: table or view does not exist
using channel ORA_DISK_1
channel ORA_DISK_1: starting compressed archived log backup set
channel ORA_DISK_1: specifying archived log(s) in backup set
input archived log thread=1 sequence=857 RECID=850 STAMP=1108688404
input archived log thread=1 sequence=858 RECID=851 STAMP=1108739104
input archived log thread=1 sequence=859 RECID=852 STAMP=1108740245
input archived log thread=1 sequence=860 RECID=853 STAMP=1108740951
input archived log thread=1 sequence=861 RECID=854 STAMP=1108861301
input archived log thread=1 sequence=862 RECID=855 STAMP=1108861301
input archived log thread=1 sequence=863 RECID=856 STAMP=1108915207
input archived log thread=1 sequence=864 RECID=857 STAMP=1108947602
input archived log thread=1 sequence=865 RECID=858 STAMP=1108947603
input archived log thread=1 sequence=866 RECID=859 STAMP=1108998019
input archived log thread=1 sequence=867 RECID=860 STAMP=1109027795
input archived log thread=1 sequence=868 RECID=861 STAMP=1109028305
input archived log thread=1 sequence=869 RECID=862 STAMP=1109028364
input archived log thread=1 sequence=870 RECID=863 STAMP=1109028397
input archived log thread=1 sequence=871 RECID=864 STAMP=1109028591
input archived log thread=1 sequence=872 RECID=865 STAMP=1109029397
channel ORA_DISK_1: starting piece 1 at 02-JUL-22
channel ORA_DISK_1: finished piece 1 at 02-JUL-22
piece handle=/u01/app/oracle/fast_recovery_area/ORCLDG/backupset/2022_07_02/o1_mf_annnn_TAG20220702T235004_kd0t9d87_.bkp tag=TAG20220702T235004 comment=NONE
channel ORA_DISK_1: backup set complete, elapsed time: 00:00:15
Finished backup at 02-JUL-22
Starting backup at 02-JUL-22
using channel ORA_DISK_1
channel ORA_DISK_1: starting compressed full datafile backup set
channel ORA_DISK_1: specifying datafile(s) in backup set
input datafile file number=00007 name=/home/oradata/ORCLDG/datafile/o1_mf_otc_jmrwnook_.dbf
input datafile file number=00006 name=/home/oradata/orcldg/htsl.dbf
input datafile file number=00002 name=/home/oradata/orcldg/sysaux01.dbf
input datafile file number=00001 name=/home/oradata/orcldg/system01.dbf
input datafile file number=00005 name=/home/oradata/orcldg/example01.dbf
input datafile file number=00003 name=/home/oradata/orcldg/undotbs01.dbf
input datafile file number=00004 name=/home/oradata/orcldg/users01.dbf
channel ORA_DISK_1: starting piece 1 at 02-JUL-22
channel ORA_DISK_1: finished piece 1 at 02-JUL-22
piece handle=/home/backup/orcldg/backup/rman/data/db4n11kstr_1_1 tag=TAG20220702T235019 comment=NONE
channel ORA_DISK_1: backup set complete, elapsed time: 00:03:05
channel ORA_DISK_1: starting compressed full datafile backup set
channel ORA_DISK_1: specifying datafile(s) in backup set
including current control file in backup set
including current SPFILE in backup set
channel ORA_DISK_1: starting piece 1 at 02-JUL-22
channel ORA_DISK_1: finished piece 1 at 02-JUL-22
piece handle=/home/backup/orcldg/backup/rman/data/db4o11kt3k_1_1 tag=TAG20220702T235019 comment=NONE
channel ORA_DISK_1: backup set complete, elapsed time: 00:00:01
Finished backup at 02-JUL-22
Starting backup at 02-JUL-22
RMAN-06820: WARNING: failed to archive current log at primary database ---再次尝试去主库执行alter system archive log current
ORACLE error from target database:
ORA-17629: Cannot connect to the remote database server
ORA-17627: ORA-00942: table or view does not exist
using channel ORA_DISK_1
specification does not match any archived log in the repository
backup cancelled because there are no files to backup
Finished backup at 02-JUL-22
综上所示:
1、在Oracle11.2.0.4中,在RMAN备份脚本中有 plus archivelog 参数就无需在备份归档日志之前执行: sql 'alter system archive log current'; 同时如果你单独备份归档的时候,也不需要提前执行 sql 'alter system archive log current';了,因为都会自动执行alter system archive log current'操作;
2、建议备份的时候遵从如下原则:
1)加上plus archivelog
BACKUP AS COMPRESSED BACKUPSET DATABASE FORMAT "/home/backup/orcldg/backup/rman/data/db%U" plus archivelog;
2)不加plus archivelog,建议最后再单独备份下归档日志;
backup database;
backup archivelog all;
3、Oracle 11.2.0.4不加plus archivelog,也没有在最后再备份下归档日志,那么你得到的备份结果集可能不一致,无法恢复的风险,具体报错:即便是你执行备份前执行了alter system archive log current,也无济于事!原因是你备份的归档日志文件的scn号可能小于你的某个数据文件的scn号,导致recover database的时候不能实现一致性!
ERROR at line 1:
ORA-01194: file 1 needs more recovery to be consistent
ORA-01110: data file 1: '/data/orcl/system01.dbf'
所以Oracle11.2.0.4备份任务中,没必要执行手动执行ALTER SYSTEM SWITCH LOGFILE和ALTER SYSTEM ARCHIVE LOG CURRENT这俩操作了!