查看实例编号,名称和日志模式:
SYS AS SYSDBA@ORCL>select dbid,name,log_mode from v$database;
DBID NAME LOG_MODE
---------- --------- ------------
1391294860 ORCL ARCHIVELOG
1 row selected.
RMAN:
登录:
[oracle@oracle ~]$ rman
RMAN> CONNECT TARGET / //连接本地数据库
connected to target database: ORCL (DBID=1391294860)
在ARCHIVELOG 模式下备份数据库, 将备份整个数据库及其归档重做日志文件到默认的备份设备
BACKUP DATABASE PLUS ARCHIVELOG;
在NO ARCHIVELOG 模式下备份数据库:只能进行一致性备份,要进行一致性备份,首先要关闭数据库,然后再加载并不打开数据库
1.启动RMAN并连接要备份的数据库,再执行下面的命令
[oracle@oracle ~]$ rman
RMAN>CONNNECT TARGET /
RMAN>SHUTDOWN IMMEDIATE;
RMAN>STARTUP FORCE DBA;
RMAN>SHUTDOWN IMMEDIATE;
RMAN>STARTUP MOUNT;
RMAN>BACKUP DATABASE; //直接备份数据库
RMAN>BACKUP AS COPY DATABASE; //备份数据库时创建所有数据文件的映像副本
备份完成后,执行下面的语句打开数据库
ALTER DATABASE OPEN;
执行增量备份:
使用BACKUP INCREMENTAL创建数据库的增量备份
备份级别:
0级:0级备份与完整备份是一致的
1级:只包含之前增量备份 后发生变化的数据块,如果在执行1级备份进,没有进行0级备份,ORACLE会自动执行0级备份
累积增量备份(CUMULATIVE INCREMENTAL BACKUP):包括从0级备份以来的所有变化的数据块
差异增量备份(DEFFERENTIAL INCREMENTAL BACKUP):只包括从最近一次差异备份以来变化的数据块
使用RMAN执行0级增量备份
RMAN> BACKUP INCREMENTAL LEVEL 0 DATABASE;
使用RMAN执行1级累积增量备份
RMAN> BACKUP INCREMENTAL LEVEL 1 CUMULATIVE DATABASE;
使用RMAN执行1级差异增量备份
RMAN> BACKUP INCREMENTAL LEVEL 1 DATABASE;
验证数据库文件和备份
检查所有数据库文件和归档重做日志文件是否存在物理和逻辑损坏
BACKUP VALIDATE CHECK LOGICAL DATABASE ARCHIVELOG ALL;
CHECK LOGICAL:表示检查是否存在逻辑块损坏
VALIDATE:可以对指定的块进行检查
检查4号数据文件的10-13个数据块:
RMAN> VALIDATE DATAFILE 4 BLOCK 10 TO 13;
在RMAN中显示备份信息:
显示当前数据库中的备份集:
LIST BACKUP OF DATABASE BY BACKUP
显示文件中的备份信息:
LIST BACKUP BY FILE;
显示备份的综合信息
LIST BACKUP SUMMARY;
显示数据文件的副本信息:
LIST COPY OF DATAFILE 1;
备份表空间和各种数据库文件:
备份表空间:
BACKUP TABLESPACE UNDOTBS1; //备份表空间时,ORACLE会自动将其转换为备份表空间中的数据文件
可以指定要备份的数据文件:
BACKUP DATAFILE 00001; //00001为数据文件的备份编号
可以对数据库处理加载和打开状态时对控制文件进行备份
在备份表空间时,可以使用INCLUDE CURRENT CONTROLFILE备份控制文件
BACKUP TABLESPACE USERS INCLUDE CURRENT CONTROLFILE;
备份控制文件:
BACKUP AS COPY CURRENT CONTROLFILE;
一个完整的使用RMAN命令备份数据库的实例:
1.将数据切换为归档日志模式:
查看日志归档模式:
SYS AS SYSDBA@ORCL>SELECT DBID,NAME,LOG_MODE FROM V$DATABASE;
DBID NAME LOG_MODE
---------- --------- ------------
1391294860 ORCL ARCHIVELOG
1 row selected.
2.如果为NOARCHIVELOG模式,则用下面的方法设置为ARCHIVELOG模式:
A.SHUTDOWN IMMEDIATE
B.STARTUP MOUNT
C.ALTER DATABASE ARCHIVELOG.
D.ALTER DATABASE OPEN
3.创建保存RMAN数据的表空间
CREATE TABLESPACE RMAN_TS DATAFILE ‘/u01/app/oracle/oradata/ORCL/rman_ts.dbf‘ SIZE 200M;
4.创建RMAN用户并授权
SYS AS SYSDBA@ORCL>CREATE USER RMAN IDENTIFIED BY RMAN DEFAULT TABLESPACE RMAN_TS TEMPORARY TABLESPACE TEMP;
User created.
Elapsed: 00:00:00.45
SYS AS SYSDBA@ORCL>GRANT CONNECT,RECOVERY_CATALOG_OWNER,RESOURCE TO RMAN;
Grant succeeded.
CONNECT:可以连接数据库,创建表,视图等数据库对象
RECOVERY_CATALOG_OWNER:可以对恢复目录进行管理
RESOURCE:可以创建表,视图等数据库对象
5.创建恢复目录
[oracle@oracle ~]$ rman catalog RMAN/RMAN TARGET ORCL
Recovery Manager: Release 11.2.0.1.0 - Production on Thu Dec 18 09:34:19 2014
Copyright (c) 1982, 2009, Oracle and/or its affiliates. All rights reserved.
target database Password:
RMAN> CREATE CATALOG TABLESPACE RMAN_TS;
recovery catalog created
6注册目标数据库
只有注册的数据库才能进行备份和恢复
RMAN> REGISTER DATABASE;
database registered in recovery catalog
starting full resync of recovery catalog
full resync complete
进行全数据库备份:
RMAN> run {
2> allocate channel dev1 type disk;
3> backup database;
4> release channel dev1;
5> }
单独备份归档日志文件:
RMAN> run {
2> allocate channel dev1 type disk;
3> backup archivelog all;
4> release channel dev1;
5> }
恢复整个数据库:
RESTORE(还原):按照RMAN存储库中的条目从备份文件中恢复需要的文件
RECOVER(恢复):使用数据文件和重做日志文件进行完全的或者时间点的数据库媒体恢复
1.关闭数据库,以MOUNT方式启动数据库
SYS AS SYSDBA@ORCL>shutdown immediate
Database closed.
Database dismounted.
ORACLE instance shut down.
SYS AS SYSDBA@ORCL>STARTUP FORCE MOUNT
ORACLE instance started.
Total System Global Area 422670336 bytes
Fixed Size 1336960 bytes
Variable Size 352323968 bytes
Database Buffers 62914560 bytes
Redo Buffers 6094848 bytes
Database mounted.
[oracle@oracle ~]$ rman
Recovery Manager: Release 11.2.0.1.0 - Production on Thu Dec 18 09:50:39 2014
Copyright (c) 1982, 2009, Oracle and/or its affiliates. All rights reserved.
RMAN> connect target /
connected to target database: ORCL (DBID=1391294860, not open)
RMAN> RESTORE DATABASE;
RMAN> RECOVER DATABASE;
[oracle@oracle ~]$ sqlplus /nolog
@>conn /as sysdba
SYS AS SYSDBA@ORCL>alter database open
恢复表空间:
数据库要在打开的情况下恢复,需要将需恢复的表空间设置为脱机状态
[oracle@oracle ~]$ rman
RMAN> connect target /
RMAN> SQL "ALTER TABLESPACE USERS OFFLINE IMMEDIATE";
RMAN> RESTORE TABLESPACE USERS;
RMAN> RECOVER TABLESPACE USERS;
RMAN> SQL "ALTER TABLESPACE USERS ONLINE";
恢复单个数据块:
在RMAN中可以恢复单个数据块
1.恢复所有被破坏的数据块
RMAN> RECOVER CORRUPTION LIST;
2.恢复指定的被破坏的数据块,可以在跟踪文件和警告日志中找到被破坏的数据块
查看跟踪文件和警告日志的位置
SELECT NAME,VALUE FROM V$DIAG_INFO;
恢复数据块时可以指定数据文件编号和数据块编号:
RMAN> RECOVER DATAFILE 1 BLOCK 233,235 DATAFILE 2 BLOCK 100 TO 200;
恢复归档日志:
RMAN> run {
2> allocate channel dev1 type disk;
3> restore archivelog all;
4> release channel dev1;
5> }
在Enterprise Manager中执行完整数据库备份
SYS AS SYSDBA@ORCL>GRANT SYSDBA TO RMAN;
Grant succeeded.
Elapsed: 00:00:00.08
SYS AS SYSDBA@ORCL>EXECUTE MGMT_USER.MAKE_EM_USER(‘RMAN‘);
PL/SQL procedure successfully completed.
使用闪回数据库:
可以快速地将ORACLE数据库倒退以前的某个时间
查看快速恢复区的初始化参数值:
SYS AS SYSDBA@ORCL>SHOW PARAMETER DB_RECOVERY_FILE_DEST;
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
db_recovery_file_dest string /u01/app/oracle/flash_recovery
_area
db_recovery_file_dest_size big integer 10G
1.SYS AS SYSDBA@ORCL>shutdown immediate
SYS AS SYSDBA@ORCL>startup mount
SYS AS SYSDBA@ORCL>ALTER DATABASE FLASHBACK ON
SYS AS SYSDBA@ORCL>ALTER DATABASE OPEN;
SYS AS SYSDBA@ORCL>select name,log_mode from v$database;
NAME LOG_MODE
--------- ------------
ORCL ARCHIVELOG
2.创建测试表:
SYS AS SYSDBA@ORCL>CREATE TABLE HR.MYDEP AS SELECT * FROM HR.DEPARTMENTS;
14:08:57 SYS AS SYSDBA@ORCL>DROP TABLE HR.MYDEP;
14:42:34 SYS AS SYSDBA@ORCL>DESC HR.MYDEP;
ERROR:
ORA-04043: object HR.MYDEP does not exist
3.14:09:40 SYS AS SYSDBA@ORCL>SHUTDOWN IMMEDIATE
14:10:00 SYS AS SYSDBA@ORCL>STARTUP MOUNT
设置当前系统日期的显示格式:
14:11:55 SYS AS SYSDBA@ORCL>ALTER SESSION SET NLS_DATE_FORMAT=‘YYYY-MM-DD HH24:MI:SS‘;
查看V$FLASHBACK_DATABASE_LOG中闪回数据库日志信息
14:16:14 SYS AS SYSDBA@ORCL>SELECT * FROM V$FLASHBACK_DATABASE_LOG;
OLDEST_FLASHBACK_SCN OLDEST_FLASHBACK_TI RETENTION_TARGET FLASHBACK_SIZE ESTIMATED_FLASHBACK_SIZE
-------------------- ------------------- ---------------- -------------- ------------------------
1759563 2014-12-18 14:04:51 1440 8192000 0
1 row selected.
14:16:52 SYS AS SYSDBA@ORCL>FLASHBACK DATABASE TO TIMESTAMP(TO_DATE(‘2014-12-18 14:05:00‘,‘YYYY-MM-DD HH24:MI:SS‘));
4.打开数据库查看表是否存在:
ALTER DATABASE OPEN RESETLOGS|NORESETLOGS
DESC HR.MYDEP;
闪回表:
可以将指定表中的数据,索引,触发器等恢复到指定的时间点
FLASHBACK TABLE<表名>
TO[BEFORE DROP [RENAME TO <表别名>]] | [SCN|TIMESTAMP] <表达式>
[Enable|disable triggers]
1.插入数据:
15:04:22 SYS AS SYSDBA@ORCL>INSERT INTO HR.MY VALUES(1,‘RSC‘,1,1);
2.查询数据:
15:04:47 SYS AS SYSDBA@ORCL>SELECT * FROM HR.MY;
1 RSC 1 1
3.闪回表:插入数据是15:04:22,如果想还原以前的数据,也就是说删除插入的数据,将闪回的时间调到插入数据以前的时间就可以了
FLASHBACK TABLE HR.MY TO TIMESTAMP(TO_DATE(‘2014-12-18 15:04:00‘,‘YYYY-MM-DD HH24:MI:SS‘));
4.查看数据,已没有了刚才插入的数据
闪回回收站:
当将表或数据库删除后,系统并没有完全删除,而是放在回收站中存放
可能使用FLASHBACK将数据对象恢复到相应的表空间中
FLASHBACK TABLE <表名> TO BEFORE DROP;
操作如下:
1.删除表HR.MYDEP:
15:20:08 SYS AS SYSDBA@ORCL>DROP TABLE HR.MYDEP;
2.在回收站中查看已删除的表:
15:21:22 SYS AS SYSDBA@ORCL>SELECT OBJECT_NAME,ORIGINAL_NAME,CREATETIME,DROPTIME FROM DBA_RECYCLEBIN;
OBJECT_NAME ORIGINAL_NAME CREATETIME DROPTIME
BIN$Cnls7xzrL7HgUKjAhSgy7g==$0 MYDEP 2014-12-18:14:07:50 2014-12-18:15:20:23
3.闪回回收站中指定的表:
15:28:05 SYS AS SYSDBA@ORCL>FLASHBACK TABLE HR.MYDEP TO BEFORE DROP;
4.查看已恢复的表对象:
5:28:23 SYS AS SYSDBA@ORCL>DESC HR.MYDEP;
Name Null? Type
----------------------------------------------------------------- -------- --------------------------------------------
DEPARTMENT_ID NUMBER(4)
DEPARTMENT_NAME NOT NULL VARCHAR2(30)
MANAGER_ID NUMBER(6)
LOCATION_ID NUMBER(4)
可以用PURGE TABLE语句删除回收站中的数据:
PURGE TABLE HR.MYDEP
使用PURGE DBA_RECYCLEBIN可以清空整个回收站:
普通用户可以使用:PURGE RECYCLEBIN命令清空整个回收站
闪回查询(Flashback Query)
可以找到发生误操作前的数据情况,为恢复数据提供依据
语句:闪回查询就是在传统的SELECT语句中加入AS OF TIMESTAMP
SELECT * FROM HR.RYX AS OF TIMESTAMP TO_TIMESTAMP(‘2014-12-18 15:42:00‘,‘YYYY-MM-DD HH24:MI:SS‘);
闪回查询的条件:
1.必须将初始化参数UNDO_MANAGEMENT设置为AUTO
2.初始化参数UNDO_RETENTION决定了能往前闪回查询的最大时间,值越大可以往前闪回查询的最大时间越长,但占用的磁盘空间也越
大
测试:
1.创建一个表
15:40:18 SYS AS SYSDBA@ORCL>CREATE TABLE HR.RYX (ID NUMBER,NAME VARCHAR2(20));
2.插入数据:
15:41:20 SYS AS SYSDBA@ORCL>INSERT INTO HR.RYX VALUES(1,‘1‘);
3.删除已创建的表中指定的行
15:42:02 SYS AS SYSDBA@ORCL>DELETE FROM HR.RYX WHERE ID=1;
4.查看已删除的行;
SELECT * FROM HR.RYX AS OF TIMESTAMP TO_TIMESTAMP(‘‘2014-12-18 15:04:00‘,‘YYYY-MM-DD HH24:MI:SS‘2014-12-18 15:41:35‘,‘YYYY-MM-DD HH24:MI:SS‘);
闪回版本查询:
闪回版本查询只能查询提交(COMMIT)后的数据
方法:在SELECT 语句中使用VERSIONS BETWEEN子句
1.创建一个表:
15:46:46 SYS AS SYSDBA@ORCL>CREATE TABLE HR.TEST(SCORE NUMBER);
2.插入数据
15:56:31 SYS AS SYSDBA@ORCL>INSERT INTO HR.TEST VALUES(100);
COMMIT
3.更新数据:
15:56:54 SYS AS SYSDBA@ORCL>UPDATE HR.TEST SET SCORE=110 WHERE SCORE=100;
COMMIT
4.删除数据:
15:57:22 SYS AS SYSDBA@ORCL>DELETE FROM TEST WHERE SCORE>0;
COMMIT
5.闪回版本查询:
15:57:45 SYS AS SYSDBA@ORCL>SELECT VERSIONS_STARTTIME,VERSIONS_OPERATION,SCORE FROM HR.TEST VERSIONS BETWEEN TIMESTAMP MINVALUE AND MAXVALUE;
VERSIONS_STARTTIME V SCORE
--------------------------------------------------------------------------- - ----------
18-DEC-14 03.57.43 PM D 110
18-DEC-14 03.57.19 PM U 110
18-DEC-14 03.56.51 PM I 100
闪回事务查询:
对于已提交的事务,也可以通过闪回方式查询,闪回事务保存表FLASHBACK_TRANSACTION_QUERY中,可以通过查询此表了解曾经发生的事务
16:01:14 SYS AS SYSDBA@ORCL>SELECT TABLE_NAME,UNDO_SQL FROM FLASHBACK_TRANSACTION_QUERY WHERE ROWNUM<3;
原文:http://rscpass.blog.51cto.com/771159/1612748