备份数据库
数据库一致性备份:关闭数据库以后备份数据库物理文件,这时数据文件scn值完全一致,所以称之为数据库的一致性备份或冷备份,适用于归档模式与非归档模式。对基于用户管理的冷备份的暂时理解:在数据库shutdown状态下使用OS命令直接拷贝数据库物理文件。
数据库非一致性备份:在open状态下备份数据库物理文件,这时数据库内容可能会改变导致数据文件scn不一致,所以称之为数据库的非一致性备份或热备份,只适用于归档模式。
基于用户管理的完全恢复(归档模式下)
1.在open状态下备份数据库物理文件(数据库非一致性备份,热备份,不影响业务运行)
2.删除数据文件来模拟文件丢失(只是删除了所有的数据文件;控制文件等其它文件处于正常状态)
3.对数据文件进行恢复
4.应用归档日志
5.查看数据是否完全恢复
SQL> conn evan/evan
Connected.
SQL> insert into t_evan values('Oracle');
1 row created.
SQL> insert into t_evan values('java');
1 row created.
SQL> commit;
Commit complete.
SQL> select * from t_evan;
TEXT
--------------------------------------------------------------------------------
oracle
java
--sysdba执行备份
SQL> conn / as sysdba
Connected.
SQL> select name from v$datafile;
NAME
--------------------------------------------------------------------------------
/oracle/10g/oracle/product/10.2.0/oradata/oralife/system01.dbf
/oracle/10g/oracle/product/10.2.0/oradata/oralife/undotbs01.dbf
/oracle/10g/oracle/product/10.2.0/oradata/oralife/sysaux01.dbf
/oracle/10g/oracle/product/10.2.0/oradata/oralife/users01.dbf
/oracle/10g/oracle/product/10.2.0/oradata/oralife/example01.dbf
SQL> alter database begin backup;
Database altered.
SQL> host cp /oracle/10g/oracle/product/10.2.0/oradata/oralife/*.dbf /oracle/10g/oracle/bakup/database/ --备份所有数据文件
SQL> alter database backup controlfile to '/oracle/10g/oracle/bakup/database/oralife.ctl'; --备份控制文件
Database altered.
-----------------------------------------------------------------------------------------------------------------------------------------
忘记了alter database end backup;
-----------------------------------------------------------------------------------------------------------------------------------------
System altered.
使用rm删除所有数据文件(*.dbf);
SQL> conn evan/evan
Connected.
SQL> select * from t_evan;
TEXT
--------------------------------------------------------------------------------
oracle
java
SQL> insert into t_evan values('spring');
1 row created.
SQL> commit;
Commit complete.
SQL> ALTER SYSTEM CHECKPOINT; --将已修改的数据从高速缓存刷新到磁盘,并更新控制文件和数据文件
System altered.
SQL> ALTER SYSTEM SWITCH LOGFILE; --日志切换
System altered.
查看alter_oralife.log出现错误信息:某某文件不存在等等等...
进行完全恢复
copy备份的数据文件到指定的目标位置,进行恢复
SQL> recover database --open状态
ORA-00283: recovery session canceled due to errors
ORA-01124: cannot recover data file 1 - file is in use or recovery
ORA-01110: data file 1:
'/oracle/10g/oracle/product/10.2.0/oradata/oralife/system01.dbf'
SQL> shutdown immediate --open状态
ORA-01122: database file 1 failed verification check
ORA-01110: data file 1: '/oracle/10g/oracle/product/10.2.0/oradata/oralife/system01.dbf'
ORA-01208: data file is an old version - not accessing current version
SQL> startup force mount; --进入mount状态进行恢复
ORACLE instance started.
Total System Global Area 528482304 bytes
Fixed Size 1220360 bytes
Variable Size 163578104 bytes
Database Buffers 356515840 bytes
Redo Buffers 7168000 bytes
Database mounted.
SQL> recover database
Media recovery complete.
SQL> alter database open;
Database altered.
SQL> conn evan/evan --使用evan登录查看恢复情况
Connected.
SQL> select * from t_evan;
TEXT
--------------------------------------------------------------------------------
oracle
java
spring
没有出现应用归档日志,应用归档日志有几种方式
Specify log: {=suggested | filename | AUTO | CANCEL}
当在open状态下对数据文件进行恢复时,应该将其offline,恢复完成后,再将其online;所有的恢复应该尽量在open状态下完成。
要理解checkpoint, alter system archive log current;,ALTER SYSTEM SWITCH LOGFILE