逻辑备份:
备份可分为两类 ,物理备份和逻辑备份
物理备份:该方法实现数据库的完整恢复,但需要极大的外部存储设备,例如磁带库,具体包括冷备份和热备份。冷备份和热备份(热备份要求数据库运行在归档模式下)都是物理备份,它涉及到组成数据库的文件,但不考虑逻辑内容。
逻辑备份: 使用软件技术从数据库中导出数据并写入一个输出文件,该文件的格式一般与原数据库的文件格式不同,只是原数据库中数据内容的一个映像。因此,逻辑备份文件只能用来对数据库进行逻辑恢复,即数据导入,而不能按数据库原来的存储特征进行物理恢复。逻辑备份一般用于增量备份,即备份那些在上次备份以后改变的数据。
在进行逻辑备份时,首先要确认数据库的字符集和操作系统的字符集是否一致,如果不一致则会报错ora-00091,具体设置方法如下:
SQL> select * from v$nls_parameters where parameter in ('NLS_LANGUAGE','NLS_TERRITORY','NLS_CHARACTERSET');
PARAMETER VALUE
-------------------- --------------------
NLS_LANGUAGE AMERICAN
NLS_TERRITORY AMERICA
NLS_CHARACTERSET WE8ISO8859P1
$ export NLS_LANG=AMERICAN_AMERICA.WE8ISO8859P1
$ echo $NLS_LANG
AMERICAN_AMERICA.WE8ISO8859P1
可以通过exp help=y或者imp help=y查看exp或imp的详细参数,下面以exp为例解释参数意义
1048576 tables=T
owner=(hr,scott)使用进行转义
SQL> create table t as select * from dba_objects;
Table created.
SQL> select segment_name,bytes/1024/1024 MB,blocks,extents,initial_extent from user_segments where segment_name='T';
SEGMENT_ MB BLOCKS EXTENTS INITIAL_EXTENT
-------- ---------- ---------- ---------- --------------
T 6 768 21 65536
采用delete的方式删除表中数据,不降低HWM
SQL> delete from t;
50625 rows deleted.
SQL> commit;
Commit complete.
SQL> select segment_name,bytes/1024/1024 MB,blocks,extents,initial_extent from user_segments where segment_name='T';
SEGMENT_ MB BLOCKS EXTENTS INITIAL_EXTENT
-------- ---------- ---------- ---------- --------------
T 6 768 21 65536
将compress设为n和y分别对t表进行两次导出
exp hr/hr tables=t file=t_n.dmp compress=n
exp hr/hr tables=t file=t_y.dmp compress=y
删除t表
SQL> drop table t purge;
Table dropped.
导入compress值为y的dmp文件
imp hr/hr file=t_y.dmp fromuser=hr touser=hr;
查询t表初始extent大小
SQL> select segment_name,bytes/1024/1024 MB,blocks,extents,initial_extent from user_segments where segment_name='T'
SEGMENT_ MB BLOCKS EXTENTS INITIAL_EXTENT
-------- ---------- ---------- ---------- --------------
T 6 768 6 6291456
查看建表语句
SQL> select dbms_metadata.get_ddl('TABLE','T','HR') FROM DUAL;
CREATE TABLE "HR"."T"
( "OWNER" VARCHAR2(30),
"OBJECT_NAME" VARCHAR2(128),
"SUBOBJECT_NAME" VARCHAR2(30),
"OBJECT_ID" NUMBER,
"DATA_OBJECT_ID" NUMBER,
"OBJECT_TYPE" VARCHAR2(19),
"CREATED" DATE,
"LAST_DDL_TIME" DATE,
"TIMESTAMP" VARCHAR2(19),
"STATUS" VARCHAR2(7),
"TEMPORARY" VARCHAR2(1),
"GENERATED" VARCHAR2(1),
"SECONDARY" VARCHAR2(1)
) PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255 NOCOMPRESS LOGGING
STORAGE(INITIAL 6291456 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT)
TABLESPACE "USERS"
再次删除t表
SQL> drop table t purge;
Table dropped.
导入compress值为n的dmp文件
imp hr/hr file=t_n.dmp fromuser=hr touser=hr;
查询t表初始extent大小
SQL>select segment_name,bytes/1024/1024 MB,blocks,extents,initial_extent from user_segments where segment_name='T'
SEGMENT_ MB BLOCKS EXTENTS INITIAL_EXTENT
-------- ---------- ---------- ---------- --------------
T .0625 8 1 65536
查询建表语句
SQL>select dbms_metadata.get_ddl('TABLE','T','HR') FROM DUAL;
CREATE TABLE "HR"."T"
( "OWNER" VARCHAR2(30),
"OBJECT_NAME" VARCHAR2(128),
"SUBOBJECT_NAME" VARCHAR2(30),
"OBJECT_ID" NUMBER,
"DATA_OBJECT_ID" NUMBER,
"OBJECT_TYPE" VARCHAR2(19),
"CREATED" DATE,
"LAST_DDL_TIME" DATE,
"TIMESTAMP" VARCHAR2(19),
"STATUS" VARCHAR2(7),
"TEMPORARY" VARCHAR2(1),
"GENERATED" VARCHAR2(1),
"SECONDARY" VARCHAR2(1)
) PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255 NOCOMPRESS LOGGING
STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT)
TABLESPACE "USERS"