Oracle database backup and recovery under Linux (emp and imp commands)

                                      Oracle database backup and recovery under Linux (emp and imp commands)

Compared with MySQL, Oracle's backup and restore commands are more powerful, the backup command is emp, and the restore command is imp. The logical file used is usually suffixed with dmp, oracle logical backup file, which is often used for database logical backup, database migration and other operations.

 

The exp export tool compresses the data backup in the database into a binary system file. It can be migrated between different OSs

It has three modes:
       a. User mode: export all objects of the user and the data in the object;
       b. Table mode: export all tables or specified tables of the user;
       c. Entire database: export all objects in the database.

The imp import tool has three modes:
       a. User mode: import all objects of the user and the data in the objects;
       b. Table mode: import all tables or specified tables of the user;
       c. Entire database: import all objects in the database.

The steps performed by imp: 
   (1) create table (2) insert data (3) create index (4) create triggers, constraints-create a table, insert data, generate indexes, create new triggers, and various constraints.

It should be noted that only users with IMP_FULL_DATABASE and DBA permissions can do the entire database import 

Problems that may occur when using the import tool imp:

(1) The database object already exists in 
general, the tables, sequences, functions/procedures, triggers, etc. under the target data should be completely deleted before importing the data; the   
database object already exists, press the default the imp parameters will import fails 
if the parameter ignore = y, is introduced into the contents of the data file will exp 
if a table has a unique key constraints, undesirable conditions will not be introduced into 
the constraint if the table is not unique keys , Will cause duplicate records 

(2) When the database object has primary and foreign key constraints that 
      do not meet the primary and foreign key constraints, the data will fail
      to import.   Solution: First
import the primary table, then import the dependent table  disable the primary and foreign key constraints of the target import object, import After the data, enable them 
(3) Insufficient permissions 
If you want to import user A's data to user B, user A needs to have imp_full_database permission 

(4) When importing large tables (greater than 80M), the storage allocation fails. When the 
      default EXP, compress = Y, that is, compress all data in one data block. When 
      importing, if there is no continuous large data block, the import will fail. When 
      exporting a large table above 80M, remember that compress = N, it will not cause This kind of error. 

(5) The character set used by imp and exp is different. 
      If the character set is different, the import will fail. You can change the unix environment variable or the NLS_LANG related information in the NT registry. 
      Change it back after the import is complete. 

select * from v$nls_parameters where parameter='NLS_CHARACTERSET'; --Query the character set used by the current system

select * from v$nls_parameters; --Query all parameters related to character set

Modify the character set of the system:

Before Oracle 8, you can directly modify the data dictionary table props$ to change the character set of the database. However, after oracle8, there are at least three system tables that record the information of the database character set. Only changing the props$ table is not complete and may cause serious consequences. The correct modification method is as follows

If the database server is started at this time, first execute the SHUTDOWN IMMEDIATE command to shut down the database server, and then execute the following command:
  SQL>STARTUP MOUNT;
  SQL>ALTER SYSTEM ENABLE RESTRICTED SESSION;
  SQL>ALTER SYSTEM SET JOB_QUEUE_PROCESSES=0;
  SQL>ALTER SYSTEM SET AQ_TM_PROCESSES=0;
  SQL>ALTER DATABASE OPEN;
  SQL>ALTER DATABASE CHARACTER SET INTERNAL_USE ZHS16GBK; //Skip supersubset detection AL32UTF8;
  SQL>ALTER DATABASE national CHARACTER SET INTERNAL ZHS16GBK;AL32UTF8;

The above process is: close the database, start the database to mount state, and enter the database maintenance mode. At this time, only system and sys users can log in to the database. Close the DBMS scheduler and DBMS schedule tasks, close the automatic task start, skip the character super subset check, set the system character set, close the database, and restart the database.



(6) Imp and exp versions cannot be upwardly compatible. 
Imp can successfully import files generated by lower versions of exp, but cannot import files generated by higher versions of exp. 
According to the situation, we can use

 

 

The difference between exp and imp and data pump expdp and impdp:

1、exp和imp是客户端工具程序,它们既可以在客户端使用,也可以在服务端使用。exp在客户端使用,要受到网速的影响,因此会比较慢。
2、expdp和impdp是服务端的工具程序,他们只能在Oracle服务端使用,不能在客户端使用。
3、imp只适用于exp导出的文件,不适用于expdp导出文件;impdp只适用于expdp导出的文件,而不适用于exp导出文件。
4、对于10g以上的服务器,使用exp通常不能导出0行数据的空表,而此时必须使用expdp导出。
5,数据泵expdp和impdp导出可以使用parallel属性指定并行任务导出,parallel=2就是并行2个任务导出。当然,这个并不是越多越好,需要考虑服务器的性能和cpu的个数等等因素。还可以通过导出多个文件的方式提高性能,即使用dumpfile=expdp.dp1,expdp.dp2这种方式提高性能。需要用户根据实际情况设置
6,exp导出时读取的是sql,通过加载sql查询结果到缓存,然后在写进目标文件,而expdp是直接读取的数据块,直接从数据块写进目标文件。

 

Let's start with a simple example, export the emp table of user zsk and import the emp table.

a, export the emp table of user zsk

Explain that the zsk user is a new user, and the emp table is created by itself. After the table is created, export the emp table as a file, then delete the emp table, and restore the emp table in the import file.

b, create a zsk user and give the appropriate permissions

Enter sqlplus, use the sys user to log in to the database, create a new zsk user, grant permissions to create tables, insert data, and delete tables.

create user zsk identified by zsk; grant create session to zsk; grant unlimited tablespace to zsk;​​​​​​​grant create any table to zsk;--connect database permissions, read tablespace permissions, create new table permissions, just These permissions are enough, and the zsk user password is zsk.

c. Create a sample table emp, the SQL statement for table creation is as follows, insert 14 records:

create table EMP
(
empno NUMBER(4),
ename VARCHAR2(10),
job VARCHAR2(9),
mgr NUMBER(4),
hiredate DATE,
sal NUMBER(7,2),
comm NUMBER(7,2),
deptno NUMBER(2)
)
;
insert into EMP (empno, ename, job, mgr, hiredate, sal, comm, deptno)
values (1, 'dog', 'code', 7369, to_date('05-06-2018', 'dd-mm-yyyy'), 5000, 5000, 10);
insert into EMP (empno, ename, job, mgr, hiredate, sal, comm, deptno)
values (7369, 'SMITH', 'CLERK', 7902, to_date('17-12-1980', 'dd-mm-yyyy'), 800, null, 20);
insert into EMP (empno, ename, job, mgr, hiredate, sal, comm, deptno)
values (7499, 'ALLEN', 'SALESMAN', 7698, to_date('20-02-1981', 'dd-mm-yyyy'), 1600, 300, 
30);
insert into EMP (empno, ename, job, mgr, hiredate, sal, comm, deptno)
values (7521, 'WARD', 'SALESMAN', 7698, to_date('22-02-1981', 'dd-mm-yyyy'), 1250, 500, 30);
insert into EMP (empno, ename, job, mgr, hiredate, sal, comm, deptno)
values (7566, 'JONES', 'MANAGER', 7839, to_date('02-04-1981', 'dd-mm-yyyy'), 2975, null, 
20);
insert into EMP (empno, ename, job, mgr, hiredate, sal, comm, deptno)
values (7654, 'MARTIN', 'SALESMAN', 7698, to_date('28-09-1981', 'dd-mm-yyyy'), 1250, 1400, 
30);
insert into EMP (empno, ename, job, mgr, hiredate, sal, comm, deptno)
values (7698, 'BLAKE', 'MANAGER', 7839, to_date('01-05-1981', 'dd-mm-yyyy'), 2850, null, 
30);
insert into EMP (empno, ename, job, mgr, hiredate, sal, comm, deptno)
values (7788, 'SCOTT', 'ANALYST', 7566, to_date('19-04-1987', 'dd-mm-yyyy'), 3000, null, 
20);
insert into EMP (empno, ename, job, mgr, hiredate, sal, comm, deptno)
values (7839, 'KING', 'PRESIDENT', null, to_date('17-11-1981', 'dd-mm-yyyy'), 5000, null, 
10);
insert into EMP (empno, ename, job, mgr, hiredate, sal, comm, deptno)
values (7844, 'TURNER', 'SALESMAN', 7698, to_date('08-09-1981', 'dd-mm-yyyy'), 1500, 0, 30);
insert into EMP (empno, ename, job, mgr, hiredate, sal, comm, deptno)
values (7876, 'ADAMS', 'CLERK', 7788, to_date('23-05-1987', 'dd-mm-yyyy'), 1100, null, 20);
insert into EMP (empno, ename, job, mgr, hiredate, sal, comm, deptno)
values (7900, 'JAMES', 'CLERK', 7698, to_date('03-12-1981', 'dd-mm-yyyy'), 950, null, 30);
insert into EMP (empno, ename, job, mgr, hiredate, sal, comm, deptno)
values (7902, 'FORD', 'ANALYST', 7566, to_date('03-12-1981', 'dd-mm-yyyy'), 3000, null, 20);
insert into EMP (empno, ename, job, mgr, hiredate, sal, comm, deptno)
values (7934, 'MILLER', 'CLERK', 7782, to_date('23-01-1982', 'dd-mm-yyyy'), 1300, null, 10);
commit;

d, export the emp table

Under the Oracle user, enter the command exp to enter interactive

[oracle@centos11 ~]$ exp

Export: Release 11.2.0.1.0 - Production on Tue Jan 19 20:13:43 2021

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


Username: zsk
Password: 

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
Enter array fetch buffer size: 4096 > 

Export file: expdat.dmp > 

(2)U(sers), or (3)T(ables): (2)U > T

Export table data (yes/no): yes > 

Compress extents (yes/no): yes > 

Export done in US7ASCII character set and AL16UTF16 NCHAR character set
server uses AL32UTF8 character set (possible charset conversion)

About to export specified tables via Conventional Path ...
Table(T) or Partition(T:P) to be exported: (RETURN to quit) > emp

. . exporting table                            EMP         14 rows exported
Table(T) or Partition(T:P) to be exported: (RETURN to quit) > 

Export terminated successfully without warnings.

The saved file is in the oracle directory under the Oracle user directory. The above is the default. Enter the name of the table to be backed up after the RETURN toquit line, and then press Enter all the way. The above export is to enable compression by default, and export table data, and the input T means to export a single table. The second RETURN to quit is input when you want to export the second table. If you don't want to export, just press Enter.

e, import the emp database.

First, sqlplus logs in to zsk and deletes the emp table. drop table emp;

Then execute the command: imp  zsk/zsk@test file=/home/oracle/expdat.dmp

The database name is test, so write test after @, and enter the following as normal:

[oracle@centos11 ~]$ imp zsk/zsk@test file=/home/oracle/emp.dmp

Import: Release 11.2.0.1.0 - Production on Tue Jan 19 20:25:06 2021

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 ZSK's objects into ZSK
. importing ZSK's objects into ZSK
. . importing table                         "EMPS"         14 rows imported
Import terminated successfully without warnings.

If the original table is imported without being deleted, an error object will be reported to exist, and the error is reported as follows, although it is also successful:

[oracle@centos11 ~]$ imp zsk/zsk@test file=/home/oracle/expdat.dmp

Import: Release 11.2.0.1.0 - Production on Tue Jan 19 20:27:24 2021

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 ZSK's objects into ZSK
. importing ZSK's objects into ZSK
IMP-00015: following statement failed because the object already exists:
 "CREATE TABLE "EMPS" ("EMPNO" NUMBER(4, 0), "ENAME" VARCHAR2(10), "JOB" VARC"
 "HAR2(9), "MGR" NUMBER(4, 0), "HIREDATE" DATE, "SAL" NUMBER(7, 2), "COMM" NU"
 "MBER(7, 2), "DEPTNO" NUMBER(2, 0))  PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRA"
 "NS 255 STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 FREELISTS 1 FREELIST"
 " GROUPS 1 BUFFER_POOL DEFAULT)                    LOGGING NOCOMPRESS"
Import terminated successfully with warnings.

 

 

 


    
   i

 

Guess you like

Origin blog.csdn.net/alwaysbefine/article/details/112757991