Oracle11g的三种备份方法

一、文件查看

数据文件、控制文件、日志文件查看如下:

select file#, status, enabled, name from V$datafile;--查看数据文件

select * from v$controlfile;--控制文件

select * from v$logfile;--日志文件

二、三种备份方法

1、导入/导出(import/export)

1)支持类型: table, user, tablespace, full database

2)例子

  • 导出

如果出现如下错误,应为用户去授予权限。

EXP-00023: must be a DBA to do Full Database or Tablespace export

授予权限(以管理员登录)
SYS@orcl> create user exp identified by oracle;

User created.

SYS@orcl> grant exp_full_database, imp_full_database to exp;

Grant succeeded.

SYS@orcl> conn exp/oracle
Connected.
EXP@orcl> select * from user_role_privs;

USERNAME		       GRANTED_ROLE		      ADM DEF OS_
------------------------------ ------------------------------ --- --- ---
EXP			       EXP_FULL_DATABASE	      NO  YES NO
EXP			       IMP_FULL_DATABASE	      NO  YES NO
将数据库orcl完全导出(全库备份)
[oracle@ocp tmp]$ exp exp/oracle@orcl file=/tmp/export.dmp full=y;
将exp和sys的表导出
[oracle@ocp tmp]$ exp exp/oracle@orcl file=/tmp/export_sys_exp_table.dmp owner=exp,sys
将scott.emp表导出
[oracle@ocp tmp]$ exp scott/oracle@orcl file=/tmp/export_scott_emp.dmp tables=emp;

Export: Release 11.2.0.4.0 - Production on Mon Feb 25 19:41:20 2019

Copyright (c) 1982, 2011, Oracle and/or its affiliates.  All rights reserved.

Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
Export done in ZHS16GBK character set and AL16UTF16 NCHAR character set

About to export specified tables via Conventional Path ...
. . exporting table                            EMP         14 rows exported
Export terminated successfully without warnings.
  • 导入 

删掉emp表,然后导入emp表,检查是否成功

SYS@orcl> drop table scott.emp;

Table dropped.

SYS@orcl> select * from scott.emp;
select * from scott.emp
                    *
ERROR at line 1:
ORA-00942: table or view does not exist
[oracle@ocp tmp]$ imp scott/oracle@orcl file=/tmp/export_scott_emp.dmp tables=emp;

Import: Release 11.2.0.4.0 - Production on Mon Feb 25 19:43:51 2019

Copyright (c) 1982, 2011, Oracle and/or its affiliates.  All rights reserved.


Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

Export file created by EXPORT:V11.02.00 via conventional path
import done in ZHS16GBK character set and AL16UTF16 NCHAR character set
. importing SCOTT's objects into SCOTT
. importing SCOTT's objects into SCOTT
. . importing table                          "EMP"         14 rows imported
About to enable constraints...
Import terminated successfully without warnings.
SYS@orcl> select * from scott.emp;

     EMPNO ENAME      JOB	       MGR HIREDATE	       SAL	 COMM
---------- ---------- --------- ---------- ------------ ---------- ----------
    DEPTNO
----------
      7369 SMITH      CLERK	      7902 17-DEC-80	       800
……

导入全库

[oracle@ocp tmp]$ imp exp/oracle@orcl file=/tmp/export_sys_exp_table.dmp full=y

导入exp和sys的表

[oracle@ocp tmp]$ exp exp/oracle@orcl file=/tmp/export_sys_exp_table.dmp owner=exp,sys

 2、冷备份(非归档模式)

       冷备份数据库是将数据库关闭之后备份所有的关键性文件包括数据文件、控制文件、联机REDO LOG文件,将其拷贝到另外的位置。此外冷备份也可以包含对参数文件和口令文件的备份,但是这两种备份是可以根据需要进行选择的。冷备份实际也是一种物理备份,是一个备份数据库物理文件的过程。

1)优点:

      <1>只需拷贝文件即可,是非常快速的备份方法。

      <2>只需将文件再拷贝回去,就可以恢复到某一时间点上。

     <3>与数据库归档的模式相结合可以使数据库很好地恢复。

       <4>维护量较少,但安全性确相对较高。

2)缺点:

       <1>在进行数据库冷备份的过程中数据库必须处于关闭状态。

      <2>单独使用冷备份时,数据库只能完成基于某一时间点上的恢复。

       <3>若磁盘空间有限,冷备份只能将备份数据拷贝到磁带等其他外部存储上,速度会更慢。

       <4>冷备份不能按表或按用户恢复。

 

3)具体备份步骤如下:

(步骤:shutdown数据库---> copy文件---> start数据库)

DBA用户或特权用户登录,查询动态性能视图v$datafilev$controlfile可以分别列出数据库的数据文件以及控制文件。

关闭数据库
SYS@orcl> shutdown immediate;
拷贝文件(日志文件、控制文件、数据文件)
[oracle@ocp orcl]$ cp *.log /tmp/leng
[oracle@ocp orcl]$ cp *.ctl /tmp/leng
[oracle@ocp orcl]$ cp *.dbf /tmp/leng
开启数据库
SYS@orcl> startup; 

3、热备份(归档模式)

       热备份是在数据库运行的情况下,采用archive log mode方式备份数据库的方法。。所以,假如你有昨天夜里的一个冷备份而且又有今天的热备份文件,在发生问题时,就可以利用这些资料恢复更多的信息。热备份要求数据库在archivelog方式下操作,并需要大量的档案空间。一旦数据库运行在archivelog状态下,就可以做备份了。热备份的命令文件由三部分组成:

1)数据文件一个表空间一个表空间的备份。 

前期准备

设置为归档模式(数据库默认非归档模式)
SYS@orcl> alter database archivelog;
查看是否为归档模式
SYS@orcl> archive log list;
Database log mode	       Archive Mode
Automatic archival	       Enabled
Archive destination	       USE_DB_RECOVERY_FILE_DEST
Oldest online log sequence     4
Next log sequence to archive   6
Current log sequence	       6
查看数据文件的位置
SYS@orcl> select file_name from dba_data_files where tablespace_name='USERS';

FILE_NAME
--------------------------------------------------------------------------------
/u01/app/oracle/oradata/orcl/users01.dbf

       (1) 设置表空间为备份状态  

SYS@orcl> alter tablespace users begin backup;

Tablespace altered.

       (2) 备份表空间的数据文件 

SYS@orcl> ho cp /u01/app/oracle/oradata/orcl/users01.dbf /tmp/re

       (3) 恢复表空间为正常状态

SYS@orcl> alter tablespace users end backup;

Tablespace altered.

2)备份归档log文件    

       (1) 临时停止归档进程  

SYS@orcl> alter tablespace users offline;

Tablespace altered.

查看表空间的状态
SYS@orcl> select tablespace_name,online_status from dba_data_files;

TABLESPACE_NAME 	       ONLINE_
------------------------------ -------
USERS			       OFFLINE
UNDOTBS1		       ONLINE
SYSAUX			       ONLINE
SYSTEM			       SYSTEM
EXAMPLE 		       ONLINE

       (2) log下那些在archive rede log目标目录中的文件 

SYS@orcl> select name from v$archived_log;
NAME
--------------------------------------------------------------------------------
/u01/app/oracle/orcl_far/ORCL/archivelog/2019_02_17/o1_mf_1_16_g6jxqfmf_.arc
/u01/app/oracle/orcl_far/ORCL/archivelog/2019_02_17/o1_mf_1_12_g6jxpqns_.arc
/u01/app/oracle/orcl_far/ORCL/archivelog/2019_02_17/o1_mf_1_17_g6jy4of3_.arc
/u01/app/oracle/orcl_far/ORCL/archivelog/2019_02_21/o1_mf_1_18_g6vg50qj_.arc
/u01/app/oracle/orcl_far/ORCL/archivelog/2019_02_21/o1_mf_1_19_g6vn2tvh_.arc
/u01/app/oracle/orcl_far/ORCL/archivelog/2019_02_21/o1_mf_1_22_g6votpnf_.arc
/u01/app/oracle/orcl_far/ORCL/archivelog/2019_02_21/o1_mf_1_25_g6vozdsw_.arc
/u01/app/oracle/orcl_far/ORCL/archivelog/2019_02_21/o1_mf_1_28_g6vpt1g5_.arc
/u01/app/oracle/orcl_far/ORCL/archivelog/2019_02_21/o1_mf_1_26_g6vp69m4_.arc
/u01/app/oracle/orcl_far/ORCL/archivelog/2019_02_21/o1_mf_1_23_g6voz0rj_.arc
/u01/app/oracle/orcl_far/ORCL/archivelog/2019_02_21/o1_mf_1_21_g6votlt6_.arc

NAME
--------------------------------------------------------------------------------
/u01/app/oracle/orcl_far/ORCL/archivelog/2019_02_21/o1_mf_1_24_g6voz6rk_.arc
/u01/app/oracle/orcl_far/ORCL/archivelog/2019_02_21/o1_mf_1_29_g6vpw3k8_.arc
/u01/app/oracle/orcl_far/ORCL/archivelog/2019_02_21/o1_mf_1_20_g6vosr3g_.arc
/u01/app/oracle/oradata/orcl/redo01.log
/u01/app/oracle/oradata/orcl/redo11.log
/u01/app/oracle/oradata/orcl/redo02.log
/u01/app/oracle/oradata/orcl/redo22.log
/u01/app/oracle/oradata/orcl/redo03.log
/u01/app/oracle/oradata/orcl/redo33.log
/u01/app/oracle/orcl_far/ORCL/archivelog/2019_02_21/o1_mf_1_28_g6vy110v_.arc
/u01/app/oracle/orcl_far/ORCL/archivelog/2019_02_21/o1_mf_1_29_g6vy1156_.arc

NAME
--------------------------------------------------------------------------------
/u01/app/oracle/orcl_far/ORCL/archivelog/2019_02_21/o1_mf_1_30_g6vy119c_.arc
/u01/app/oracle/orcl_far/ORCL/archivelog/2019_02_24/o1_mf_1_2_g75dfbol_.arc
/u01/app/oracle/orcl_far/ORCL/archivelog/2019_02_24/o1_mf_1_1_g75dfbos_.arc
/u01/app/oracle/orcl_far/ORCL/archivelog/2019_02_25/o1_mf_1_3_g76zrs0j_.arc
/u01/app/oracle/orcl_far/ORCL/archivelog/2019_03_04/o1_mf_1_4_g7svhv8j_.arc
/u01/app/oracle/orcl_far/ORCL/archivelog/2019_03_04/o1_mf_1_5_g7sx7g17_.arc

28 rows selected.

       (3) 重新启动archive进程  

SYS@orcl> alter database archivelog;

       (4) 备份归档的redo log文件

SYS@orcl> ho cp /u01/app/oracle/oradata/orcl/*.log /tmp

SYS@orcl> alter tablespace users online;

Tablespace altered.

SYS@orcl> select tablespace_name,online_status from dba_data_files;

TABLESPACE_NAME 	       ONLINE_
------------------------------ -------
USERS			       ONLINE
UNDOTBS1		       ONLINE
SYSAUX			       ONLINE
SYSTEM			       SYSTEM
EXAMPLE 		       ONLINE
切换日志,使用当前日志归档
SYS@orcl> alter system switch logfile;

3)用alter database bachup controlfile命令来备份控制文件热备份

SYS@orcl> alter database backup controlfile to '/tmp/controlfile';
	
Database altered.

优点是:   

       (1)可在表空间或数据库文件级备份,备份的时间短。 

        (2)备份时数据库仍可使用。  

        (3)可达到秒级恢复(恢复到某一时间点上)。  

        (4)可对几乎所有数据库实体做恢复。

        (5)恢复是快速的,在大多数情况下爱数据库仍工作时恢复。    

4)热备份的不足是:    

        (1)不能出错,否则后果严重。

        (2)若热备份不成功,所得结果不可用于时间点的恢复。

       (3)因难于维护,所以要非凡仔细小心,不答应“以失败告终”。

 

log_archive_start=true --开启自动归档,否则只能手工归档,如果是关闭了归档,则设置该参数为false。10g之后废除该参数,不需要设置。
log_archive_dest = ---归档日志存放路径
log_archive_format ="CX%TS%S.ARC" ---归档日志文件名格式

alter system archive log current;        更改归档日志

 

 

 

 

 

 

 

   

 

 

 

 

 

 

你可能感兴趣的:(Oracle数据库)