RMAN命令
1、独立命令
RMAN>shutdown immediate
RMAN>startup
RMAN>backup format ‘d:\backup\%d_%s.bak‘ tablespace users;
2、作业命令
RMAN>run { backup format ‘d:\backup\$d_%s.bak‘ tablespace users; }
3、运行SQL命令
RMAN>sql ‘alter system switch logfile‘ ;
4、运行存储脚本
RMAN>run { execute script s1;}
5、运行操作系统命令
RMAN>run { host "dir d:\backup"; }
6、运行RMAN命令
c:\rman target sys/[email protected] nocatalog @c:\bak_users.cmd log a.log
bak_users.cmd示例:
backup format ‘d:\backup\%d_%s.bak tablespace users;
sql ‘alter system archive log current‘ ;
配置RMAN环境(略)
建立备份集
1、完全数据库备份集
1)、一致性备份(适用于NOARCHIVELOG和ARCHIVELOG模式)
c:\rman target sys/[email protected] nocatalog
RMAN>shutdown immediate
RMAN>startup mout
RMAN>backup database; 如果备份到别的目录backup database format =‘d:\backup\%d_%s.dbf‘ ;
RMAN>sql ‘alter system archive log current ‘ ;
2)、非一致性备份(在OPEN状态)
RMAN>backup database; 如果备份到别的目录backup database format =‘d:\backup\%d_%s.dbf‘ ;
RMAN>sql ‘alter system archive log current ‘ ;
3)、免除表空间
RMAN>configure exclude for tablespace user03;
RMAN>backup database; 如果备份到别的目录backup database format =‘d:\backup\%d_%s.dbf‘ ;
RMAN>sql ‘alter system archive log current ‘ ;
2、表空间备份集
RMAN>backup tablespace users format=‘d:\backup\%N_%f_%s.dbf‘ ;
3、数据文件备份集
RMAN>backup datafile 5 format=‘d:\backup\%N_%f_%s.dbf‘ ;
4、控制文件备份集
RMAN>backup current controlfile format=‘d:\backup\%d_%s.ctl‘ ;
5、SPFILE备份集
RMAN>backup spfile format=‘c:\%d_%s.par‘;
6、归档日志备份集(过去一天所生成的归档日志)
RMAN>backup format=‘c:\%d_%s.arc‘ archivelog from time=‘sysdate-1‘ until time=‘sysdate‘;
7、并行化备份集
RMAN>configure device type disk parallelism 3 ;
RMAN>backup database format=‘c:\%d_%s.dbf‘;
RMAN>configure device type disk clear;
8、建立多重备份
RMAN>backup copied 3 tablespace users format=‘c:\1\%N_%s.dbf‘,‘c:\2\%N_%s.dbf‘,‘c:\3\%N_%s.dbf‘ ;
9、备份备份集
RMAN>backup backupset 32 format=‘c:\%d_%s.bak‘;
10、建立多个备份片
RMAN>configure channel device type sbt maxpiecesize 4G;
RMAN>backup device type sbt format ‘%d_%s_%p.dbf‘ database;
11、建立压缩备份集
RMAN>backup as compressed backupset tablespace users format=‘c:\%d_%s.dbf‘;
12、备份数据文件和归档日志
RMAN>backup format ‘c:\%d_%s.bak‘ tablespace users plus archivelog ;
13、指定备份集标记
RMAN>backup format=‘c:\%d_%s.bak‘ tablespace users tag=‘users‘ ;
建立映像副本
1、建立数据文件映像副本
RMAN>backup as copy format=‘c:\df_5.dbf‘ datafile 5; 或者
RMAN>copy datafile 5 to ‘c:\df_5.dbf‘ ;
2、建立控制文件映像副本
RMAN>backup as copy format=‘c:\demo.ctl‘ current controlfile; 或者
RMAN>copy current controlfile to ‘c:\demo.ctl‘;
3、建立归档日志映像副本
RMAN>backup as copy format=‘c:\arc10.log‘ archivelog sequence 10; 或者
RMAN>copy archivelog ‘c:\demo\archive\arc00020_012345.001‘ to ‘c:\arc20.log‘;
4、并行化建立映像副本
RMAN>configure device type disk parallelism 3;
RMAN>backup as copy format =‘c:\df_%f.dbf‘ datafile 5,6,7 ;
RMAN>configure device type disk clear;
5、指定映像副本标记
RMAN>backup as copy format=‘c:\df_%f.dbf‘ datafile 5 tag=df_5; 或者
RMAN>copy datafile 5 to ‘c:\df_%f.dbf‘ tag=df_5;
建立增量备份和累积备份
增量备份可以减少备份时间,累积备份可以减少恢复时间。
增量备份示例:周日 LEVEL 0 ,周一、周二、周四、、周五、周六 LEVEL 2 ,周三 LEVEL 1
RMAN>backup incremental level 0 format=‘c:\backup\%d_%s.dbf‘ tablespace users; 周日
RMAN>backup incremental level 2 format=‘c:\backup\%d_%s.dbf‘ tablespace users; 周一
RMAN>backup incremental level 2 format=‘c:\backup\%d_%s.dbf‘ tablespace users; 周二
RMAN>backup incremental level 1 format=‘c:\backup\%d_%s.dbf‘ tablespace users; 周三
RMAN>backup incremental level 2 format=‘c:\backup\%d_%s.dbf‘ tablespace users; 周四
RMAN>backup incremental level 2 format=‘c:\backup\%d_%s.dbf‘ tablespace users; 周五
RMAN>backup incremental level 2 format=‘c:\backup\%d_%s.dbf‘ tablespace users; 周六
RMAN完全恢复
1、恢复数据库
1)、所有数据文件被删除
c:\rman target sys/[email protected] nocatalog
RMAN>startup force mount
RMAN> run {
restore databse ;
recover database;
sql ‘alter database open‘ ;
}
2)、数据文件所在磁盘出现硬件故障
RMAN> run {
startup force mount;
set newname for datafile 1 to ‘c:\demo\system01.dbf‘;
set newname for datafile 2 to ‘c:\demo\dundotbs01.dbf‘;
set newname for datafile 3 to ‘c:\demo\sysaux01.dbf‘;
set newname for datafile 4 to ‘c:\demo\users01.dbf‘;
set newname for datafile 5 to ‘c:\demo\example01.dbf‘;
set newname for datafile 6 to ‘c:\demo\test0.dbf‘;
restore database;
switch datafile all;
recover database;
sql ‘alter database open ‘;
}
2、恢复SYSTEM表空间的数据文件
1)、SYSTEM表空间的数据文件被误删除
RMAN>run {
startup force mount;
restore datafile 1;
recover datafile 1;
sql ‘alter database open‘ ;
}
2)、SYSTEM表空间数据文件所在的磁盘出现故障
RMAN> run {
startup force mount;
set newname for datafile 1 to ‘c:\demo\system01.dbf‘;
restore datafile 1;
switch datafile 1;
recover datafile 1;
sql ‘alter database open‘;
}
3、在OPEN状态下恢复关闭后意外丢失数据文件
1)、数据文件被删除
RMAN>run {
startup force mount;
sql ‘alter database datafile 4 offline‘;
sql ‘alter database open ‘;
restore datafile 4;
recover datafile 4;
sql ‘alter database datafile 4 online‘;
}
2)、数据文件所在的磁盘出现损坏
RMAN>run {
startup force mount;
sql ‘alter database datafile 4 offline‘;
sql ‘alter database open ‘;
set newname for datafile 4 to ‘c:\demo\user01.dbf‘;
restore datafile 4;
switch datafile 4;
recover datafile 4;
sql ‘alter database datafile 4 online‘;
}
4、在OPEN状态下恢复打开时意外丢失的数据文件
1)、数据文件被误删除
RMAN>run {
sql ‘alter database datafile 4 offline‘;
restore datafile 4;
recover datafile 4;
sql ‘alter database datafile 4 online‘;
}
2)、数据文件所在磁盘出现故障
RMAN>run {
sql ‘alter database datafile 4 offline‘;
set newname for datafile 4 to ‘c:\demo\user01.dbf‘;
restore datafile 4;
switch datafile 4 ;
recover datafile 4;
sql ‘alter database datafile 4 online‘;
}
5、在OPEN状态下恢复未备份的数据文件(创造新数据文件后没有进行过备份)
1)、数据文件被误
RMAN>run {
startup force mount;
sql ‘alter database datafile 7 offline ‘;
sql ‘alter database open‘;
restore datafile 7;
recover datafile 7;
sql ‘alter database datafile 7 online‘;
}
2)、数据文件所在磁盘出现故障
RMAN>run {
startup force mount;
sql ‘alter database datafile 7 offline ‘;
sql ‘alter database open‘;
set newname for datafile 7 to ‘c:\demo\user04.dbf‘;
restore datafile 7;
switch datafile 7;
recover datafile 7;
sql ‘alter database datafile 7 online‘;
}
6、恢复表空间
1)、表空间被删除
RMAN>run {
sql ‘alter tablespace users offline for recover‘;
restore tablespace users;
recover tablespace users;
sql ‘alter tablespace users online‘;
}
2)、表空间的数据文件所在磁盘出现故障
RMAN>run {
sql ‘alter tablespace users offline for recover‘;
set newname for datafile 4 to ‘c:\demo\user01.dbf‘;
restore tablespace users;
switch tablespace users;
recover tablespace users;
sql ‘alter tablespace users online‘;
}
7、数据块介质恢复
RMAN>blockrecover device type disk datafile 5 block 20,21,100 ;
RMAN不完全恢复
1、基本时间恢复
c:\set nls_date_format=yyyy-dd-mm hh24:mi:ss
c:\rman target sys/[email protected] nocatalog
RMAN>run {
startup force mount;
set until time=‘2010-08-22 12:00:08‘;
restore database;
recover database;
sql ‘alter database open resetlogs;
}
2、基于SCN恢复
RMAN>run {
startup force mount;
set until scn=123456;
restore database;
recover database;
sql ‘alter database open resetlogs‘;
}
3、基于日志序列号恢复
RMAN>run {
startup force mount;
set until seqence=10;
restore database;
recover database;
sql ‘alter database open resetlogs‘;
}
4、基于备份控制文件恢复
c:\set nls_date_format=yyyy-dd-mm hh24:mi:ss
c:\rman target sys/[email protected] nocatalog
RMAN>startup force nomount;
RMAN>set dbid=1113606269;
RMAN>restore controlfile from autobackup maxseq 6;
RMAN>alter database mount;
RMAN>run {
set until time=‘2010-08-22 12:00:08‘;
restore database;
recover database;
sql ‘alter database open resetlogs;
}
当执行了上述4种不完全恢复之后,建议删除早期的所有备份,并重新备份数据库
RMAN>run {
delete noprompt backup;
delete noprompt copy;
backup database format=‘c:\backup\%d_%s.bak‘;
sql ‘alter system archive log current‘;
}
RMAN维护命令
REPORT
1、报告目标数据库的物理结构
RMAN>report schema;
2、报告需要备份的数据文件
1)、报告需要转储的增量备份超出特定个数的数据文件
RMAN> report need backup incremental 3;
2)、报告特定天数未备份的数据文件
RMAN>report need backup days 3 tablespace users ;
3)、报告低于特定备份次数的数据文件
RMAN> report need backup redundancy 2 database ;
4)、报告在恢复时间窗口内未备份的数据文件
RMAN> report need backup recovery window of 3 days ;
3、报告陈旧备份
RMAN>report obsolete redundancy 2;
4、报告不可恢复的数据文件
RMAN>report unrecoverable;
LIST
1、列出数据文件备份集
RMAN>list backup of database ;
RMAN>list backup of tablespace users;
RMAN>list backup of datafile 4;
2、列出控制文件备份集
RMAN>list backup of controlfile ;
3、列出归档日志备份集
RMAN>list backup of archivelog all;
4、列出SPFILE备份集
RMAN>list backup of spfile ;
5、列出数据文件映像副本
RMAN>list copy of database;
RMAN>list copy of tablespace users;
RMAN>list copy of datafile 4;
6、列出控制文件映像副本
RMAN>list copy of controlfile;
7、列出归档日志映像副本
RMAN>list copy of archivelog all;
8、列出数据库副本
RMAN>list incarnation of database ;
注意,每次用RESETLOGS选项打开数据库之后都会生成一个新的数据库副本。
9、列出备份集文件的信息
RMAN>list backupset 100;
CROSSCHECK
1、核对所有备份集
RMAN>crosscheck backup;
2、核对所有数据文件的备份集
RMAN>crosscheck backup of database ;
3、核对特定表空间的备
RMAN>crosscheck backup of tablespace system;
4、核对特定数据文件的备份集
RMAN>crosscheck backup of datafile 1;
5、核对控制文件的备份集
RMAN>crosscheck backup of controlfile ;
6、核对SPFILE的备份集
RMAN> crosscheck backup of spfile;
7、核对归档日志的备份集
RMAN> crosscheck backup of archivelog sequence 40;
8、核对所有映像副本
RMAN> crosscheck copy ;
9、核对所有数据文件的映像副本
RMAN> crosscheck copy of database;
10、核对特定表空间的映像副本
RMAN> crosscheck copy of tablespace users;
11、核对特定数据文件的映像副本
RMAN> crosscheck copy of datafile 1;
12、核对控制文件的映像副本
RMAN> crosscheck copy of controlfile;
13、核对归档日志的映像副本
RMAN> crosscheck copy of archivelog sequence 44;
DELETE
1、删除陈旧备份
delete obsolete; #提示
delete noprompt obsolete; #不提示
2、删除EXPIRED备份集
delete noprompt expired backup;
3、删除EXPIRED副本
delete noprompt expired copy ;
4、删除特定备份集
delete noprompt backupset 19;
5、删除特定备份片
delete noprompt backuppiece ‘c:\test\demo1.bak‘;
6、删除所有备份集
delete noprompt backup ; 或者
delete noprompt backupset;
7、删除特定映像副本
delete noprompt datafilecopy ‘c:\test\demo.bak‘; 删除数据文件
delete noprompt controlfilecopy ‘c:\test\controlfile_bak.ctl‘; 删除控制文件
delete noprompt archivelog ‘c:\test\arc.bak‘; 删除归档日志
8、删除所有映像副本
delete noprompt copy;
9、在备份后删除输入对象
backup archivelog all delete input ;
backup backupset 22 format=‘c:\test\%u.bak‘ delete input ;
CHANGE
1、改变备份集状态
change backupset 22 unavailable;
2、改变映像副本状态
change datafilecopy ‘c:\demo.bak‘ unavailable ;
change controlfilecopy ‘c:\demo.bak‘ unavailable ;
change archivelog ‘c:\demo.bak‘ unavailable ;
3、建立长期备份
change backupset 100 keep forver logs; 使备份集100永久保留
change backupset 100 keep until time ‘sysdate+60‘ logs; 使备份集100保留60天
change backupset 156 nokeep ; 免除备份集100的保留期限
CATALOG
1、注册副本
catalog datafilecopy ‘c:\user01.bak‘;
2、注册备份片
catalog backuppiece ‘c:\demo_10.dbf‘ ;
3、注册特定目录的所有备份文件
catalog start with ‘c:\bak‘;
4、注销备份记录
change datafilecopy ‘c:\user01.bak‘ uncatalog;
change controlfilecopy ‘c:\demo.ctl‘ uncatalog;
change backuppiece ‘c:\demo_10.bak‘ uncatalog;
恢复目录管理
建立恢复目录
1、建立恢复目录表空间
SQL> create tablespace rman_ts
2 datafile ‘D:\oracle\product\10.1.0\db_1\train\rman.dbf‘
3 size 15m ;
表空间已创建。
2、建立恢复目录所有者
SQL> create user rman identified by rman
2 default tablespace rman_ts;
用户已创建。
3、授予恢复目录所有者角色
SQL> grant connect,resource,recovery_catalog_owner to rman;
授权成功。
4、建立恢复目录
c:\rman catalog rman/[email protected]
RMAN>create catalog;
5、注册目标数据库
RMAN>connect target sys/[email protected]
RMAN>register database;
RMAN>report schema;
使用存储脚本
1、建立存储脚本
C:\>rman catalog rman/[email protected] target sys/[email protected]
RMAN>create script bak_database {
backup format=‘c:\temp\%d_%s.bak‘
database include current controlfile
plus archivelog;
sql ‘alter system archive log current‘ ;
}
2、运行脚本
(1)、在RMAN提示符下运行存储脚本
RMAN>run { execute script bak_database; }
(2)、在命令行运行存储脚本
c:\rman catalog rman/[email protected] target sys/[email protected] script bak_database
3、显示存储脚本内容
RMAN>print script bak_database;
4、替换存储脚本
RMAN>replace script bak_database {
backup database format=‘c:\temp\%d_%s.bak‘;
}
5、删除存储脚本
RMAN>delete script bak_database;
6、列出所有存储脚本
RMAN>list script names;
维护恢复目录
1、重新同步恢复目录
RMAN> resync catalog;
2、注销目标数据库
RMAN>unregister database;
当注销了目标数据库之后,会丢失早于controlfile_record_keep_time设置时间的RMAN资料库记录。
3、升级恢复目录
C:\>rman catalog rman/[email protected] target sys/[email protected]
RMAN> upgrade catalog;
RMAN> upgrade catalog;
4、删除恢复目录
C:\>rman catalog rman/[email protected] target sys/[email protected]
RMAN> drop catalog ;
RMAN> drop catalog ;
5、恢复数据库到早期数据库副本