oracle07_backup

 一、冷备

	1.生成备份脚本
spool coldscript.sql
set heading off   --不显示表头
set feedback off   --不显示总计
select '&cp '||name||' &destdir' from  --windows运行则前面先输入copy /y,后输f:\backup\. linux运行先输入cp, 后输 /backup/
(select name from v$datafile
UNION ALL
select name from v$tempfile
UNION ALL
select member from v$logfile
UNION all
select name from v$controlfile
UNION ALL
select value from v$parameter where name='spfile');
spool off;
	2.关闭数据库 shutdown immediate;
	3.运行脚本备份 
	4.开启数据库   startup

--另外再生成还原脚本保存起来,供还原用
select '&cp &destdir'||substr(name,instr(name,'\',-1)+1)|| ' '||name 
from  --windows运行则前面先输入copy /y,后输f:\backup\. linux运行先输入cp, 后输 /backup/,instr函数里'\'要换成'/'
(select name from v$datafile
UNION ALL
select name from v$tempfile
UNION ALL
select member from v$logfile
UNION all
select name from v$controlfile
UNION ALL
select value from v$parameter where name='spfile');

二、 在没有任何备份的情况下,出现数据库故障,特殊恢复方法

--<恢复临时表空间>  
-> 思路:新建个临时表空间,再将默认临时表空间改为新建的那个。--(11g如果临时表空间的数据文件丢失,启动数据库时会自动重建)
	create temporary tablespace temp6 tempfile '/oracle/oradata/pod/temp6.dbf' size 20m ;
	alter database default temporary tablespace temp6;
	查看:select username,temporary_tablespace from dba_users;

--<恢复一个重做日志组成员>  当前状况是:3 group ,2 member
1.丢失的是inactive组其中一个member
	切换日志:alter system switch logfile;  多切几次发现没有影响.
	总结:当redo 成员丢失时候,只要另一个member是正常的,数据库没有影响.通过告警找到哪个member丢失,及时重建就可以恢复	 
	重建日志:alter database add logfile member '   '  to group 3; 告警里丢失的那个文件名
	查看告警:
Mon Aug 16 10:22:43 2010
Errors in file /u01/app/oracle/admin/pod/bdump/pod_arc2_5887.trc:
ORA-00313: open failed for members of log group 3 of thread 1
ORA-00312: online log 3 thread 1: '/u01/app/oracle/oradata/pod/redo03.log'
ORA-27037: unable to obtain file status
Linux Error: 2: No such file or directory
Additional information: 3
Mon Aug 16 10:22:43 2010
Errors in file /u01/app/oracle/admin/pod/bdump/pod_arc2_5887.trc:
ORA-00313: open failed for members of log group 3 of thread 1
ORA-00312: online log 3 thread 1: '/u01/app/oracle/oradata/pod/redo03.log'
ORA-27037: unable to obtain file status
Linux Error: 2: No such file or directory

2.丢失的是一个inactive组全部的成员
	a.如果是非归档模式:
	alter database clear logfile group 3; 	-->根据控制文件来重建日志组。 .log文件会自动建立
	b.如果是归档模式:
	alter database clear logfile group 3;  -->先重建日志组
*
ERROR at line 1:
ORA-00350: log 3 of instance pod (thread 1) needs to be archived
ORA-00312: online log 3 thread 1: '/u01/app/oracle/oradata/pod/redo03.log'
ORA-00312: online log 3 thread 1: '/u01/app/oracle/oradata/disk1/redo0302.log'
	alter database clear unarchived logfile group 3;  -->如果日志未归档,重建会报错需归档,用unarchived强行重建日志组

3.丢失的是当前写的那个组全部成员
SQL> alter database clear logfile group 1; -->重建日志组,不行
alter database clear logfile group 1
*
ERROR at line 1:
ORA-01624: log 1 needed for crash recovery of instance pod (thread 1)
ORA-00312: online log 1 thread 1: '/u01/app/oracle/oradata/pod/redo01.log'
ORA-00312: online log 1 thread 1: '/u01/app/oracle/oradata/disk1/redo0102.log'

SQL> alter database clear unarchived logfile group 1; -->强行重建日志组,还是不行
alter database clear unarchived logfile group 1
*
ERROR at line 1:
ORA-01624: log 1 needed for crash recovery of instance pod (thread 1)
ORA-00312: online log 1 thread 1: '/u01/app/oracle/oradata/pod/redo01.log'
ORA-00312: online log 1 thread 1: '/u01/app/oracle/oradata/disk1/redo0102.log'

方法:隐含参数
	编辑pfile vim initpod.ora 加入:
		_allow_resetlogs_corruption=TRUE

	利用pfile重启到mount状态:startup force mount pfile='';

	SQL> recover database until cancel;
		Specify log: {<RET>=suggested | filename | AUTO | CANCEL}
		cancel   <---先输入cancel 

	SQL> recover database until cancel;
		Specify log: {<RET>=suggested | filename | AUTO | CANCEL}
		auto   <---输入auto 

	SQL> recover database until cancel;
		Specify log: {<RET>=suggested | filename | AUTO | CANCEL}
		cancel   <---输入cancel 

	ORA-01112: media recovery not started
	出现报错,不必理会,启库
	SQL> alter database open resetlogs;

	立刻执行备份,关闭数据库,隐含参数去掉,_allow_resetlogs_corrup参数,启库

--<索引表空间丢失>
	1. you  need to re_create 
	2. drop the index tablespace

--<密码文件丢失:>
	1. rm $ORACLE_HOME/dbs/orapwSID
	2. orapwd file=$ORACLE_HOME/dbs/orapwSID   password=*** 

三、undo 丢失恢复

	得到undo表空间名: show parameter undo_tabl  -> 为 UNDOTBS1
	查undo表空间文件名:  select file_id,file_name from dba_data_files where tablespace_name='UNDOTBS1' ;
	删除文件

	--丢失后dml无法操作,
	--查看告警alertSID.log,可知丢失的是哪个文件
	alertsid.log:
ORA-01157: cannot identify/lock data file 2 - see DBWR trace file
ORA-01110: data file 2: '/u01/app/oracle/oradata/pod/undotbs01.dbf'
ORA-27037: unable to obtain file status
Linux Error: 2: No such file or directory
Additional information: 3
	--数据库open时根据控制文件的内容打开数据文件,物理上的数据文件丢失,则要控制文件不去找这个文件,在逻辑上将些文件也跳过:
	逻辑上跳过: alter database datafile  2 offline drop;	
	启库: alter database open;
	新建undo: create undo tablespace undo1 datafile '/oracle/oradata/pod/undo1.dbf' size 445m; -->大小要跟以前一样
	将undo表空间改为新建的:alter system set undo_tablespace=UNDO1; 若以前就已经创建了undo表空间,现在可以不用新建
	将以前逻辑上的undo表空间删除: drop tablespace undotbs1 including contents and datafiles;

四、exp/imp

--<导出工具 exp >
查看帮助: exp help=y
exp导出工具将数据库中的数据备份压缩成二进制操作系统文件,可以在不同os迁移

1.导出表: 导出的dmp文件已存在,会覆盖原来
	$ exp scott/tiger tables=dept,emp file=/oracle/Disk2/t2.dmp log=/oracle/Disk2/t2.log -->导出多个表
	$ exp  "'sys/oracle as sysdba'" tables=scott.dept  file=/oracle/Disk2/users.dmp  -->sys导scott的表
	$ exp scott/tiger@sky  tables=t_181:p1  file=/oracle/Disk2/T_181.dmp    -->远程导出分区表

2.导出整个scott方案:
	$ exp scott/tiger owner=scott file=/oracle/Disk2/scott.dmp log=scott.log  -->会导出scott的所有对象

导出某用户下所有表:
思路:查出该用户拥有的表,写脚本将其全部导出
	spool export.txt
	set heading off
	set feedback off
	select 'ho exp scott/tiger tables='||table_name||' file=/oracle/disk1/'||table_name||'.dmp' 
	from dba_tables where owner='SCOTT';
	spool off
	完成后将脚本编辑,将第一行和最后一行删掉,保存后运行,并且可以通过crontab 指定时间夜里执行备份 

3.导出表空间:
	$ exp system/oracle tablespaces=users  file=/oracle/Disk2/users.dmp

4.全库导出:
	$ exp system/oracle full=y file=/u01/app/oracle/Disk2/full.dmp
	   		\-->普通用户如果要做全库导出,需要权限:exp_full_database
			
select ROLE ,PRIVILEGE FROM role_sys_privs where role='EXP_FULL_DATABASE';  --查看exp_full_database拥有的权限

ROLE                           PRIVILEGE
------------------------------ ----------------------------------------
EXP_FULL_DATABASE              READ ANY FILE GROUP
EXP_FULL_DATABASE              EXECUTE ANY PROCEDURE
EXP_FULL_DATABASE              RESUMABLE
EXP_FULL_DATABASE              SELECT ANY TABLE
EXP_FULL_DATABASE              EXECUTE ANY TYPE
EXP_FULL_DATABASE              BACKUP ANY TABLE
EXP_FULL_DATABASE              ADMINISTER RESOURCE MANAGER
EXP_FULL_DATABASE              SELECT ANY SEQUENCE

	
用scott用户做个全库导出:(不可以给Dba角色)
	grant exp_full_database to scott;  先用SYS授权

	ho exp full=y  file=/u01/app/oracle/Disk2/fulls.dmp
	Username: scott
	Password: 

--<导入工具 imp >
将操作系统压缩的二进制文件dmp文件 ,在导入数据库中
dba, imp_full_database 角色才能作全库导入

1.导入表:
	$ imp system/oracle tables=etable  file=etable_181.dmp  fromuser=eman touser=system 
	$ imp system/oracle@sky  tables=ee file=/ee.dmp fromuser=scott touser=system ignore=y
	  imp 所做的工作 :
	1) create table  2)insert data  3)create index ,trigger,constraints..
	将eman用户下的表导入到system用户下,用fromuser  touser, 表名前不用加用户.
	因imp会create 表,若system用户已经有了这个表,用ignore=y 忽略创建错误 进行追加,(已经有的表跟要导入的表结构要相同)

导入表空间:
	$ imp system/oracle tablespaces=etbsp  file=etbsp.dmp 
	-->因表空间下的表应该属于某个用户,故就先查出表空间下有哪些用户,先创建这些用户,再导入表空间
	create user u1 identified by u1;
	grant connect,resource to u1

导入方案:
	$ imp system/oracle full=y  file=etbsp.dmp  --> imp 没有owner=,用full=y将exp owner=导出的方案全部导入 


<传输表空间:>
	exp和imp时要加参数 transport_tablespace=y,只导表空间下的所有表的结构,不导数据,imp时要指定datafiles=''
	dbf文件cp到目标机,库迁移时可提高速率
	$ exp tablespaces=sky file=sky.dmp transport_tablespace=y
	$ imp tablespaces=sky file=sky.dmp transport_tablespace=y datafiles=/oracle/oradata/pod/sky.dbf 

  要传输的表空间有index,依附于另一个表空间的表,传输时会报错 非自包含(not self-contained),应该先校验
	校验: SQL> exec sys.dbms_tts.transport_set_check('tt1_e2',true);
	查看结果: SQL> select * from sys.transport_set_violations; --->空间内有索引依附于其他表空间
	两个一起校验:SQL> exec sys.dbms_tts.transport_set_check('tt1_e2,TT1_E1',true);
	查看结果: SQL> select * from sys.transport_set_violations; -->没有记录,说明该表空间一起可以传输
	将两个表空间同时导出:
		SQL> alter tablespace TT1_E1 read only;
		SQL> alter tablespace tt1_e2 read only;
		SQL> ho exp tablespaces=tt1_e2,TT1_E1  transport_tablespace=y file=tt1_e12.dmp

	将导出的.dmp文件及.dbf文件拷贝到目标机scp  *.dmp  scp  *.dbf

	查看这两个表空间下的用户:在目标机上创建这些用户
	select username,default_tablespace from dba_users where default_tablespace in('TT1_E1','TT1_E2');
	create user e1 identified by e1;

	导入:$ imp tablespaces=tt1_e1,tt1_e2 transport_tablespace=y file=/con/tt1_e12.dmp datafiles=/oracle/oradata/sky/tt1_e1.dbf,/oracle/oradata/sky/tt1_e2.dbf


五、expdp/impdp :10g 新增的,基于api模式,速度要快4~5倍

数据泵 要有directory
 创建directory:
	SQL> create directory dir as  '/oracle/Disk2/';
	SQL> grant read,write on directory dir to scott,text;
	SQL> select * from dba_directories where directory_name='DIR';

--<expdp>
导出表 :
	$ expdp  scott/tiger tables=dept,emp dumpfile=dept.dmp directory=dir logfile=dept.log -->导出之前会先估算大小
导出方案:
	$ expdp system/oracle schemas=scott dumpfile=scott.dmp directory=dir
导出表空间:
	$ expdp system/oracle tablespaces=tt1_e1 dumpfile=tt1_e1.dmp directory=dir  
全库导出:
	$ expdp system/oracle full=y dumpfile=full.dmp directory=dir logfile=full.log
并行导出:
	$ expdp system/oracle full=y dumpfile=full_%u.dmp directory=dir parallel=4 -->并行度为4生成4个dmp文件,文件命名有加%u

--<impdp>
导入表:
	$ impdp system/oracle tables=t1 dumpfile=t1_x.dmp directory=dir remap_schema=text:scott  从text用户导入到scott用户
	$ impdp  text/text tables=t1,t2 dumpfile=t1.dmp directory=dir table_exists_action=append
	  
导入方案:
	$ impdp system/oracle schemas=scott dumpfile=scott.dmp directory=dir remap_schema=scott:system

并行导入:
	$ impdp system/oracle schemas=scott dumpfile=scott_%u.dmp directory=dir parallel=4 table_exists_action=replace

--<数据泵参数:>
1.content : 
	data_only:仅导出行记录
	metadata_only:进导出结构信息,没有行记录
	all:默认,以上都导出
2.table_exists_action:							
	skip: 跳过去  默认为此
	replace:替换
	append:追加
	truncate:清空行记录,再追加进去
3.estimate_only=y  仅作估算,不做导出,不要指定dumpfile,导出前查看磁盘空间是否足够
	$ expdp full=y directory=dir estimate_only=y 
4. exclude     eg . exclude=table:emp  ,   exclude=table
	选项用于指定操作时候释放要排除对象类型或者相关对象
	object_type用于指定要排除的类型
	name_clause用于指定要排除的具体对象
	exclude 和include  不能同时使用
5.filesize :指定导出文件的最大尺寸 默认为0(不限制),导出文件大于最大尺寸时,分成多个文件,注意dmp命名%u

ORA-39006: internal error
内部错误,数据泵异常,不可用:
解决方法:
@?/rdbms/admin/catdph.sql
@?/rdbms/admin/prvtdtde.plb
@?/rdbms/admin/catdpb.sql
@?/rdbms/admin/dbmspump.sql
@?/rdbms/admin/utlrp.sql

六、5种闪回技术

<1.flashback drop 功能>
	将drop的表从recyclebin(回收站)里闪回 。注意:不管是哪个用户删除的,只要是system表空间的对象就无法闪回。
	只针对表,视图索引这些删除不会放回收站。但是删除的表上的索引触发器等也跟着放到回收站中,当将该表闪回回来后,相关的索引和触发器也跟着恢复。
	普通用户只能在回收站看见删除的表,dba才能在回收站看见索引、触发器..
查看回收站
	show recyclebin			->普通用户
	select owner,object_name,original_name from dba_recyclebin;  ->dba
闪回删除的表:
	drop table emp;
	flashback table emp to before drop; ->表的属主和dba都可以闪回删除的表
	flashback table scott.emp to before drop rename to emp1; ->删表后建了同命表,闪回时会报错 对象已存在,改名。emp1还是scott的

	book表删除后又建了同结构的同名表,想把以前的数据也插入现在的book表
	flashback table book to before drop rename to book1 ;
	insert into book select * from book1;
彻底删除表:
	drop table emp purge;
清空回首站内容:
	purge recyclebin;
	purge dba_recyclebin;
	purge table emp;     ->清除回收站中指定表
	purge  tablespace tt1_e1; ->清除回收站中指定表空间下的所有对象
	purge  tablespace tt1_e1  user e1; ->清除指定表空间下的某个指定用户在回收站中的所有对象

<2.flashback database 功能>  
	要在归档模式下,启动闪回功能,数据库会将变化的数据由rvwr进程写入到闪回日志文件中,在进行数据库闪回时,这些数据块直接复制过来。
	可以这样理解,redo是(ddl,dml)sql语句 ,undo 是数据块,闪回日志是redo + undo
开启闪回功能:  --->要在mount 状态下
	startup force mount
	alter database flashback on; -->开启闪回功能要求数据库必须在归档模式
	alter database open;

查看状态:
	select flashback_on from v$database;
	FLASHBACK_ON
	------------------
	YES			--->表示闪回开启

更改闪回文件路径,大小,时间
	alter system set db_recovery_file_dest='..' ;   ---->改闪回路径,闪回文件很大,可另外放一块盘
	alter system set db_recovery_file_dest_size=3g; --->改闪回空间大小
	alter system set db_flashback_retention_target=720; -->改数据库能够闪回的最大时间上限,默认1440分钟(1天)


select to_char( oldest_flashback_time,'yyyy-mm-dd hh24:mi:ss') from  v$flashback_database_log;
TO_CHAR(OLDEST_FLAS
-------------------
2010-08-18 11:14:18  -->能闪回去的最远时间

查看scn号:  ---> scn与时间点是一一对应,一直增长的
	select time_dp,scn from smon_scn_time;

scn号与时间点相互转换:
	select scn_to_timestamp(2421047) from dual;
	select timestamp_to_scn(scn_to_timestamp(2421047)) from dual;

	SQL> alter session set nls_date_format='yyyy-mm-dd hh24:mi:ss'; -->改显示时间格式
	SQL> select dbms_flashback.get_system_change_number fscn from dual;  -->查此时的 scn 号
 
闪回数据库步骤:
	startup force mount --> mount状态下闪回
	flashback database to timestamp  to_timestamp('2010-08-18 11:40:59','yyyy-mm-dd hh24:mi:ss');
	alter database open resetlogs;  --->重设归档日志

<3. 闪回一张表>
表没有被drop,而是删除了其中的数据

语法:
	alter table table_name enable row movement; -->数据删除后,后来的数据会覆盖原来的块,若要闪回必须要允许row movement
	flashback table table_name to scn|timestamp ;

	SQL> alter table book1 enable row movement;
	SQL> flashback table book1 to scn 2429928; 

	--->scn号猜测
	select count (*) from ob as of scn  2002676; 
	ORA-01466: unable to read data - table definition has changed  -->猜的时间太早表还没建会报此错


问题2:
某人删除某个闪回日志:
	startup force  -->当时使用没问题,重新启动控制文件找不到闪回日志会报错,只能开到mount状态
	ORA-38760: This database instance failed to turn on flashback database

	解决--mount下 关闭闪回,使控制文件不找闪回日志,重启到mount状态,再打开闪回,开库
	alter database flashback off; 
	alter database open;
	startup force mount
	alter database flashback on;
	alter database open;

问题3:
怎样正确的删除闪回日志:
	SQL> select * from v$flash_recovery_area_usage;
	FILE_TYPE    PERCENT_SPACE_USED PERCENT_SPACE_RECLAIMABLE NUMBER_OF_FILES
	------------ ------------------ ------------------------- ---------------
	CONTROLFILE                   0                         0               0
	ONLINELOG                     0                         0               0
	ARCHIVELOG                  .04                         0               4
	BACKUPPIECE                   0                         0               0
	IMAGECOPY                     0                         0               0
	FLASHBACKLOG                .76                         0               1
	-->假设发现闪回日志下有大量归档日志文件,尽量把归档目录单独存放
	
	alter system set log_archive_dest_1='location=/u01/app/oracle/archivelog/db/'; 

如果有大量闪回日志文件,不需要,要删除手工
	alter database flashback off;
	rm flashlog;mount
	alter database flashback  on ;


问题4:
闪回丢失的方案: drop user eman cascade;  -->整个方案丢失 
思路:
	mount状态: 闪回方案丢失前
	open read only: 整个方案导出
	mount状态: recover databasd;回到现在
	open数据库 导入


	select username from dba_users; ---发现用户不存在
	准备把数据库推移到未误操作前,把owner导出来 
	startup force mount 
	flashback database to timestamp to_date('2010-08-18  15:06:53','yyyy-mm-dd hh24:mi:ss');
	alter database open read only;
	select username from dba_users; -->查看用户,发现存在
	ho exp system/oracle owner=eman file='/oracle/disk1/eman.dmp' --->导出整个方案
	startup force mount   
	recover database;  --->数据库回到现在状态(mount下进行)
	alter database open;
	ho imp system/oracle file=/oracle/disk1/eman.dmp full=y  --->导出可以按owner,导入不行,用full=y 
==============================================
同一服务器上可以有两个同名的数据库,但是不能有两个同名的实例
SQL> select dbid,name from v$database;
      DBID NAME
---------- ---------
1469001501 POD    -->dbid用来惟一标识数据库
===============================================

七、RMAN

rman热备,表空间offline,read only是不会备份
全库还原恢复mount状态,其余可在open状态下进行

确定为归档模式:SQL> archive log list;
确定要备份的数据库是哪个:	$ echo $ORACLE_SID 
连接: $ rman target system/oracle
RMAN> show all;
	using target database control file instead of recovery catalog
	rman configuration parameters are:
	configure retention policy to redundancy 1; # default -->备份集的保留冗余, recovery window of 7 days;保留的时间
	configure backup optimization off; # default
	configure default device type to disk; # default  -->备份类型磁盘,sbt表示:磁带
	configure controlfile autobackup off; # default  -->on后,控制文件和spfile自动备份
	configure controlfile autobackup format for device type disk to '%F'; # default -->备份格式 只适用于磁盘设备
	configure device type disk parallelism 1 backup type to backupset; # default -->备份并行度,多个通道同时工作,提高备份速度
	configure datafile backup copies for device type disk to 1; # default
	configure archivelog backup copies for device type disk to 1; # default
	configure maxsetsize to unlimited; # default
	configure encryption for database off; # default  -->on后,对备份集进行加密
	configure encryption algorithm 'aes128'; # default
	configure archivelog deletion policy to none; # default
	configure snapshot controlfile name to '/oracle/product/10g/dbs/snapcf_ocm.f'; # default -->控制文件的快照

一次备份到多个路径下,启用多个通道实现:  将一个备份集分成两个备份片同时备份
	RMAN> configure channel 1 device type disk format '/u01/app/oracle/Disk1/backup_%U';  -->1 2 名称随便取
	RMAN> configure channel 2 device type disk format '/u01/app/oracle/Disk2/backup_%U';
恢复为默认:会备份到闪回区
	RMAN> configure channel 1 device type disk clear;
	RMAN> configure channel 2 device type disk clear;


	RMAN> backup validate database; -->检查数据库中所有的dbf是否正常,比如说是否有坏块等
	RMAN> backup database; -->备份数据库,会自动检测
	RMAN> backup tablespace system; -->备份表空间
	RMAN> backup archivelog all; -->备份归档
	RMAN> backup archivelog all delete input; -->,归档日志要求空间比较大,rman每次备份走以后,可以自动清除,释放空间
	RMAN> copy datafile 1 to '/u01/app/oracle/Disk2/dbf1.cpy';  --> 拷贝数据文件

	RMAN> list backup; ---->列出备份
	RMAN> list backup of controlfile;
	RMAN> list backup of spfile;
	RMAN> list copy;--->列出copy
	RMAN> list archivelog all; --->列出关于归档的备份

	RMAN> restore database validate; --> 验证备份的restore
	RMAN> report schema;--> 查看数据库的物理结构
	RMAN> list incarnation;--> 数据库原型列表,记录 resetlogs

	RMAN> crosscheck backup; --> 检测标识过期和失效
	RMAN> list expired backup;--> 列出失效的对象,在物理上已删除,但逻辑上还有的对象
	RMAN> delete noprompt expired backup;--> 确定删除失效备份
	
	RMAN> crosscheck archivelog all;
	RMAN> list expired archivelog all;
	RMAN> delete expired archivelog all;

	RMAN> report obsolete;--> 列出已经过期的备份
	RMAN> delete noprompt obsolete; --> 确定删除已经过期的备份

	RMAN> sql 'alter system switch logfile';  --> 用rman执行sql语句
--------------------------------------------
备份:
	backup database;
	backup archivelog all delete input;
	backup archivelog from time 'sysdate-12/24';
	backup tablespace users;
	copy datafile 11 to '';
检查备份集:
	crosscheck backup;
	crosscheck archivelog all;
列出备份:
	list backup;
	list archivelog all; --列出的是没有备份的归档
	list backup of archivelog all; --列出备份的归档
	list backup of controlfile;
	list backup of spfile;
	list copy;
	list expired backup;
	list expired archivelog all;

	report schema;
	report obsolete;
删除备份:
	delete expired backup;
	delete expired archivelog all;
	delete obsolete;
	delete archivelog until time 'sysdate-1';
--------------------------------------------
恢复目录  catalog  
用DB datbase 作为恢复目录
目标库是ocm 

DB库
	SQL> create user ctl identified by ctl;
	SQL> grant connect,resource ,recovery_catalog_owner to ctl;

	[oracle@fly ~]$ echo $ORACLE_SID
	ocm
	[oracle@fly ~]$ rman target / catalog ctl/ctl@DB  -->每次使用rman就用这种方式
	RMAN> create catalog;
	RMAN> register database;
--------------------------------------------

<完善备份策略:>
数据量:1t   每天归档2~3g ,/archive每5小时会占空间的80%,满了以后redo无法归档,dml ddl无法进行 
周日凌晨作全备 fullbak.sh ,每4小时备归档 archive.sh

	$ crontab -e
	0 0 * * 7 sh /home/oracle/fullbak.sh &>/dev/null
	0 */4 * * * sh /home/oracle/archive.sh &>/dev/null

fullbak.sh
	export ORACLE_SID=ocm
	export ORACL_BASE=/oracle
	export ORACLE_HOME=/oracle/product/10g
	rq=`date +"20%y%m%d%H%M%S"`
	/oracle/product/10g/bin/rman target / log=/oracle/disk1/backall_log_$rq.log<<EOF
	run{
	allocate channel ch1 device type disk;
	allocate channel ch2 device type disk;
	sql 'alter system switch logfile';
	backup database format '/oracle/disk1/db_%U_%d';
	sql 'alter system switch logfile';
	backup archivelog all format '/oracle/disk1/arch_%t_%s' delete all input;
	backup format '/oracle/disk1/con_%s_%p' current controlfile;
	crosscheck backup;
	crosscheck archivelog all;
	delete noprompt obsolete;
	delete noprompt expired backup;
	delete noprompt backup of database completed before 'sysdate -15';
	delete noprompt backup of archivelog all completed before 'sysdate -15';
	release channel ch1;
	release channel ch2;
	}
	EOF

archive.sh
	export ORACLE_SID=ocm
	export ORACL_BASE=/oracle
	export ORACLE_HOME=/oracle/product/10g
	rq=`date +"20%y%m%d%H%M%S"`
	/oracle/product/10g/bin/rman target / log=/oracle/disk1/backall_log_$rq.log<<EOF
	run{
	allocate channel ch1 device type disk;
	allocate channel ch2 device type disk;
	sql 'alter system switch logfile';
	backup archivelog all format '/oracle/disk1/arch_%t_%s' delete all input;
	crosscheck backup;
	crosscheck archivelog all;
	delete noprompt obsolete;
	delete noprompt expired backup;
	release channel ch1;
	release channel ch2;
	}
	EOF


<估算备份数据量的有效>
查看数据库的使用空间:
	select (sum1-sum2)/1048576 
	from (select sum(bytes) sum1 from dba_data_files),(select sum(bytes) sum2 from dba_free_space);
	(SUM1-SUM2)/1048576
	-------------------
	             863.25 rman只备份这些使用过的块,最后备份出来的文件大小要小于这个数字,因为备份出来的都是数据,没有碎片,当然要小 

监控rman备份进度:
	select sid,serial#,context,sofar,totalwork,round(sofar/totalwork*100,2) "%_com" from v$session_longops
	where opname like 'RMAN%'  -->也可写成'EXPDP%' 'EXP%'监控EXP导数据的进度 
	and totalwork<>0
	and sofar<>totalwork;

备份时异常结束,rman再接着备份
	RMAN> backup not backed up since time 'sysdate -2'  2天内没备完的接着备

使用clear将配置回到默认值
	RMAN> configure retention policy clear;
	RMAN> configure retention policy to none; -使不起作用

<restore and recover>
还原了的文件还能被恢复,还原是物理的 恢复是逻辑的一致性
场景1:一个表空间,或者数据文件丢失
情况一:database  open
	select file#,error from v$recover_file; 查看丢失的文件id号
	RMAN> run{
	allocate channel ch1 type disk;
	sql 'alter database datafile 11 offline';
	restore datafile 11;
	recover datafile 11;
	sql 'alter database datafile 11 online';
	}

情况二:database  mount
	SQL> select t.name,r.error,d.name from v$tablespace t,v$datafile d,v$recover_file r where t.ts#=d.ts# and d.file#=r.FILE#;

	RMAN> run{
	allocate channel a1 type disk;
	restore datafile 11;
	recover datafile 11;
	sql 'alter database open';}

场景2:controlfile镜像全丢
数据库只能nomount状态
	RMAN> run{
	allocate channel ch1 type disk;
	restore controlfile from autobackup;
	alter database mount;
	recover database;
	alter database open resetlogs;
	}

场景3:周日全备份,周一dba创建了一个新tablspace,周二下午,丢失3个dbf,其中一个dbf,就是新建立的tablespace
4,11号文件全备份里有,32号没有,但是有归档备份
	通过告警:得知哪些文件丢失
	RMAN> run{
	allocate channel ch1 type disk;
	restore datafile 4,11,32;  -->4,11 从备份集里还原,32备份集里没有,会新建这个dbf
	recover datafile 4,11,32;  -->恢复 4,11,再找归档备份 找redo向32 里插入数据.
	}

	select open_mode from v$database;

场景4:
	模拟介质故障,数据库只能mount状态:  将某dbf还原到别的路径   
	disk a over  ---->disk c 
思路:
datafile 11 offline  , open
set newname for datafile 11 to  '  disk c   ';
restore datafile ...
switch datafile .. --->更新控制文件里的路径
recover datafile ...

	RMAN> run{
	allocate channel ch1 type disk;
	sql 'alter database datafile 11 offline';
	sql 'alter database open';
	set newname for datafile 11 to '/u01/app/oracle/Disk1/users01.dbf';
	restore datafile 11;
	switch datafile all;
	recover datafile 11;
	sql 'alter database datafile 11 online';
	}

	RMAN> run{
	allocate channel ch1 type disk;
	sql 'alter database datafile 11,3,4,5 offline';
	sql 'alter database open';
	set newname for datafile 11 to '/u01/app/oracle/Disk1/users01.dbf';
	set newname for datafile 3 to '/u01/app/oracle/Disk1/rman.dbf';
	restore datafile 11,3,4;
	switch datafile all;
	recover datafile 11,3,4;
	sql 'alter database datafile 11 online';
	}

场景5:
基于时间点的不完全恢复:
drop user scott cascade;
1.恢复scott用户以及用户下所有数据
2.找出谁的误操作

	RMAN> run{
	sql 'startup force mount';
	sql 'alter session set nls_Date_format="yyyy-mm-dd hh24:mi:ss"';
	restore database;
	recover database until time '2010-08-20 14:40:33';
	sql 'alter database open resetlogs';
	}
阅读更多
个人分类: oracle学习笔记
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭