• Oracle 逻辑备份(数据迁移)


    以逻辑结构为单位进行的备份

    • 跨用户移动数据
    • 跨数据库移动数据库
    • 为测试保存原始的数据状态
    • 对数据库进行版本升级

    逻辑导出的注意事项:

    • exp程序在目录中发现同名文件时会直接覆盖,不提示!!
    • exp无法备份无段的空表
    • 执行逻辑导出时一定要注意字符集!最好使用包含中文的小表做测试!!
    • 执行逻辑导出时一定要注意字符集!最好使用包含中文的小表做测试!!
    • 导入时的数据和导出时的数据一模一样,导出之后数据库中表的数据变化全都丢失!!

    逻辑导出:所有版本都可用,服务器端和客户端都可用。

    mkdir -p /home/oracle/expbk

    exp userid=scott/tiger@pdb1 table=t01 file=/home/oralce/expbk/t01.dmp buffer=1048576 feedback=10000 log=/home/oracle/expbk/t01.log

    1. [oracle@oracle-db-19c expbk]$
    2. [oracle@oracle-db-19c expbk]$ exp userid=scott/tiger@PDB1 tables=emp file=/home/oracle/expbk/emp.dmp buffer=1048576 log=/home/oracle/expbk/emp.log
    3. Export: Release 19.0.0.0.0 - Production on Mon Dec 5 23:34:37 2022
    4. Version 19.3.0.0.0
    5. Copyright (c) 1982, 2019, Oracle and/or its affiliates. All rights reserved.
    6. Connected to: Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production
    7. Version 19.3.0.0.0
    8. Export done in US7ASCII character set and AL16UTF16 NCHAR character set
    9. server uses AL32UTF8 character set (possible charset conversion)
    10. About to export specified tables via Conventional Path ...
    11. . . exporting table EMP 14 rows exported
    12. EXP-00091: Exporting questionable statistics.
    13. EXP-00091: Exporting questionable statistics.
    14. Export terminated successfully with warnings.
    15. [oracle@oracle-db-19c expbk]$

    逻辑导入:

    drop table t01 purge;

    imp userid=scott/tiger tables=t01 file=/home/oracle/expbk/t01.dmp buffer=1048576 feedback=10000 log=/home/oracle/expbk/t01.log
    
    1. [oracle@oracle-db-19c expbk]$
    2. [oracle@oracle-db-19c expbk]$ imp userid=scott/tiger@PDB1 tables=emp file=/home/oracle/expbk/emp.dmp buffer=1048576 log=/home/oracle/expbk/imp_emp.log
    3. Import: Release 19.0.0.0.0 - Production on Tue Dec 6 18:13:14 2022
    4. Version 19.3.0.0.0
    5. Copyright (c) 1982, 2019, Oracle and/or its affiliates. All rights reserved.
    6. Connected to: Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production
    7. Version 19.3.0.0.0
    8. Export file created by EXPORT:V19.00.00 via conventional path
    9. import done in UTF8 character set and AL16UTF16 NCHAR character set
    10. import server uses AL32UTF8 character set (possible charset conversion)
    11. export client uses US7ASCII character set (possible charset conversion)
    12. . importing SCOTT's objects into SCOTT
    13. . importing SCOTT's objects into SCOTT
    14. . . importing table "EMP" 14 rows imported
    15. About to enable constraints...
    16. Import terminated successfully without warnings.
    17. [oracle@oracle-db-19c expbk]$

    导出数据时带有查询条件:

    exp scott/tiger tables=emp file=/home/oracle/expbk/emp_30.dmp query=\'where deptno=30\' buffer=1000000 log=/home/oracle/expbk/emp_30.log
    1. [oracle@oracle-db-19c expbk]$
    2. [oracle@oracle-db-19c expbk]$ exp scott/tiger@PDB1 tables=emp file=/home/oracle/expbk/emp_30.dmp query=\'where deptno=30\' buffer=1000000 log=/home/oracle/expbk/emp_30.log
    3. Export: Release 19.0.0.0.0 - Production on Tue Dec 6 18:30:18 2022
    4. Version 19.3.0.0.0
    5. Copyright (c) 1982, 2019, Oracle and/or its affiliates. All rights reserved.
    6. Connected to: Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production
    7. Version 19.3.0.0.0
    8. Export done in UTF8 character set and AL16UTF16 NCHAR character set
    9. server uses AL32UTF8 character set (possible charset conversion)
    10. About to export specified tables via Conventional Path ...
    11. . . exporting table EMP 6 rows exported
    12. EXP-00091: Exporting questionable statistics.
    13. EXP-00091: Exporting questionable statistics.
    14. Export terminated successfully with warnings.
    15. [oracle@oracle-db-19c expbk]$ ls -ltr
    16. total 48
    17. -rw-r--r--. 1 oracle oinstall 516 Dec 5 23:34 emp.log
    18. -rw-r--r--. 1 oracle oinstall 16384 Dec 5 23:34 emp.dmp
    19. -rw-r--r--. 1 oracle oinstall 614 Dec 6 18:13 imp_emp.log
    20. -rw-r--r--. 1 oracle oinstall 153 Dec 6 18:21 employees.log
    21. -rw-r--r--. 1 oracle oinstall 512 Dec 6 18:30 emp_30.log
    22. -rw-r--r--. 1 oracle oinstall 16384 Dec 6 18:30 emp_30.dmp
    23. [oracle@oracle-db-19c expbk]$

     

    导入时追加数据:ignore=y

    (1)删除30部门的数据。

    1. SQL>
    2. SQL> delete emp where deptno=30;
    3. 6 rows deleted.
    4. SQL> commit;
    5. Commit complete.
    6. SQL> select * from emp;
    7. EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
    8. ---------- ---------- --------- ---------- --------- ---------- ---------- ----------
    9. 7369 SMITH CLERK 7902 17-DEC-80 800 20
    10. 7566 JONES MANAGER 7839 02-APR-81 2975 20
    11. 7782 CLARK MANAGER 7839 09-JUN-81 2450 10
    12. 7788 SCOTT ANALYST 7566 24-JAN-87 3000 20
    13. 7839 KING PRESIDENT 17-NOV-81 5000 10
    14. 7876 ADAMS CLERK 7788 02-APR-87 1100 20
    15. 7902 FORD ANALYST 7566 03-DEC-81 3000 20
    16. 7934 MILLER CLERK 7782 23-JAN-82 1300 10
    17. 8 rows selected.

     (2)导入备份数据到表中

    1. [oracle@oracle-db-19c expbk]$
    2. [oracle@oracle-db-19c expbk]$ imp scott/tiger@PDB1 tables=emp file=/home/oracle/expbk/emp_30.dmp ignore=y buffer=100000 log=/home/oracle/expbk/imp_emp_30.log
    3. Import: Release 19.0.0.0.0 - Production on Tue Dec 6 18:33:35 2022
    4. Version 19.3.0.0.0
    5. Copyright (c) 1982, 2019, Oracle and/or its affiliates. All rights reserved.
    6. Connected to: Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production
    7. Version 19.3.0.0.0
    8. Export file created by EXPORT:V19.00.00 via conventional path
    9. import done in UTF8 character set and AL16UTF16 NCHAR character set
    10. import server uses AL32UTF8 character set (possible charset conversion)
    11. . importing SCOTT's objects into SCOTT
    12. . importing SCOTT's objects into SCOTT
    13. . . importing table "EMP" 6 rows imported
    14. About to enable constraints...
    15. Import terminated successfully without warnings.
    16. [oracle@oracle-db-19c expbk]$

     (3)查询数据

    1. SQL> select * from emp;
    2. EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
    3. ---------- ---------- --------- ---------- --------- ---------- ---------- ----------
    4. 7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30
    5. 7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30
    6. 7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30
    7. 7698 BLAKE MANAGER 7839 01-MAY-81 2850 30
    8. 7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30
    9. 7900 JAMES CLERK 7698 03-DEC-81 950 30
    10. 7369 SMITH CLERK 7902 17-DEC-80 800 20
    11. 7566 JONES MANAGER 7839 02-APR-81 2975 20
    12. 7782 CLARK MANAGER 7839 09-JUN-81 2450 10
    13. 7788 SCOTT ANALYST 7566 24-JAN-87 3000 20
    14. 7839 KING PRESIDENT 17-NOV-81 5000 10
    15. 7876 ADAMS CLERK 7788 02-APR-87 1100 20
    16. 7902 FORD ANALYST 7566 03-DEC-81 3000 20
    17. 7934 MILLER CLERK 7782 23-JAN-82 1300 10
    18. 14 rows selected.
    19. SQL>

     闪回导出: 只有system可以调用

    exp system/system@PDB1 tables=scott.emp file=/home/oracle/expbk/emp_1010.dmp buffer=1000000 flashback_time=\"to_timestamp\(\'2022-12-06 19:00:00\',\'yyyy-mm-dd hh24:mi:ss'\)\" log=/home/oracle/expbk/emp_1010.log
    1. [oracle@oracle-db-19c expbk]$
    2. [oracle@oracle-db-19c expbk]$ exp system/system@PDB1 tables=scott.e03 file=/home/oracle/expbk/e03_1930.dmp buffer=1000000 flashback_time=\"to_timestamp\(\'2022-12-06 19:30:00\',\'yyyy-mm-dd hh24:mi:ss\'\)\" log=/home/oracle/expbk/e03_1930.log
    3. Export: Release 19.0.0.0.0 - Production on Tue Dec 6 19:47:12 2022
    4. Version 19.3.0.0.0
    5. Copyright (c) 1982, 2019, Oracle and/or its affiliates. All rights reserved.
    6. Connected to: Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production
    7. Version 19.3.0.0.0
    8. Export done in UTF8 character set and AL16UTF16 NCHAR character set
    9. server uses AL32UTF8 character set (possible charset conversion)
    10. About to export specified tables via Conventional Path ...
    11. Current user changed to SCOTT
    12. . . exporting table E03 14 rows exported
    13. EXP-00091: Exporting questionable statistics.
    14. Export terminated successfully with warnings.
    15. [oracle@oracle-db-19c expbk]$

    导入数据:

    1. [oracle@oracle-db-19c expbk]$ imp system/system@PDB1 file=/home/oracle/expbk/e03_1930.dmp full=y ignore=y
    2. Import: Release 19.0.0.0.0 - Production on Tue Dec 6 19:49:06 2022
    3. Version 19.3.0.0.0
    4. Copyright (c) 1982, 2019, Oracle and/or its affiliates. All rights reserved.
    5. Connected to: Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production
    6. Version 19.3.0.0.0
    7. Export file created by EXPORT:V19.00.00 via conventional path
    8. import done in UTF8 character set and AL16UTF16 NCHAR character set
    9. import server uses AL32UTF8 character set (possible charset conversion)
    10. IMP-00403:
    11. Warning: This import generated a separate SQL file "import_sys" which contains DDL that failed due to a privilege issue.
    12. . importing SYSTEM's objects into SYSTEM
    13. . importing SCOTT's objects into SCOTT
    14. . . importing table "E03" 14 rows imported
    15. Import terminated successfully with warnings.
    16. [oracle@oracle-db-19c expbk]$

    只导出表结构(元数据)不导出数据:备份模型,不备份数据

    1. exp scott/tiger@PDB1 tables=ob1 rows=n file=/home/oracle/expbk/ob1_metadata.dmp log=/home/oracle/expbk/ob1_metadata.log

     

    select object_name,created from user_objects;

    vi /home/oracle/expbk/exp.sh

    1. export ORACLE_HOME = $ORACLE_BASE/product/19.3.0/dbhome_1
    2. export ORACLE_SID = PDB1
    3. export LANG=en_US.UTF-8
    4. export NLS_LANG=AMERICAN_AMERICA.AL32UTF8
    5. name=`date '+%Y%m%d_%H%M%s'`
    6. `$ORACLE_HOME/bin/exp userid=scott/tiger@PDB1 tables=t01 file=/home/oracle/expbk/t01\_$name.dmp buffer=1048576 feedback=10000 log=/home/oracle/expbk/t01\_$name.log`
    chmod +x /home/oracle/expbk/exp.sh

     

    1. [oracle@oracle-db-19c expbk]$
    2. [oracle@oracle-db-19c expbk]$ sh -x exp.sh
    3. + export ORACLE_HOME = /u01/app/oracle/product/19.3.0/dbhome_1
    4. exp.sh: line 2: export: `=': not a valid identifier
    5. exp.sh: line 2: export: `/u01/app/oracle/product/19.3.0/dbhome_1': not a valid identifier
    6. + export ORACLE_SID = PDB1
    7. exp.sh: line 3: export: `=': not a valid identifier
    8. + export LANG=en_US.UTF-8
    9. + LANG=en_US.UTF-8
    10. + export NLS_LANG=AMERICAN_AMERICA.AL32UTF8
    11. + NLS_LANG=AMERICAN_AMERICA.AL32UTF8
    12. ++ date +%Y%m%d_%H%M%S
    13. + name=20221206_20191670329163
    14. ++ /u01/app/oracle/product/19.3.0/dbhome_1/bin/exp userid=scott/tiger@PDB1 tables=t01 file=/home/oracle/expbk/t01_20221206_20191670329163.dmp buffer=1048576 feedback=10000 log=/home/oracle/expbk/t01_20221206_20191670329163.log
    15. Export: Release 19.0.0.0.0 - Production on Tue Dec 6 20:19:23 2022
    16. Version 19.3.0.0.0
    17. Copyright (c) 1982, 2019, Oracle and/or its affiliates. All rights reserved.
    18. Connected to: Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production
    19. Version 19.3.0.0.0
    20. Export done in AL32UTF8 character set and AL16UTF16 NCHAR character set
    21. About to export specified tables via Conventional Path ...
    22. . . exporting table T01
    23. 14 rows exported
    24. Export terminated successfully without warnings.
    25. [oracle@oracle-db-19c expbk]$ cat exp.sh
    26. export ORACLE_HOME=$ORACLE_BASE/product/19.3.0/dbhome_1
    27. export ORACLE_SID=PDB1
    28. export LANG=en_US.UTF-8
    29. export NLS_LANG=AMERICAN_AMERICA.AL32UTF8
    30. name=`date '+%Y%m%d_%H%M%s'`
    31. `$ORACLE_HOME/bin/exp userid=scott/tiger@PDB1 tables=t01 file=/home/oracle/expbk/t01\_$name.dmp buffer=1048576 feedback=10000 log=/home/oracle/expbk/t01\_$name.log`
    32. [oracle@oracle-db-19c expbk]$
    1. [oracle@oracle-db-19c expbk]$
    2. [oracle@oracle-db-19c expbk]$ echo $ORACLE_HOME
    3. /u01/app/oracle/product/19.3.0/dbhome_1
    4. [oracle@oracle-db-19c expbk]$ echo $ORACLE_BASE
    5. /u01/app/oracle
    6. [oracle@oracle-db-19c expbk]$

    导出用户:

    1. exp userid=scott/tiger@PDB1 owner=scott file=/home/oracle/expbk/scott.dmp buffer=1048576 feedback=10000 log=/home/oracle/expbk/scott.log
    1. [oracle@oracle-db-19c expbk]$
    2. [oracle@oracle-db-19c expbk]$
    3. [oracle@oracle-db-19c expbk]$ exp userid=scott/tiger@PDB1 owner=scott file=/home/oracle/expbk/scott.dmp buffer=1048576 feedback=10000 log=/home/oracle/expbk/scott.log
    4. Export: Release 19.0.0.0.0 - Production on Tue Dec 6 20:29:06 2022
    5. Version 19.3.0.0.0
    6. Copyright (c) 1982, 2019, Oracle and/or its affiliates. All rights reserved.
    7. Connected to: Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production
    8. Version 19.3.0.0.0
    9. Export done in UTF8 character set and AL16UTF16 NCHAR character set
    10. server uses AL32UTF8 character set (possible charset conversion)
    11. . exporting pre-schema procedural objects and actions
    12. . exporting foreign function library names for user SCOTT
    13. . exporting PUBLIC type synonyms
    14. . exporting private type synonyms
    15. . exporting object type definitions for user SCOTT
    16. About to export SCOTT's objects ...
    17. . exporting database links
    18. . exporting sequence numbers
    19. . exporting cluster definitions
    20. . about to export SCOTT's tables via Conventional Path ...
    21. . . exporting table BONUS
    22. 0 rows exported
    23. EXP-00091: Exporting questionable statistics.
    24. . . exporting table DEPT
    25. 4 rows exported
    26. EXP-00091: Exporting questionable statistics.
    27. EXP-00091: Exporting questionable statistics.
    28. . . exporting table E01
    29. 13 rows exported
    30. EXP-00091: Exporting questionable statistics.
    31. . . exporting table E03
    32. 14 rows exported
    33. EXP-00091: Exporting questionable statistics.
    34. . . exporting table EMP
    35. 14 rows exported
    36. EXP-00091: Exporting questionable statistics.
    37. EXP-00091: Exporting questionable statistics.
    38. . . exporting table EMP_AGGR_MV
    39. 3 rows exported
    40. . . exporting table SALGRADE
    41. 5 rows exported
    42. EXP-00091: Exporting questionable statistics.
    43. . . exporting table T01
    44. 14 rows exported
    45. EXP-00091: Exporting questionable statistics.
    46. . . exporting table TAB1
    47. 0 rows exported
    48. EXP-00091: Exporting questionable statistics.
    49. . . exporting table TAB2
    50. 0 rows exported
    51. EXP-00091: Exporting questionable statistics.
    52. . exporting synonyms
    53. . exporting views
    54. . exporting stored procedures
    55. . exporting operators
    56. . exporting referential integrity constraints
    57. . exporting triggers
    58. . exporting indextypes
    59. . exporting bitmap, functional and extensible indexes
    60. . exporting posttables actions
    61. . exporting materialized views
    62. . exporting snapshot logs
    63. . exporting job queues
    64. . exporting refresh groups and children
    65. . exporting dimensions
    66. . exporting post-schema procedural objects and actions
    67. . exporting statistics
    68. Export terminated successfully with warnings.
    69. [oracle@oracle-db-19c expbk]$

    先删除SCOTT用户:

    1. SQL>
    2. SQL> show user
    3. USER is "SYS"
    4. SQL> show con_name
    5. CON_NAME
    6. ------------------------------
    7. CDB$ROOT
    8. SQL>
    9. SQL> drop user scott cascade;
    10. drop user scott cascade
    11. *
    12. ERROR at line 1:
    13. ORA-01918: user 'SCOTT' does not exist
    14. SQL>
    15. SQL> alter session set container=PDB1;
    16. Session altered.
    17. SQL> drop user scott cascade;
    18. User dropped.
    19. SQL>
    20. SQL>
    21. SQL>
    22. SQL> grant connect,resource to scott identified by tiger;
    23. Grant succeeded.
    24. SQL> conn scott/tiger@PDB1;
    25. Connected.
    26. SQL> select * from emp;
    27. select * from emp
    28. *
    29. ERROR at line 1:
    30. ORA-00942: table or view does not exist
    31. SQL>

    导入用户:

    1. imp userid=scott/tiger full=y file=/home/oracle/expbk/scott.dmp buffer=1048576 feedback=10000 log=/home/oracle/expbk/impscott.log

    跨用户导入数据 : scott ---> tom

    imp userid=system/system@PDB1 file=scott.dmp fromuser=scott touser=tom tables=dept,emp,salgrade buffer=1000000 log=imptom.log
    

    使用主机管道压缩备份文件:

    mknod /home/oracle/expbk/exp_pipe p

    1. exp userid=scott/tiger@PDB1 owner=scott log=/home/oracle/expk/scott.log file=/home/oracle/expbk/exp_pipe & gzip </home/oracle/expbk/exp_pipe> scott.dmp.gz
    1. [oracle@oracle-db-19c expbk]$ exp userid=scott/tiger@PDB1 owner=scott log=/home/oracle/expbk/scott.log file=/home/oracle/expbk/exp_pipe & gzip </home/oracle/expbk/exp_pipe> scott.dmp.gz
    2. [1] 87055
    3. -bash: /home/oracle/expbk/exp_pipe: No such file or directory
    4. [oracle@oracle-db-19c expbk]$
    5. Export: Release 19.0.0.0.0 - Production on Tue Dec 6 21:13:04 2022
    6. Version 19.3.0.0.0
    7. Copyright (c) 1982, 2019, Oracle and/or its affiliates. All rights reserved.
    8. Connected to: Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production
    9. Version 19.3.0.0.0
    10. Export done in US7ASCII character set and AL16UTF16 NCHAR character set
    11. server uses AL32UTF8 character set (possible charset conversion)
    12. . exporting pre-schema procedural objects and actions
    13. . exporting foreign function library names for user SCOTT
    14. . exporting PUBLIC type synonyms
    15. . exporting private type synonyms
    16. . exporting object type definitions for user SCOTT
    17. About to export SCOTT's objects ...
    18. . exporting database links
    19. . exporting sequence numbers
    20. . exporting cluster definitions
    21. . about to export SCOTT's tables via Conventional Path ...
    22. . . exporting table BONUS 0 rows exported
    23. EXP-00091: Exporting questionable statistics.
    24. . . exporting table DEPT 4 rows exported
    25. EXP-00091: Exporting questionable statistics.
    26. EXP-00091: Exporting questionable statistics.
    27. . . exporting table EMP 14 rows exported
    28. EXP-00091: Exporting questionable statistics.
    29. EXP-00091: Exporting questionable statistics.
    30. . . exporting table EMP_AGGR_MV 3 rows exported
    31. . . exporting table SALGRADE 5 rows exported
    32. EXP-00091: Exporting questionable statistics.
    33. . . exporting table T01 0 rows exported
    34. EXP-00091: Exporting questionable statistics.
    35. . . exporting table TAB1 0 rows exported
    36. EXP-00091: Exporting questionable statistics.
    37. . . exporting table TAB2 0 rows exported
    38. EXP-00091: Exporting questionable statistics.
    39. . exporting synonyms
    40. . exporting views
    41. . exporting stored procedures
    42. . exporting operators
    43. . exporting referential integrity constraints
    44. . exporting triggers
    45. . exporting indextypes
    46. . exporting bitmap, functional and extensible indexes
    47. . exporting posttables actions
    48. . exporting materialized views
    49. . exporting snapshot logs
    50. . exporting job queues
    51. . exporting refresh groups and children
    52. . exporting dimensions
    53. . exporting post-schema procedural objects and actions
    54. . exporting statistics
    55. Export terminated successfully with warnings.
    56. [1]+ Done exp userid=scott/tiger@PDB1 owner=scott log=/home/oracle/expbk/scott.log file=/home/oracle/expbk/exp_pipe
    57. [oracle@oracle-db-19c expbk]$

     

    导入用户数据时,要在数据库中将用户创建好。

    imp scott/tiger@PDB1 file=scott full=y buffer=10000000 log=/home/oracle/expbk/imp_scott.log

    导出表空间:

    exp system/system@PDB1 tablespaces=tbs1 file=/home/oracle/expbk/exp_tbs1.dmp buffer=10000000

  • 相关阅读:
    力扣刷题链表需要调试?一个简单的调试器帮你解决苦恼
    Windows11 环境安装Gradle
    Unity3D教程:手游开发常用排序算法 -上
    LIF-MDF6000-6KMG80I FPGA现场可编程门阵列 封装BGA
    leetcode-06-[454]四数相加II[383]赎金信 [15] 三数之和 [18] 四数之和
    SqlBoy:异或、交换奇偶
    Redis - 高级
    【性能测试】JMeter:集合点,同步定时器的应用实例!
    STM32实战总结:HAL之触摸按键
    前端:nodejs多版本管理工具nvm
  • 原文地址:https://blog.csdn.net/u011868279/article/details/128195126