1.备份与恢复概述
DBA 的最主要任务就是维持数据库的高可靠性运行,尽量提高数据库的连续可用时间( Mean-Time-Between-Failures ),降低数据库的平均恢复时间(Mean-Time-To-Recover,MTTR),同时最小化故障时的数据损失及业务影响。
数据库环境中 ,常见的故障类型有:
1.语句失败
2.用户进程失败
3.用户错误
4.实例失败
5.介质故障
6.网络故障
1.实例失败: 数据库实例异常中断,如断电、异常错误导致Crash、Shutdown abort强制终止。这类失败在下一次数据库重新启动后会自动执行实例恢复(Instance Recovery)
通常最为严重的故障主要是用户错误和介质故障
2.用户错误:主要指用户由于疏忽错误的drop/truncate 掉数据表,或者误操作delete/update 表中的数据并最终提交。通常就需要 DBA 介入进行数据恢复或错误纠正。
3.介质故障:一般指当数据库对数据文件进行读写时出现问题,问题可能是由于硬件的故障、软件的Bug、或者文件的意外删除等引发,介质故障是最为严重的数据库故障,也是DBA需要精心应付的主要问题。
为了防止种种故障可能造成的数据损失,Oracle 提供了不同手段来进行数据备份,通过制定完善的备份策略,可以最小化数据损失
2.Oracle 备份方法
按照备份方式:逻辑备份和物理备份
逻辑备份指通过逻辑导出对数据进行备份,逻辑备份的数据只能基于备份时刻进行数据转储,所以恢复时也只能恢复到备份时保存的数据。对于故障点和备份点之间的数据,逻辑备份是无能为力的,逻辑备份适合备份那些很少变化的数据表,当这些数据因误操作被损坏时,可以通过逻辑备份进行快速恢复。如果通过逻辑备份进行全库恢复,通常需要重建数据库,导入备份数据来完成,对于可用性要求很高的数据库,这种恢复的时间太长,通常不被采用。由于逻辑备份具有平台无关性,所以更为常见的是,逻辑备份被作为一个数据迁移及移动的主要手段。
物理备份是指通过物理文件拷贝的方式对数据库进行备份,物理备份又可以分为冷备份和热备份。冷备份是指对数据库进行关闭后的拷贝备份,这样的备份具有一致和完整的时间点数据,恢复时只需要恢复所有文件就可以启动数据库;在生产系统中最为常见的备份方式是热备份,进行热备份的数据库需要运行在归档模式,热备份时不需要关闭数据库,从而能够保证系统的持续运行,在进行恢复时,通过备份的数据文件及归档日志等文件,数据库可以进行完全恢复,恢复可以一直进行到最后一个归档日志,如果联机日志存在,则恢复可以继续,实现无数据损失的完全恢复。当然,如果是为了恢复某些用户错误,热备份的恢复完全可以在某一个时间点上停止恢复,也就是不完全恢复。
3. 恢复原理
Oracle 数据库有一个重要的组成结构:重做日志(Redo Log)。重做日志用来记录数据库操作的必要信息,以便在发生故障时能够通过事务重演来恢复数据。Oracle 的数据恢复就依赖于重做日志文件(Redo Log File)以及由其衍生的归档日志文件(Archived Redo Log File)。
针对不同的故障情况,Oracle 可以通过不同的方式进行数据恢复,下面我们将主要介绍对于热备份的恢复。根据不同的故障情况,热备份的恢复可以分为完全恢复和不完全恢复两类。
如果在恢复时我们拥有足够的归档日志(Archived Redo Log)和在线重做日志(Online Redo Log),那么通过恢复一个全备份,应用归档日志和重做日志,最终数据库就可以实现完全恢复,恢复后的数据库不会有任何数据损失,这个恢复如下图所示:
如果恢复在应用日志完成之前停止,则进行的就是一次不完全恢复。逐渐应用日志向前恢复的过程称为前滚(Roll Forward),前滚的过程实际上就是应用日志重演事务的过程(Replay transactions),完成前滚后,数据文件将包含提交和未提交的数据,然后需要应用回滚数据,将未提交的事务回滚,这个过程称为rolling back或transaction recovery。
通常,完全恢复应用于那些由于硬件故障导致的数据库损失,在这种情况下需要最大可能的恢复数据;不完全恢复通常用于恢复用户错误,比如用户在图8-1中Time2时间点误操作Drop掉一个业务数据表,那么恢复需要执行到Time2之前停止,这样就可以找回被误删除的数据表,此时执行的就是一次不完全恢复。
在实际管理中,很多情况下进行的是不完全恢复,选择不完全恢复的可能原因很多,最常见的情况如下。
A. 归档日志丢失。由于某个归档日志丢失,恢复只能执行的过去的某个时间点。
B. 在线日志文件损坏。在线的日志文件损坏,则恢复只能停止在损坏的日志之前。
C. 用户错误操作。用户错误地 drop/truncate 了数据表,恢复必须在这些动作发出前停止,以完成数据恢复。
不完全恢复主要有 4 种类型:基于时间的恢复(Time-based Recovery)、基于放弃的恢复(Cancel-based Recovery)、基于改变的恢复(Change-based Recovery)和基于日志序列的恢复(Log sequence recovery)。
基于时间点的恢复最为常用。当然更多情况下,我们可能面对的是单个数据文件或表空间的损坏。对于这类故障,可以针对单个文件或表空间进行完全恢复,恢复通常并不会影响整个数据库的运行,受影响可能只是和故障表空间相关的应用。
4.检查点与RBA
在恢复的内部控制上,Oracle数据库通过检查点和RBA信息来确定恢复范围,正常情况下,最后一个完成的检查点对应的RBA是恢复的起点(Cache-Low RBA),恢复能够达到的终点是On-Disk RBA,也就是最后一个写出到磁盘的REDO日志记录。在实例恢复中,Oracle数据库就将进行介于Cache-Low RBA和On-Disk RBA 之间的自动实例恢复。
5.数据库的运行模式
归档模式( Archivelog )和非归档模式(NoArchivelog)是Oracle数据库的两种运行方式,所谓归档是指对历史的RedoLog日志文件进行归档保存。Oracle依赖Redo Log File来进行故障恢复,也就是重做,在非归档模式下,Redo Log File以覆盖的方式循环使用,在归档模式下,日志文件被覆盖之前必须已经被复制归档,保留的归档日志将为Oracle提供强大的故障恢复能力。
有一个问题大家都应该清楚:最危险的数据库是什么样子的?答案很简单:非归档、无备份。更改数据库的归档模式需要重新启动数据库,在mount模式下修改,以下是步骤说明。修改必要的初始化参数。以immediate方式关闭数据库。启动实例到mount状态。更改运行模式并打开数据库。
以下简单介绍如何启用和关闭数据库的归档模式。
(1)修改初始化参数。和归档相关的几个主要参数如下。
log_archive_start:用于定义是否启动自动归档,Oracle10g这个参数废弃。log_archive_format:用于定义归档文件格式,可以采用缺省值。log_archive_dest:用于定义归档文件路径,与log_archive_dest_n参数不兼容。log_archive_dest_n:Oracle允许定义多个归档路径,一般可以使用log_archive_dest_1参数即可。
如果数据库使用的是pfile文件,则可以直接修改,如果用的是spfile文件,则可以通过命令行修改:
sys@cdb$root:orclcdb>
sys@cdb$root:orclcdb> show parameter spfile;
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
spfile string /opt/oracle/product/19c/dbhome
_1/dbs/spfileORCLCDB.ora
sys@cdb$root:orclcdb>
(2)关闭数据库。以shutdown normal或shutdown immediate方式关闭数据库:
SQL> shutdown immediate;
Database closed.
Database dismounted.
ORACLE instance shut down.
3)启动数据库到mount状态:
SQL> startup mount;
ORACLE instance started.
Database mounted.
(4)启用或停止归档模式。如果要启用归档模式,此处使用alter database archivelog命令:
SQL> alter database archivelog;
SQL> alter database open;
SQL> archive log list;
Database log mode ArchiveMode
Automatic archival Enabled
如果需要停止归档模式,此处使用alter database noarchivelog命令:
SQL> alter database noarchivelog;
SQL> alter database open;
闪回恢复区可以指定为文件系统上的一个目录,也可以是一个ASM磁盘组,闪回区的大小由db_recovery_file_dest_size 参数指定,路径由db_recovery_file_dest参数指定:
SQL> show parameter db_recover
设置归档日志存放在闪回区,可以使用如下命令:
SQL> alter system set log_archive_dest_1 ='LOCATION=USE_DB_RECOVERY_FILE_DEST';
System altered.SQL> archive log list;
Database log mode Archive
ModeAutomatic archival Disabled
闪回区的一个优势是Oracle会执行自动的空间维护,过期的文件或备份可以自动被删除以释放空间。在闪回恢复区中的空间使用超过85%的时候,数据库将会向alert文件中写入告警信息;当超过97%的时候将会写入严重告警信息;当闪回恢复区空间不够的时候,则数据库将会挂起,日志中会记录如下信息:
删除归档日志文件,正确的做法应该是使用RMAN登录数据库进行crosscheck,再删除过期的备份,空间才能得到释放。常用的命令如下:crosscheck archivelog all;delete expired archivelog all;
6.逻辑备份与恢复
6.1 使用EXP进行逻辑备份
导入/导出(IMP/EXP)是Oracle最古老的两个命令行工具,通过导出(EXP)工具可以将Oracle数据库中的数据提取出来,在恢复时可以将数据导入(IMP)进行恢复。
注意: 使用EXP命令备份数据进行全库恢复时,需要重新创建数据库,导入备份的数据,恢复的过程可能会极其漫长。
[oracle@oracle-db-19c ~]$ exp -help
Export: Release 19.0.0.0.0 - Production on Tue Mar 28 08:44:30 2023
Version 19.3.0.0.0
Copyright (c) 1982, 2019, Oracle and/or its affiliates. All rights reserved.
You can let Export prompt you for parameters by entering the EXP
command followed by your username/password:
Example: EXP SCOTT/TIGER
Or, you can control how Export runs by entering the EXP command followed
by various arguments. To specify parameters, you use keywords:
Format: EXP KEYWORD=value or KEYWORD=(value1,value2,...,valueN)
Example: EXP SCOTT/TIGER GRANTS=Y TABLES=(EMP,DEPT,MGR)
or TABLES=(T1:P1,T1:P2), if T1 is partitioned table
USERID must be the first parameter on the command line.
Keyword Description (Default) Keyword Description (Default)
--------------------------------------------------------------------------
USERID username/password FULL export entire file (N)
BUFFER size of data buffer OWNER list of owner usernames
FILE output files (EXPDAT.DMP) TABLES list of table names
COMPRESS import into one extent (Y) RECORDLENGTH length of IO record
GRANTS export grants (Y) INCTYPE incremental export type
INDEXES export indexes (Y) RECORD track incr. export (Y)
DIRECT direct path (N) TRIGGERS export triggers (Y)
LOG log file of screen output STATISTICS analyze objects (ESTIMATE)
ROWS export data rows (Y) PARFILE parameter filename
CONSISTENT cross-table consistency(N) CONSTRAINTS export constraints (Y)
OBJECT_CONSISTENT transaction set to read only during object export (N)
FEEDBACK display progress every x rows (0)
FILESIZE maximum size of each dump file
FLASHBACK_SCN SCN used to set session snapshot back to
FLASHBACK_TIME time used to get the SCN closest to the specified time
QUERY select clause used to export a subset of a table
RESUMABLE suspend when a space related error is encountered(N)
RESUMABLE_NAME text string used to identify resumable statement
RESUMABLE_TIMEOUT wait time for RESUMABLE
TTS_FULL_CHECK perform full or partial dependency check for TTS
VOLSIZE number of bytes to write to each tape volume
TABLESPACES list of tablespaces to export
TRANSPORT_TABLESPACE export transportable tablespace metadata (N)
TEMPLATE template name which invokes iAS mode export
Export terminated successfully without warnings.
[oracle@oracle-db-19c ~]$
6.2 简单介绍几个EXP的注意事项及特殊用法
1.EXP导出与字符集
[oracle@oracle-db-19c ~]$ export NLS_LANG=AMERICAN_AMERICA.UTF8
[oracle@oracle-db-19c ~]$ echo $NLS_LANG
AMERICAN_AMERICA.UTF8
[oracle@oracle-db-19c ~]$
数据库端使用的字符集可以通过如下查询获得:
[oracle@oracle-db-19c ~]$ sqlplus / as sysdba
SQL*Plus: Release 19.0.0.0.0 - Production on Tue Mar 28 08:53:54 2023
Version 19.3.0.0.0
Copyright (c) 1982, 2019, Oracle. All rights reserved.
Connected to:
Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production
Version 19.3.0.0.0
SQL> show user;
USER is "SYS"
SQL> show con_name
CON_NAME
------------------------------
CDB$ROOT
SQL> alter session set container=PDB1;
Session altered.
SQL>
SQL> select * from NLS_DATABASE_PARAMETERS where parameter='NLS_CHARACTERSET';
PARAMETER
--------------------------------------------------------------------------------
VALUE
--------------------------------------------------------------------------------
NLS_CHARACTERSET
AL32UTF8
SQL>
2.带查询子句的部分导出
[oracle@oracle-db-19c ~]$ exp scott/tiger@PDB1 file=test.dmp tables=emp query=\"where sal \< 3000\"
Export: Release 19.0.0.0.0 - Production on Tue Mar 28 08:59:50 2023
Version 19.3.0.0.0
Copyright (c) 1982, 2019, Oracle and/or its affiliates. All rights reserved.
Connected to: Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production
Version 19.3.0.0.0
Export done in UTF8 character set and AL16UTF16 NCHAR character set
server uses AL32UTF8 character set (possible charset conversion)
About to export specified tables via Conventional Path ...
. . exporting table EMP 11 rows exported
EXP-00091: Exporting questionable statistics.
EXP-00091: Exporting questionable statistics.
EXP-00091: Exporting questionable statistics.
Export terminated successfully with warnings.
[oracle@oracle-db-19c ~]$
3.参数文件的使用
对于类似以上的复杂处理,可以通过参数PARFILE来指定一个参数文件,具体的参数写在参数文件中,这样就可以避免复杂的转义等操作:
[oracle@oracle-db-19c backup_and_recovery]$ vim parfile.lst
[oracle@oracle-db-19c backup_and_recovery]$ cat parfile.lst
userid=scott/tiger@PDB1
file=test1.dmp
tables=emp
query="where sal < 3000"
[oracle@oracle-db-19c backup_and_recovery]$ expparfile=parfile.lst
4.使用通配符进行模糊导出
有时候,我们希望按条件导出部分表,EXP工具支持模糊查找,通配符可以在tables参数中使用
[oracle@oracle-db-19c backup_and_recovery]$ exp scott/tiger@PDB1 tables=e% file=a.dmp
Export: Release 19.0.0.0.0 - Production on Tue Mar 28 09:12:02 2023
Version 19.3.0.0.0
Copyright (c) 1982, 2019, Oracle and/or its affiliates. All rights reserved.
Connected to: Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production
Version 19.3.0.0.0
Export done in UTF8 character set and AL16UTF16 NCHAR character set
server uses AL32UTF8 character set (possible charset conversion)
About to export specified tables via Conventional Path ...
. . exporting table EMP 14 rows exported
EXP-00091: Exporting questionable statistics.
EXP-00091: Exporting questionable statistics.
EXP-00091: Exporting questionable statistics.
. . exporting table EMP1 917504 rows exported
. . exporting table EMP2 14 rows exported
EXP-00091: Exporting questionable statistics.
. . exporting table EMP_AGGR_MV 3 rows exported
. . exporting table EMP_BKP 14 rows exported
EXP-00091: Exporting questionable statistics.
. . exporting table EMP_PANDAS 14 rows exported
EXP-00091: Exporting questionable statistics.
. . exporting table EMP_TEMP 14 rows exported
EXP-00091: Exporting questionable statistics.
EXP-00091: Exporting questionable statistics.
EXP-00091: Exporting questionable statistics.
. . exporting table EMP_TEMP1 14 rows exported
EXP-00091: Exporting questionable statistics.
. . exporting table EXAMPLE 5 rows exported
EXP-00091: Exporting questionable statistics.
EXP-00091: Exporting questionable statistics.
Export terminated successfully with warnings.
[oracle@oracle-db-19c backup_and_recovery]$ ls -ltr
total 43012
-rw-r--r--. 1 oracle oinstall 16384 Mar 28 08:59 test.dmp
-rw-r--r--. 1 oracle oinstall 75 Mar 28 09:06 parfile.lst
-rw-r--r--. 1 oracle oinstall 44023808 Mar 28 09:12 a.dmp
[oracle@oracle-db-19c backup_and_recovery]$
5.按日期区分文件名称的导出
在导出备份时,我们希望在文件名中加入日期变量,以区分不同日期的导出文件,也可以防止覆盖。在导出文件名中引入时间有很多不同的做法,本文主要介绍以下两种方法。
(1)通过数据库查询获取日期。
(2)通过操作系统获取日期。
[oracle@oracle-db-19c backup_and_recovery]$ exp scott/tiger@PDB1 file=maxwell_`date +%Y%m%d`
Export: Release 19.0.0.0.0 - Production on Tue Mar 28 09:21:20 2023
Version 19.3.0.0.0
Copyright (c) 1982, 2019, Oracle and/or its affiliates. All rights reserved.
Connected to: Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production
Version 19.3.0.0.0
Export done in ZHS16GBK character set and AL16UTF16 NCHAR character set
server uses AL32UTF8 character set (possible charset conversion)
. exporting pre-schema procedural objects and actions
. exporting foreign function library names for user SCOTT
. exporting PUBLIC type synonyms
. exporting private type synonyms
. exporting object type definitions for user SCOTT
About to export SCOTT's objects ...
. exporting database links
. exporting sequence numbers
. exporting cluster definitions
. about to export SCOTT's tables via Conventional Path ...
. . exporting table A 2 rows exported
EXP-00091: Exporting questionable statistics.
. . exporting table A1 4 rows exported
EXP-00091: Exporting questionable statistics.
. . exporting table B1 3 rows exported
EXP-00091: Exporting questionable statistics.
. . exporting table BONUS 0 rows exported
EXP-00091: Exporting questionable statistics.
. . exporting table DEPT 4 rows exported
EXP-00091: Exporting questionable statistics.
EXP-00091: Exporting questionable statistics.
. . exporting table DEPT1 2 rows exported
EXP-00091: Exporting questionable statistics.
. . exporting table DEPT2 4 rows exported
EXP-00091: Exporting questionable statistics.
. . exporting table EMP 14 rows exported
EXP-00091: Exporting questionable statistics.
EXP-00091: Exporting questionable statistics.
EXP-00091: Exporting questionable statistics.
. . exporting table EMP1 917504 rows exported
. . exporting table EMP2 14 rows exported
EXP-00091: Exporting questionable statistics.
. . exporting table EMP_AGGR_MV 3 rows exported
. . exporting table EMP_BKP 14 rows exported
EXP-00091: Exporting questionable statistics.
. . exporting table EMP_PANDAS 14 rows exported
EXP-00091: Exporting questionable statistics.
. . exporting table EMP_TEMP 14 rows exported
EXP-00091: Exporting questionable statistics.
EXP-00091: Exporting questionable statistics.
EXP-00091: Exporting questionable statistics.
. . exporting table EMP_TEMP1 14 rows exported
EXP-00091: Exporting questionable statistics.
. . exporting table EXAMPLE 5 rows exported
EXP-00091: Exporting questionable statistics.
. . exporting table MAXWELL_FLOAT_TEST 4 rows exported
EXP-00091: Exporting questionable statistics.
. . exporting table OB1 73271 rows exported
EXP-00091: Exporting questionable statistics.
. . exporting table SALGRADE 5 rows exported
EXP-00091: Exporting questionable statistics.
. . exporting table T 2 rows exported
EXP-00091: Exporting questionable statistics.
. . exporting table T01 0 rows exported
EXP-00091: Exporting questionable statistics.
. . exporting table T100 0 rows exported
EXP-00091: Exporting questionable statistics.
. . exporting table TAB1 0 rows exported
EXP-00091: Exporting questionable statistics.
. . exporting table TAB2 0 rows exported
EXP-00091: Exporting questionable statistics.
. . exporting table TEST 13 rows exported
. . exporting table X 2 rows exported
EXP-00091: Exporting questionable statistics.
. . exporting table Y 2 rows exported
EXP-00091: Exporting questionable statistics.
. exporting synonyms
. exporting views
. exporting stored procedures
. exporting operators
. exporting referential integrity constraints
. exporting triggers
. exporting indextypes
. exporting bitmap, functional and extensible indexes
. exporting posttables actions
. exporting materialized views
. exporting snapshot logs
. exporting job queues
. exporting refresh groups and children
. exporting dimensions
. exporting post-schema procedural objects and actions
. exporting statistics
Export terminated successfully with warnings.
[oracle@oracle-db-19c backup_and_recovery]$ ls -ltr
total 97908
-rw-r--r--. 1 oracle oinstall 16384 Mar 28 08:59 test.dmp
-rw-r--r--. 1 oracle oinstall 75 Mar 28 09:06 parfile.lst
-rw-r--r--. 1 oracle oinstall 44023808 Mar 28 09:12 a.dmp
-rw-r--r--. 1 oracle oinstall 56213504 Mar 28 09:21 maxwell_20230328.dmp
[oracle@oracle-db-19c backup_and_recovery]$
6.3 使用IMP进行逻辑恢复
由于使用EXP备份的数据进行恢复时,一般需要重新创建数据库,导入备份的数据,恢复的过程可能会极为漫长;如果是做局部恢复(只对部分用户或部分表的恢复)则要相对简单。
7. EXP/IMP
传统的导出导入程序指的是exp/imp,用于实施数据库的逻辑备份和恢复.
导出程序exp将数据库中的对象定义和数据备份到一个操作系统二进制文件中.
导入程序imp读取二进制导出文件并将对象和数据载入数据库中
传统的导出导入是基于客户端的.在$ORACLE_HOME/bin下
7.1 导出和导入实用程序的特点有:
1).可以按时间保存表结构和数据
2).允许导出指定的表,并重新导入到新的数据库中
3).可以把数据库迁移到另外一台异构服务器上
4).在两个不同版本的Oracle数据库之间传输数据
5).在联机状态下进行备份和恢复
6).可以重新组织表的存储结构,减少链接及磁盘碎片
7.2 使用以下三种方法调用导出和导入实用程序:
1).交互提示符 :以交互的方式提示用户逐个输入参数的值.
2).命令行参数 :在命令行指定执行程序的参数和参数值.
3).参数文件 :允许用户将运行参数和参数值存储在参数文件中,以便重复使用参数
7.3 导出和导入数据库对象的四种模式是:
1).数据库模式 :导出和导入整个数据库中的所有对象
2).TBS模式 :导出和导入一个或多个指定的 TBS中的所有对象
3).用户模式 :导出和导入一个用户模式中的所有对象
4).表模式 :导出和导入一个或多个指定的表或表分区
7.4 导入导出表
scott导入导出自己的表,一般是从服务器导出到客户端(在cmd下操作)
7.4.1 查看命令格式
[oracle@oracle-db-19c ~]$
[oracle@oracle-db-19c ~]$ imp help=y
Import: Release 19.0.0.0.0 - Production on Tue Mar 28 19:28:38 2023
Version 19.3.0.0.0
Copyright (c) 1982, 2019, Oracle and/or its affiliates. All rights reserved.
You can let Import prompt you for parameters by entering the IMP
command followed by your username/password:
Example: IMP SCOTT/TIGER
Or, you can control how Import runs by entering the IMP command followed
by various arguments. To specify parameters, you use keywords:
Format: IMP KEYWORD=value or KEYWORD=(value1,value2,...,valueN)
Example: IMP SCOTT/TIGER IGNORE=Y TABLES=(EMP,DEPT) FULL=N
or TABLES=(T1:P1,T1:P2), if T1 is partitioned table
USERID must be the first parameter on the command line.
Keyword Description (Default) Keyword Description (Default)
--------------------------------------------------------------------------
USERID username/password FULL import entire file (N)
BUFFER size of data buffer FROMUSER list of owner usernames
FILE input files (EXPDAT.DMP) TOUSER list of usernames
SHOW just list file contents (N) TABLES list of table names
IGNORE ignore create errors (N) RECORDLENGTH length of IO record
GRANTS import grants (Y) INCTYPE incremental import type
INDEXES import indexes (Y) COMMIT commit array insert (N)
ROWS import data rows (Y) PARFILE parameter filename
LOG log file of screen output CONSTRAINTS import constraints (Y)
DESTROY overwrite tablespace data file (N)
INDEXFILE write table/index info to specified file
SKIP_UNUSABLE_INDEXES skip maintenance of unusable indexes (N)
FEEDBACK display progress every x rows(0)
TOID_NOVALIDATE skip validation of specified type ids
FILESIZE maximum size of each dump file
STATISTICS import precomputed statistics (always)
RESUMABLE suspend when a space related error is encountered(N)
RESUMABLE_NAME text string used to identify resumable statement
RESUMABLE_TIMEOUT wait time for RESUMABLE
COMPILE compile procedures, packages, and functions (Y)
STREAMS_CONFIGURATION import streams general metadata (Y)
STREAMS_INSTANTIATION import streams instantiation metadata (N)
DATA_ONLY import only data (N)
VOLSIZE number of bytes in file on each volume of a file on tape
The following keywords only apply to transportable tablespaces
TRANSPORT_TABLESPACE import transportable tablespace metadata (N)
TABLESPACES tablespaces to be transported into database
DATAFILES datafiles to be transported into database
TTS_OWNERS users that own data in the transportable tablespace set
Import terminated successfully without warnings.
[oracle@oracle-db-19c ~]$
7.4.2 案例建表
# Step1 建表
SQL>
SQL> create table emp1 as select * from emp;
Table created.
SQL> create table dept1 as select * from dept;
Table created.
SQL>
# Step2 导出表
$ pwd
/home/oracle/backup_and_recovery
$
$ exp scott/tiger@PDB1 file=/home/oracle/backup_and_recovery/empdept1.dmp tables=\(emp1,dept1\);
Export: Release 19.0.0.0.0 - Production on Tue Mar 28 19:54:01 2023
Version 19.3.0.0.0
Copyright (c) 1982, 2019, Oracle and/or its affiliates. All rights reserved.
Connected to: Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production
Version 19.3.0.0.0
Export done in UTF8 character set and AL16UTF16 NCHAR character set
server uses AL32UTF8 character set (possible charset conversion)
About to export specified tables via Conventional Path ...
. . exporting table EMP1 14 rows exported
EXP-00091: Exporting questionable statistics.
. . exporting table DEPT1 4 rows exported
EXP-00091: Exporting questionable statistics.
Export terminated successfully with warnings.
$ exp scott/tiger@PDB1 file=/home/oracle/backup_and_recovery/empdept2.dmp tables=emp1,dept1;
Export: Release 19.0.0.0.0 - Production on Tue Mar 28 19:54:20 2023
Version 19.3.0.0.0
Copyright (c) 1982, 2019, Oracle and/or its affiliates. All rights reserved.
Connected to: Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production
Version 19.3.0.0.0
Export done in UTF8 character set and AL16UTF16 NCHAR character set
server uses AL32UTF8 character set (possible charset conversion)
About to export specified tables via Conventional Path ...
. . exporting table EMP1 14 rows exported
EXP-00091: Exporting questionable statistics.
. . exporting table DEPT1 4 rows exported
EXP-00091: Exporting questionable statistics.
Export terminated successfully with warnings.
$
$ ls -ltr
total 32
-rw-r--r--. 1 oracle oinstall 16384 Mar 28 19:54 empdept1.dmp
-rw-r--r--. 1 oracle oinstall 16384 Mar 28 19:54 empdept2.dmp
$
# Step 3 删除表
SQL>
SQL> select * from emp1;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ------------------------------ --------------------------- ---------- --------------- ---------- ---------- ----------
7369 SMITH CLERK 7902 17-DEC-80 800 20
7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30
7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30
7566 JONES MANAGER 7839 02-APR-81 2975 20
7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30
7698 BLAKE MANAGER 7839 01-MAY-81 2850 30
7782 CLARK MANAGER 7839 09-JUN-81 2450 10
7788 SCOTT ANALYST 7566 24-JAN-87 3000 20
7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30
7876 ADAMS CLERK 7788 02-APR-87 1100 20
7900 JAMES CLERK 7698 03-DEC-81 950 30
7902 FORD ANALYST 7566 03-DEC-81 3000 20
7934 MILLER CLERK 7782 23-JAN-82 1300 10
7839 KING PRESIDENT 8000 17-NOV-81 5000 0 10
14 rows selected.
SQL> select * from dept1;
DEPTNO DNAME LOC
---------- ------------------------------------------ ---------------------------------------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
SQL>
SQL> drop table emp1 purge;
Table dropped.
SQL> drop table dept1 purge;
Table dropped.
SQL>
SQL> select * from emp1;
select * from emp1
*
ERROR at line 1:
ORA-00942: table or view does not exist
SQL> select * from dept1;
select * from dept1
*
ERROR at line 1:
ORA-00942: table or view does not exist
SQL>
# Step 4 导入表
$
$ imp scott/tiger@PDB1 file=/home/oracle/backup_and_recovery/empdept1.dmp
Import: Release 19.0.0.0.0 - Production on Tue Mar 28 19:59:55 2023
Version 19.3.0.0.0
Copyright (c) 1982, 2019, Oracle and/or its affiliates. All rights reserved.
Connected to: Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production
Version 19.3.0.0.0
Export file created by EXPORT:V19.00.00 via conventional path
import done in UTF8 character set and AL16UTF16 NCHAR character set
import server uses AL32UTF8 character set (possible charset conversion)
. importing SCOTT's objects into SCOTT
. importing SCOTT's objects into SCOTT
. . importing table "EMP1" 14 rows imported
. . importing table "DEPT1" 4 rows imported
Import terminated successfully without warnings.
$
# Step 5 验证原表
SQL> show user;
USER is "SCOTT"
SQL> show con_name;
CON_NAME
------------------------------
PDB1
SQL> select * from emp1;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ------------------------------ --------------------------- ---------- --------------- ---------- ---------- ----------
7369 SMITH CLERK 7902 17-DEC-80 800 20
7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30
7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30
7566 JONES MANAGER 7839 02-APR-81 2975 20
7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30
7698 BLAKE MANAGER 7839 01-MAY-81 2850 30
7782 CLARK MANAGER 7839 09-JUN-81 2450 10
7788 SCOTT ANALYST 7566 24-JAN-87 3000 20
7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30
7876 ADAMS CLERK 7788 02-APR-87 1100 20
7900 JAMES CLERK 7698 03-DEC-81 950 30
7902 FORD ANALYST 7566 03-DEC-81 3000 20
7934 MILLER CLERK 7782 23-JAN-82 1300 10
7839 KING PRESIDENT 8000 17-NOV-81 5000 0 10
14 rows selected.
SQL> select * from dept1;
DEPTNO DNAME LOC
---------- ------------------------------------------ ---------------------------------------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
SQL>
7.4.3 导入导出用户
当前用户scott导出自己的所有对象, 注意仅仅导出的是schema的object,也就是说不包括数据字典中的信息,比如用户账户,及原有的一些系统权限等等.
7.4.3.1 导出scott用户的object
$ exp scott/tiger@PDB1 file=/home/oracle/backup_and_recovery/scott.dmp owner=scott
Export: Release 19.0.0.0.0 - Production on Tue Mar 28 21:01:07 2023
Version 19.3.0.0.0
Copyright (c) 1982, 2019, Oracle and/or its affiliates. All rights reserved.
SQL> CREATE DIRECTORY export_dir AS '/home/oracle/backup_and_recovery';
Directory created.
SQL>
SQL>