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.
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章