学习资料

学习的笔记


第十九章 归档模式下的数据库恢复

<p>[TOC]</p> <h2>前一章回顾</h2> <pre><code>archive log list show parameter log_archive_dest_ show parameter log_archive_min_succeed_dest show parameter log_archive_max_processes alter system switch logfile; alter system archive log current; select * from dba_data_files; --查看数据文件备份情况 select * from v$backup; --查看数据文件对应的表空间,PIONEER_INDX file_id=12 select file_id,file_name,tablespace_name from dba_data_files; --先将该文件对应的表空间设置为备份状态 alter tablespace PIONEER_INDX begin backup; --确认文件已经设置为备份状态 select * from v$backup; --在数据文件备份状态下复制数据文件进行备份 SQL&gt; host cp /home/oracle/data/disk2/data/ORCL/datafile/o1_mf_pioneer__jgrgzf2w_.dbf /home/oracle/TBSbackup/disk2/data/ORCL/datafile --物理文件cp完成,将表空间恢复为正常非备份状态 alter tablespace PIONEER_INDX end backup;</code></pre> <h2>在归档模式下的介质恢复</h2> <ul> <li> <p>数据文件或者表空间要设置为脱机,不包括系统表空间和活动的还原表空间 使用备份修复,物理数据文件的恢复(restore) 恢复备份到崩溃这段时间的数据(recover) 回滚或者事务恢复</p> </li> <li>归档模式可以进行完全恢复和不完全恢复,重点学习完全恢复</li> </ul> <h2>数据库的完全恢复</h2> <ul> <li> <p>数据文件或者表空间设置为脱机,不包括系统表空间或者活动的还原表空间</p> </li> <li> <p>复制备份的物理数据文件到原来位置或者其他正常盘的位置</p> </li> <li>恢复备份到崩溃这段时间的数据</li> </ul> <h2>Recover 命令及与恢复有关的数据字典</h2> <ul> <li> <p>recover 命令 <code>recover [automatic] database;</code>--mount状态执行 <code>recover [automatic] tablespace;</code>--open状态执行 <code>recover [automatic] datafile;</code>--mount、open状态执行</p> </li> <li> <p>查看文件号,数据文件,表空间信息;表空间信息;数据文件状态</p> <pre><code>select file_id,file_name,tablespace_name from dba_data_files; select tablespace_name,status from dba_tablespaces; select file#,status from v$datafile;</code></pre> </li> <li> <p>标识需要恢复的数据文件、恢复何处开始;恢复期间所需要的归档日志:暂无需要恢复的为空</p> <pre><code>select * from v$recover_file; select * from v$recovery_log;</code></pre> </li> <li> <p>数据库维护时尽量使用将表空间设置为脱机,数据文件脱机复杂点</p> </li> <li>表空间脱机、数据文件脱机的区别;两个视图的介绍(PIONEER_INDX、2)</li> </ul> <pre><code>--表空间置为脱机,再联机即可用 alter tablespace PIONEER_INDX offline; select tablespace_name,status from dba_tablespaces where tablespace_name='PIONEER_INDX'; select file#,status from v$datafile where file#=2;--status=OFFLINE select * from v$recover_file;--error=OFFLINE NORMAL 数据文件正常脱机 select * from v$recovery_log;--没有要恢复的归档日志 alter tablespace PIONEER_INDX online;--重复以上四个查询 --数据文件脱机,需要恢复才可以联机 alter database datafile 2 offline; --数据文件脱机了,表空间还是联机,status=ONLINE select tablespace_name,status from dba_tablespaces where tablespace_name='PIONEER_INDX'; select file#,status from v$datafile where file#=2;--status=RECOVER,需要恢复 select * from v$recover_file;--error=NULL 数据文件脱机原因不详 select * from v$recovery_log;--没有要恢复的归档日志 alter database datafile 2 online;--ORA-01113: file 2 needs media recovery alter database recover datafile 2 ;--在SQLPLUS中可以不要alter database alter database datafile 2 online;</code></pre> <h2>Recover 期间归档日志文件的使用</h2> <ul> <li>可以手动指定归档日志所在目录</li> <li>可以自动获取所需要的归档日志文件:<code>recover automatic datafile 2;set autorecovery on</code></li> </ul> <h2>获取SQL*PLUS 命令的信息和使用方法</h2> <ul> <li>应该熟练学习SQL*PLUS命令 <pre><code>SQL&gt; help SQL&gt; help index SQL&gt; help recover</code></pre></li> </ul> <h2>数据库完全恢复方法</h2> <p><strong>想尽一切办法以最快的速度将数据库启动</strong></p> <ul> <li>在数据库最初处于打开的状态下,进行开启数据库(Open Database)的恢复</li> <li>在数据库最初处于关闭的状态下,进行开启数据库的恢复</li> <li>恢复没有备份的数据文件</li> <li>在关闭状态下进行数据库恢复</li> </ul> <h2>恢复没有备份的数据文件</h2> <ul> <li>不能是系统表空间或者活动的还原表空间</li> <li>从文件创建开始到现在的所有归档日志和重做日志都在</li> </ul> <h2>最初处于打开的状态下进行开启数据库恢复(使用EXAMPLE表空间8号文件做试验)</h2> <pre><code>select file_id,tablespace_name,file_name from dba_data_files; select tablespace_name,status from dba_tablespaces; select file#,name,status from v$datafile; alter tablespace EXAMPLE online; select * from sh.sales; select * from v$backup where file#=8;--8号文件有做备份,涉及sh.sales表 --数据文件备份的路径 [oracle@oracle datafile]$ pwd /home/oracle/TBSbackup/disk2/data/ORCL/datafile [oracle@oracle datafile]$ ll o1_mf_example_jgy0yptr_.dbf -rw-r----- 1 oracle oinstall 943726592 Jul 25 21:59 o1_mf_example_jgy0yptr_.dbf select * from v$recover_file; select * from v$recovery_log; --删除该文件 /home/oracle/data/disk2/data/ORCL/datafile/o1_mf_example_jgy0yptr_.dbf --使用如下文件进行恢复 /home/oracle/TBSbackup/disk2/data/ORCL/datafile/o1_mf_example_jgy0yptr_.dbf</code></pre> <ul> <li>删除数据文件,脱机表空间后,再联机就会报错 <pre><code>SQL&gt; alter tablespace EXAMPLE online; ORA-01157: cannot identify/lock data file 8 - see DBWR trace file ORA-01110: data file 8:'/home/oracle/data/disk2/data/ORCL/datafile/o1_mf_example_jgy0yptr_.dbf' select file_id,tablespace_name,file_name from dba_data_files where file_id=8; select tablespace_name,status from dba_tablespaces where tablespace_name='EXAMPLE'; select file#,name,status from v$datafile where file#=8; select * from v$recover_file; select * from v$recovery_log; --开始恢复,表空间与数据文件已经是脱机状态 oracle:cp /home/oracle/TBSbackup/disk2/data/ORCL/datafile/o1_mf_example_jgy0yptr_.dbf /home/oracle/data/disk2/data/ORCL/datafile/o1_mf_example_jgy0yptr_.dbf recover automatic datafile 8; alter tablespace EXAMPLE online --查询数据字典及数据表</code></pre></li> </ul> <h2>最初处于关闭的状态下进行开启数据库的恢复(使用EXAMPLE表空间8号文件做试验)</h2> <ul> <li> <p>mount状态可以使用v$datafile;不可使用dba_tablespaces</p> <pre><code>SQL&gt; shutdown immediate SQL&gt; startup mount select file#,status from v$datafile; desc dba_tablespaces SQL&gt; alter database open; select file#,status from v$datafile; select tablespace_name,status from dba_tablespaces;</code></pre> </li> <li> <p>准备环境(使用EXAMPLE表空间8号文件做试验,恢复到不同的位置)</p> <pre><code>--先确保有做热备份 select file_id,tablespace_name,file_name from dba_data_files; select tablespace_name,status from dba_tablespaces; select file#,name,status from v$datafile; SQL&gt; shutdown immediate $ rm /home/oracle/data/disk2/data/ORCL/datafile/o1_mf_example_jgy0yptr_.dbf --删除该文件 /home/oracle/data/disk2/data/ORCL/datafile/o1_mf_example_jgy0yptr_.dbf --使用如下文件进行恢复 /home/oracle/TBSbackup/disk2/data/ORCL/datafile/o1_mf_example_jgy0yptr_.dbf --恢复到 /home/oracle/data/disk3/data/ORCL/datafile/o1_mf_example_jgy0yptr_.dbf</code></pre> </li> <li>开始恢复 <pre><code>--锁定问题 SQL&gt; startup ORACLE instance started. Total System Global Area 838860800 bytes Fixed Size 8626240 bytes Variable Size 587206592 bytes Database Buffers 239075328 bytes Redo Buffers 3952640 bytes Database mounted. ORA-01157: cannot identify/lock data file 8 - see DBWR trace file ORA-01110: data file 8: '/home/oracle/data/disk2/data/ORCL/datafile/o1_mf_example_jgy0yptr_.dbf' --恢复数据库 alter database datafile 8 offline; alter database open; select tablespace_name,status from dba_tablespaces; select file#,name,status from v$datafile; select * from v$recover_file; --复制文件到另一个位置 $ cp /home/oracle/TBSbackup/disk2/data/ORCL/datafile/o1_mf_example_jgy0yptr_.dbf /home/oracle/data/disk3/data/ORCL/datafile/o1_mf_example_jgy0yptr_.dbf alter database rename file '/home/oracle/data/disk2/data/ORCL/datafile/o1_mf_example_jgy0yptr_.dbf' to '/home/oracle/data/disk3/data/ORCL/datafile/o1_mf_example_jgy0yptr_.dbf'; recover automatic datafile 8; alter database datafile 8 online; select tablespace_name,status from dba_tablespaces; select file#,name,status from v$datafile; select * from sh.sales;--数据回来了</code></pre></li> </ul> <h2>在关闭状态下进行数据库恢复</h2> <ul> <li>属于系统表空间或者活动的还原表空间</li> <li>超过一半以上的数据要恢复</li> <li>非7乘24运营数据库</li> <li> <p>准备工作(试验表空间 SYSTEM,1号文件)</p> <pre><code>select file_id,tablespace_name,file_name from dba_data_files; select tablespace_name,status from dba_tablespaces; select file#,name,status from v$datafile; --原来数据文件 /home/oracle/data/disk1/sys/system01.dbf select * from v$backup;--2021/7/25 21:58:26 --备份文件 /home/oracle/TBSbackup/disk1/sys/system01.dbf --恢复到 /home/oracle/data/disk3/data/ORCL/datafile/system01.dbf --关闭数据库,删除数据文件</code></pre> </li> <li>进行恢复 <pre><code>SQL&gt; startup mount SQL&gt; host cp /home/oracle/TBSbackup/disk1/sys/system01.dbf /home/oracle/data/disk3/data/ORCL/datafile/system01.dbf SQL&gt; alter database rename file '/home/oracle/data/disk1/sys/system01.dbf' to '/home/oracle/data/disk3/data/ORCL/datafile/system01.dbf'; SQL&gt; recover automatic database; SQL&gt; alter database open; SQL&gt; select tablespace_name,status from dba_tablespaces where tablespace_name='SYSTEM' ;</code></pre></li> </ul> <h2>个人测试实际</h2> <pre><code>archive log list --归档目录:/home/oracle/dbdata/disk4/offlinelog select * from v$parameter where name like 'log_archive_dest_%' and isbasic='TRUE'; --备份日志:2021/7/24 16:13:30 select * from v$backup; --热备份目录:/home/oracle/TBSBackup/disk1/orcl select tablespace_name,file_name,file_id from dba_data_files; select tablespace_name,status,contents,logging from dba_tablespaces; select name,status from v$datafile; --情景1:disk1盘坏了(系统表空间,需要关机恢复) 1 /home/oracle/dbdata/disk1/orcl/system01.dbf SYSTEM 2 /home/oracle/dbdata/disk1/orcl/system02.dbf SYSTEM 3 /home/oracle/dbdata/disk1/orcl/sysaux01.dbf ONLINE 4 /home/oracle/dbdata/disk1/orcl/undotbs01.dbf ONLINE -- 开机报错1号文件 SQL&gt; startup ORACLE instance started. Total System Global Area 838860800 bytes Fixed Size 8626240 bytes Variable Size 641732544 bytes Database Buffers 184549376 bytes Redo Buffers 3952640 bytes Database mounted. ORA-01157: cannot identify/lock data file 1 - see DBWR trace file ORA-01110: data file 1: '/home/oracle/dbdata/disk1/orcl/system01.dbf' -- 查看数据文件 col name for a50 col status for a10 SQL&gt; select name,status from v$datafile; NAME STATUS -------------------------------------------------- ---------- /home/oracle/dbdata/disk1/orcl/system01.dbf SYSTEM /home/oracle/dbdata/disk1/orcl/system02.dbf SYSTEM /home/oracle/dbdata/disk1/orcl/sysaux01.dbf ONLINE /home/oracle/dbdata/disk1/orcl/undotbs01.dbf ONLINE /home/oracle/dbdata/disk2/orcl/tbs01.dbf ONLINE /home/oracle/dbdata/disk2/orcl/users01.dbf ONLINE /home/oracle/dbdata/disk2/orcl/indx02.dbf ONLINE /home/oracle/dbdata/disk2/orcl/ts_data01.dbf ONLINE /home/oracle/dbdata/disk2/orcl/tbs02.dbf ONLINE /home/oracle/dbdata/disk2/orcl/PIONEER_DATA01.dbf ONLINE /home/oracle/dbdata/disk2/orcl/PIONEER_INDX01.dbf ONLINE [oracle@oracle disk1]$ cd /home/oracle/dbdata/disk1 [oracle@oracle disk1]$ ll total 4 drwxr-xr-x 2 oracle oinstall 4096 Aug 5 17:23 bkorcl -- 无法启动是因为 /home/oracle/dbdata/disk1/orcl 这个盘坏了 -- 开始恢复,restore\recover\open -- 恢复至 /home/oracle/dbdata/disk0/orcl 这个盘 -- restore\rename -- $ cp /home/oracle/TBSBackup/disk1/orcl/*.dbf /home/oracle/dbdata/disk0/orcl alter database rename file '/home/oracle/dbdata/disk1/orcl/system01.dbf' to '/home/oracle/dbdata/disk0/orcl/system01.dbf'; alter database rename file '/home/oracle/dbdata/disk1/orcl/system02.dbf' to '/home/oracle/dbdata/disk0/orcl/system02.dbf'; alter database rename file '/home/oracle/dbdata/disk1/orcl/sysaux01.dbf' to '/home/oracle/dbdata/disk0/orcl/sysaux01.dbf'; alter database rename file '/home/oracle/dbdata/disk1/orcl/undotbs01.dbf' to '/home/oracle/dbdata/disk0/orcl/undotbs01.dbf'; -- recover recover automatic database; -- open alter database open; -- 检查 select tablespace_name,file_name,online_status from dba_data_files; select tablespace_name,contents,status from dba_tablespaces; select file#,name,status from v$datafile; --情景2: 11 /home/oracle/dbdata/disk2/orcl/PIONEER_DATA01.dbf 这个文件丢失 select * from v$backup where file#=11; [oracle@oracle orcl]$ pwd /home/oracle/TBSBackup/disk2/orcl [oracle@oracle orcl]$ ll -rw-r----- 1 oracle oinstall 104865792 Jul 24 16:15 PIONEER_DATA01.dbf alter tablespace PIONEER_DATA offline; SQL&gt; alter tablespace PIONEER_DATA online; ORA-01157: cannot identify/lock data file 11 - see DBWR trace file ORA-01110: data file 11: '/home/oracle/dbdata/disk2/orcl/PIONEER_DATA01.dbf' select tablespace_name,file_name,online_status from dba_data_files where tablespace_name='PIONEER_DATA'; select tablespace_name,contents,status from dba_tablespaces where tablespace_name='PIONEER_DATA'; select file#,name,status from v$datafile where file#=11; --restore\recover\online cp /home/oracle/TBSBackup/disk2/orcl/PIONEER_DATA01.dbf /home/oracle/dbdata/disk2/orcl/PIONEER_DATA01.dbf recover automatic datafile 11; alter database datafile 11 online; alter tablespace PIONEER_DATA online;</code></pre>

页面列表

ITEM_HTML