Oracle数据库逻辑备份与恢复(2)——传输表空间

Oracle数据库逻辑备份与恢复(2)——传输表空间

使用exp、imp导出和导入表空间数据时有两种操作:
(1)直接导出表空间中的数据,导入数据时把数据导入到一个已经存在的表空间;
(2)二是使用传输表空间,导出时只导出表空间信息,又称为元数据(metadata),导入时使用元数据文件和表空间对应的数据文件共同导入数据。该方法导入数据时表空间不存在,可以实现表空间在不同数据库之间移动。

一、不使用传输表空间导入导出数据

步骤如下:

1、导出某个表空间的数据

导出和导入表空间的数据不能由普通用户来操作,必须由系统管理员(system或sys用户)来操作。

(1)查看表空间data01所拥有的表

SQL> select table_name from dba_tables where tablespace_name='DATA01';

TABLE_NAME
------------------------------
E01
E02

(2)导出表空间data01的数据

[oracle@wgx oradata]$ exp system/system tablespaces=data01 file=/home/oracle/bak/ts_data01.dmp buffer=1048576 log=/home/oracle/bak/ts_data01.log

Export: Release 11.2.0.1.0 - Production on Tue Apr 7 17:12:02 2020

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

Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
Export done in US7ASCII character set and AL16UTF16 NCHAR character set
server uses AL32UTF8 character set (possible charset conversion)

About to export selected tablespaces ...
For tablespace DATA01 ...
. exporting cluster definitions
. exporting table definitions
. . exporting table                            E01     114688 rows exported
. . exporting table                            E02     114688 rows exported
. exporting referential integrity constraints
. exporting triggers
Export terminated successfully without warnings.

(3)查看导出的文件及大小

[oracle@wgx bak]$ ll ts_data01* -h
-rw-r--r-- 1 oracle oinstall 11M 4月   7 17:12 ts_data01.dmp
-rw-r--r-- 1 oracle oinstall 705 4月   7 17:12 ts_data01.log

2、导入数据(表空间存在)

(1)删除表空间data01中的表

SQL> drop table scott.e01 purge;
Table dropped.

SQL> drop table scott.e02 purge;
Table dropped.

SQL> select table_name from dba_tables where tablespace_name='DATA01';
no rows selected

(2)导入表空间data01中的数据

[oracle@wgx bak]$ imp system/system full=y file=/home/oracle/bak/ts_data01 buffer=1048576 log=/home/oracle/bak/imp_ts_data01

Import: Release 11.2.0.1.0 - Production on Tue Apr 7 17:21:05 2020

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

Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.1.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 US7ASCII character set and AL16UTF16 NCHAR character set
import server uses AL32UTF8 character set (possible charset conversion)
. importing SYSTEM's objects into SYSTEM
. importing SCOTT's objects into SCOTT
. . importing table                          "E01"     114688 rows imported
. . importing table                          "E02"     114688 rows imported
Import terminated successfully without warnings.

(3)查看表空间data01中的表

SQL> select table_name from dba_tables where tablespace_name='DATA01';

TABLE_NAME
------------------------------
E02
E01

3、导入数据(表空间不存在)

如果导入数据时data01表空间不存在,则导入到默认永久表空间。

(1)删除data01表空间

SQL> drop tablespace data01 including contents;
Tablespace dropped.

SQL> select tablespace_name from dba_tablespaces where tablespace_name='DATA01';
no rows selected

(2)导入表空间data01中的数据

[oracle@wgx bak]$ imp system/system full=y file=/home/oracle/bak/ts_data01 buffer=1048576 log=/home/oracle/bak/imp_ts_data01

Import: Release 11.2.0.1.0 - Production on Tue Apr 7 17:25:26 2020

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

Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.1.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 US7ASCII character set and AL16UTF16 NCHAR character set
import server uses AL32UTF8 character set (possible charset conversion)
. importing SYSTEM's objects into SYSTEM
. importing SCOTT's objects into SCOTT
. . importing table                          "E01"     114688 rows imported
. . importing table                          "E02"     114688 rows imported
Import terminated successfully without warnings.

(3)查看表e01和e02所在的表空间

SQL> select table_name,tablespace_name from dba_tables where table_name in ('E01','E02');

TABLE_NAME		       TABLESPACE_NAME
------------------------------ ------------------------------
E02			       USERS
E01			       USERS

--查看数据库默认的表空间
SQL> select property_name,property_value from database_properties where rownum<5;

PROPERTY_NAME		       PROPERTY_VALUE
------------------------------ ------------------------------
DICT.BASE		               2
DEFAULT_TEMP_TABLESPACE        TEMP
DEFAULT_PERMANENT_TABLESPACE   USERS
DEFAULT_EDITION 	           ORA$BASE

可以看出,data01表空间中的两张表被导入到users表空间中。因为users表空间是数据库默认的表空间。

二、使用传输表空间导入导出数据

如果使用传输表空间在不同的数据库之间导入导出数据,要求导出数据的数据库和导入数据的数据库软件版本必须相同,并且具有相同的字符集。详细步骤如下:

step1、设置要导出的表空间为只读

SQL> create tablespace data01 datafile '/usr/local/oradata/orcl/data01.dbf' size 50m autoextend on;

Tablespace created.

SQL> 
SQL> alter table scott.e01 move tablespace data01;
Table altered.

SQL> alter table scott.e02 move tablespace data01;
Table altered.

SQL> alter tablespace data01 read only;
Tablespace altered.

step2、导出表空间信息(元数据)

注意:(1)该操作必须使用sys用户才能完成;(2)exp命令中加入transport_tablespace=y表示传输表空间。

[oracle@wgx bak]$ exp \'sys/sys as sysdba\' tablespaces=data01 transport_tablespace=y file=/home/oracle/bak/transport_data01.dmp log=/home/oracle/bak/transport_data01.log

Export: Release 11.2.0.1.0 - Production on Tue Apr 7 17:47:32 2020

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

Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
Export done in US7ASCII character set and AL16UTF16 NCHAR character set
server uses AL32UTF8 character set (possible charset conversion)
Note: table data (rows) will not be exported
About to export transportable tablespace metadata...
For tablespace DATA01 ...
. exporting cluster definitions
. exporting table definitions
. . exporting table                            E01
. . exporting table                            E02
. exporting referential integrity constraints
. exporting triggers
. end transportable tablespace metadata export
Export terminated successfully without warnings.

查看导出的文件信息:

[oracle@wgx bak]$ ll -h
总用量 20K
-rw-r--r-- 1 oracle oinstall 16K 4月   7 17:48 transport_data01.dmp
-rw-r--r-- 1 oracle oinstall 758 4月   7 17:48 transport_data01.log

从导出的文件大小可以看出,传输表空间导出的不是数据,而是表空间信息。因此在恢复数据时需要导出的元数据文件和表空间对应的数据文件。

step3、利用导出的元数据文件和表空间对应的数据文件恢复数据

(1)把两个文件复制到另一台计算机

[oracle@wgx bak]$ scp /home/oracle/bak/transport_data01.dmp /usr/local/oradata/orcl/data01.dbf [email protected]:/home/oracle

[email protected]'s password: 
transport_data01.dmp                  100%   16KB  16.0KB/s   00:00    
data01.dbf                            100%   50MB   7.1MB/s   00:07    
[oracle@wgx bak]$ 

(2)登录192.168.1.202计算机

查看/home/oracle文件中的文件:

[oracle@wgx ~]$ ll
total 51224
-rw-r----- 1 oracle oinstall 52436992 Apr  8 05:52 data01.dbf
-rw-r--r-- 1 oracle oinstall    16384 Apr  8 05:52 transport_data01.dmp

登录oracle,查看表空间及数据文件信息:

SQL> select tablespace_name from dba_tablespaces;

TABLESPACE_NAME
------------------------------
SYSTEM
SYSAUX
UNDOTBS1
TEMP
USERS

SQL> select name from v$datafile;

NAME
--------------------------------------------------------------------------------
/usr/local/oradata/orcl/system01.dbf
/usr/local/oradata/orcl/sysaux01.dbf
/usr/local/oradata/orcl/undotbs01.dbf
/usr/local/oradata/orcl/users01.dbf

(3)把表空间对应的数据文件data01.dbf移动到文件/usr/local/oradata/orcl/下:

[oracle@wgx ~]$ mv /home/oracle/data01.dbf /usr/local/oradata/orcl/data01.dbf
[oracle@wgx ~]$ ll /usr/local/oradata/orcl/
total 1505724
-rw-r----- 1 oracle oinstall   9748480 Apr  8 05:59 control01.ctl
-rw-r----- 1 oracle oinstall  52436992 Apr  8 05:52 data01.dbf
-rw-r----- 1 oracle oinstall  21045248 Apr  8 05:50 data02.dbf
-rw-r----- 1 oracle oinstall  52429312 Apr  8 05:58 redo01.log
-rw-r----- 1 oracle oinstall  52429312 Apr  8 01:55 redo02.log
-rw-r----- 1 oracle oinstall  52429312 Apr  8 01:55 redo03.log
-rw-r----- 1 oracle oinstall 513810432 Apr  8 05:55 sysaux01.dbf
-rw-r----- 1 oracle oinstall 702554112 Apr  8 05:55 system01.dbf
-rw-r----- 1 oracle oinstall  30416896 Apr  8 02:55 temp01.dbf
-rw-r----- 1 oracle oinstall  78651392 Apr  8 05:56 undotbs01.dbf
-rw-r----- 1 oracle oinstall   5251072 Apr  8 01:55 users01.dbf

step4、导入表空间

[oracle@wgx ~]$ imp \'sys/sys as sysdba\' tablespaces=data01 transport_tablespace=y file=/home/oracle/transport_data01.dmp datafiles=\'/usr/local/oradata/orcl/data01.dbf\' buffer=1048576

Import: Release 11.2.0.1.0 - Production on Wed Apr 8 06:02:33 2020

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

Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.1.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
About to import transportable tablespace(s) metadata...
import done in US7ASCII character set and AL16UTF16 NCHAR character set
import server uses AL32UTF8 character set (possible charset conversion)
. importing SYS's objects into SYS
. importing SYS's objects into SYS
. importing SCOTT's objects into SCOTT
. . importing table                          "E01"
. . importing table                          "E02"
. importing SYS's objects into SYS
Import terminated successfully without warnings.

查看表空间及数据文件信息:

SQL> select tablespace_name from dba_tablespaces;

TABLESPACE_NAME
------------------------------
SYSTEM
SYSAUX
UNDOTBS1
TEMP
USERS
DATA01

6 rows selected.

SQL> select name from v$datafile;

NAME
--------------------------------------------------------------------------------
/usr/local/oradata/orcl/system01.dbf
/usr/local/oradata/orcl/sysaux01.dbf
/usr/local/oradata/orcl/undotbs01.dbf
/usr/local/oradata/orcl/users01.dbf
/usr/local/oradata/orcl/data01.dbf

查看data01表空间中的表信息:

SQL> select table_name from dba_tables where tablespace_name='DATA01';

TABLE_NAME
------------------------------
E01
E02

step5、把data01表空间设置为可读写状态

SQL> alter tablespace data01 read write;

Tablespace altered.
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章