oracle 热备份与恢复实验

--------------------------- 7-17_上午_oracle_热备份_01   -------------
-------------- 修改为 archive log 模式  --------------
archive log list;
show parameter archive;
alter system set log_archive_format='%s_%t_%r.arc' scope=spfile;
alter system set log_archive_dest_1='location=/arc';  -- 请先以建立arc目录,并给属主改为:oracle
shutdown immediate;
startup mount;
alter database archivelog;  -- alter database noarchivelog; 可关闭archive 模式;
alter database open;


--------------- 切换日期,以使得 archive log 生成 ----------------------------
select * from v$log; 
alter system switch logfile;
/
/
select * from v$log;


--------------- 进行热备份与恢复  ------------------
select file#, name, checkpoint_change# from v$datafile; -- 可查看本数据的数据文件位置、checkpoint 检查点的情况;
alter tablespace users begin backup;  -- 告诉 user 表空间 要开始备份了, 我要开始备份了; 1.存盘,2.冻结表空间的块头;	
select file#, name, checkpoint_change# from v$datafile;   -- 再看表空间users 数据文件 checkpoint_chang# 变化了 
host cp /u01/app/oracle/oradata/orcl/users01.dbf /bk      -- 进行热备份
startup force;   -- 启动不了,报错,说 need media recovery
select file#, name, checkpoint_change# from v$datafile; 
alter database end backup; -- 结束 备份;  4号文件变为, NOT ACTIVE , 才能正常启动;因为表空间是逻辑层的,到mount状态后,数据文件都检查完,但到open时,就要检查表空间了,而表空间又是本地管理,所以就报错;
alter database open;    -- 可以正常启动;


------------ 7-17_上午_oracle_热备份_03_验证日志增长 ------ 
---- 1 window
conn scott/tiger;
select * from tab;
drop table t1;
create table t1 as emp;
insert  into  t1 select * from t1;
/
/
/
/
/
commit;


-- 2.window
sqlplus / as sysdba;
conn / as sysdba;
SELECT * from v$sysstat where class=2;
select value from v$sysstat where STATISTIC#=178;  -- 178 是上条语句查出的 redo size 对应的 statistic# 号
SQL> SELECT value from v$sysstat where STATISTIC#=178;

     VALUE
----------
   3776244
   

-- 1.window
update t1 set sal=sal+1 where empno=7900;
commit;

-- 2.window
SELECT value from v$sysstat where STATISTIC#=178;  -- 注意比刚才大了; 约几M ,这里假定 3M
alter tablespace users begin backup;  -- 冻结表空间;

-- 1.window
update t1 set sal=sal+1 where empno=7900;
commit;

-- 2.window
SELECT value from v$sysstat where STATISTIC#=178;  -- 注意第一次update大好; 增长一倍



------------ 7-17_上午_oracle_热备份_04_备份与恢复
sqlplus / as sysdba
SQL> select name from v$datafile;

NAME
---------------------------------------------
/u01/app/oracle/oradata/orcl/system01.dbf
/u01/app/oracle/oradata/orcl/sysaux01.dbf
/u01/app/oracle/oradata/orcl/undotbs01.dbf
/u01/app/oracle/oradata/orcl/users01.dbf
/u01/app/oracle/oradata/orcl/tbsp1.dbf

SQL> !cp /u01/app/oracle/oradata/orcl/users01.dbf /bk/users01.dbf.bk2  -- 备份数据文件
update scott.emp set sal=sal+1;
commit;
alter system switch logfile;
-- 多执行几次
--- 销毁文件 
save /bk/1.txt;
select * from scott.emp;
SQL> !cp /bk/1.txt /u01/app/oracle/oradata/orcl/users01.dbf;  -- 模拟文件损坏


select * from scott.emp;  --- 有可能看见,因为在buffer_cache中
alter system flush buffer_cache;
alter system flush check_point;  -- 注意 11G 一刷数据库就自动关掉了,,,,
startup force; -- 起不来,报错
select * from scott.emp;  --- 看不见了.....报错..
SQL> select * from v$recover_file;  -- 查找 需要恢复文件
!cp /bk/users01.dbf.bk2 /u01/app/oracle/oradata/orcl/users01.dbf;  -- 取备份,恢复正确的文件回来; 
SQL> recover datafile 4;  -- 从archive log 日志中恢复数据文件;   archive log下,一路回车,只要备份文件在;
alter tablespace users online;  -- 或是 startup force;


 

你可能感兴趣的:(oracle 热备份与恢复实验)