Backup dan Restore Schema Oracle Menggunakan exp dan imp
Oracle menyediakan perintah untuk membackup schema dan merestore schema hasil backup tersebut. Kedua perintah tersebut yaitu exp untuk membackup schema dan imp untuk merestore schema hasil backup sebelumnya.
Kita coba dengan backup terlebih dahulu. Pertama kita buat direktori backup untuk menampung hasil backup schema yang kita lakukan, kemudian kita gunakan direktori tersebut sebagai base direktori pekerjaan kita:
oracle@NeuronServer:~> mkdir schema-backup oracle@NeuronServer:~> cd schema-backup/ oracle@NeuronServer:~/schema-backup>
Misalnya kita ingin membackup schema train yang memiliki password train yang ada di server oracle dengan service id neuron, file backup schema-nya kita simpan dengan nama train.dmp. Penggunaan perintah exp-nya seperti dibawah ini:
oracle@NeuronServer:~/schema-backup> exp train/train@neuron file=train.dmp Export: Release 10.2.0.1.0 - Production on Fri Jan 2 16:17:32 2009 Copyright (c) 1982, 2005, Oracle. All rights reserved. Connected to: Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production With the Partitioning, OLAP and Data Mining options Export done in US7ASCII character set and AL16UTF16 NCHAR character set server uses WE8ISO8859P1 character set (possible charset conversion) . exporting pre-schema procedural objects and actions . exporting foreign function library names for user TRAIN . exporting PUBLIC type synonyms . exporting private type synonyms . exporting object type definitions for user TRAIN About to export TRAIN's objects ... . exporting database links . exporting sequence numbers . exporting cluster definitions . about to export TRAIN's tables via Conventional Path ... . . exporting table TRAIN 2 rows exported . 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 without warnings. oracle@NeuronServer:~/schema-backup>
File train.dmp tersebut dapat kita gunakan untuk merestore schema tersebut dikemudian hari atau untuk menduplikat schema tersebut ke server yang lain.
Sebagai contoh kita gunakan file tersebut untuk menduplikat schema di mesin yang berbeda tapi memiliki user yang sama yaitu train. Penggunaan imp:
oracle@linuxbox:~> imp train/train@xe file=/home/alam/train.dmp Import: Release 10.2.0.1.0 - Production on Fri Jan 2 15:43:10 2009 Copyright (c) 1982, 2005, Oracle. All rights reserved. Connected to: Oracle Database 10g Express Edition Release 10.2.0.1.0 - Production Export file created by EXPORT:V10.02.01 via conventional path import done in US7ASCII character set and AL16UTF16 NCHAR character set import server uses AL32UTF8 character set (possible charset conversion) . importing TRAIN's objects into TRAIN . . importing table "TRAIN" 2 rows imported Import terminated successfully without warnings. oracle@linuxbox:~>
Pastikan bahwa schema yang dimaksud (train) ada di server yang hendak direstore. Perlu diingat bahwa perintah imp ini akan menimpa schema yang sudah ada di server, jadi pastikan bahwa anda memang ingin menimpa schema tersebut.
UPDATE
Ada banyak opsi-opsi yang dapat kita gunakan dalam menggunakan exp dan imp. Untuk mengetahui apa saja opsi-opsi yang tersedia jalankan perintah berikut:
oracle@NeuronServer:~> exp help=y Export: Release 10.2.0.1.0 - Production on Fri Jan 2 14:06:04 2009 Copyright (c) 1982, 2005, Oracle. 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@NeuronServer:~> imp help=y Import: Release 10.2.0.1.0 - Production on Fri Jan 2 14:08:51 2009 Copyright (c) 1982, 2005, Oracle. 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) 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@NeuronServer:~>
Sebelumnya kita mengekspor schema yang memiliki dua user yang sama di dua sistem yang berbeda. Bagaimana jika database yang akan mengimpor menggunakan username yang berbeda? Sebagai contoh saya akan mencoba mengimpor schema train yang sebelumnya dibackup untuk direstore di server yang ada di mesin yang berbeda, dimana user yang digunakan yaitu neurondev dengan password neurondev yang ada di service xe. Logisnya saya akan menggunakan command dibawah ini:
oracle@linuxbox:~> imp neurondev/neurondev@xe file=/home/alam/train.dmp Import: Release 10.2.0.1.0 - Production on Fri Jan 2 13:48:33 2009 Copyright (c) 1982, 2005, Oracle. All rights reserved. Connected to: Oracle Database 10g Express Edition Release 10.2.0.1.0 - Production Export file created by EXPORT:V10.02.01 via conventional path Warning: the objects were exported by TRAIN, not by you import done in US7ASCII character set and AL16UTF16 NCHAR character set import server uses AL32UTF8 character set (possible charset conversion) . importing TRAIN's objects into NEURONDEV Import terminated successfully without warnings. oracle@linuxbox:~>
Kita tidak bisa mengimpor schema yang dibuat oleh user yang lain yang berbeda dengan yang kita gunakan untuk mengimpor schema tersebut, dalam hal ini backup schema tersebut dimiliki oleh user train sedangkan kita menggunakan user neurondev. Bagaimana solusinya? Gunakan opsi fromuser dan touser yang disediakan oleh imp seperti ini:
oracle@linuxbox:~> imp neurondev/neurondev@xe file=/home/alam/train.dmp fromuser=train touser=neurondev Import: Release 10.2.0.1.0 - Production on Fri Jan 2 14:57:59 2009 Copyright (c) 1982, 2005, Oracle. All rights reserved. Connected to: Oracle Database 10g Express Edition Release 10.2.0.1.0 - Production Export file created by EXPORT:V10.02.01 via conventional path Warning: the objects were exported by TRAIN, not by you import done in US7ASCII character set and AL16UTF16 NCHAR character set import server uses AL32UTF8 character set (possible charset conversion) . . importing table "TRAIN" 2 rows imported Import terminated successfully without warnings. oracle@linuxbox:~>
Opsi fromuser digunakan untuk memberitahu oracle user awal yang memiliki backup schema tersebut (owner) dan opsi touser digunakan untuk menentukan user mana yang akan memiliki schema hasil impor tersebut. Jadi kita isi train untuk opsi fromuser karena backup schema tersebut dimiliki oleh user train dan dibackupnya pun oleh user train. Sedangkan opsi fromuser kita isi dengan neurondev karena user ini yang akan memiliki schema hasil impor tersebut dan juga user yang melakukan proses impornya.
Sekarang mari kita coba mengimpornya menggunakan user yang sama sekali berbeda:
oracle@linuxbox:~> imp admin/admin@xe file=/home/alam/train.dmp fromuser=train touser=neurondev Import: Release 10.2.0.1.0 - Production on Fri Jan 2 15:23:23 2009 Copyright (c) 1982, 2005, Oracle. All rights reserved. Connected to: Oracle Database 10g Express Edition Release 10.2.0.1.0 - Production Export file created by EXPORT:V10.02.01 via conventional path Warning: the objects were exported by TRAIN, not by you import done in US7ASCII character set and AL16UTF16 NCHAR character set import server uses AL32UTF8 character set (possible charset conversion) IMP-00007: must be a DBA to import objects to another user's account IMP-00000: Import terminated unsuccessfully oracle@linuxbox:~>
Kita membutuhkan user dengan grant DBA jika ingin melakukan hal diatas.
oracle@linuxbox:~> imp system@xe file=/home/alam/train.dmp fromuser=train touser=neurondev Import: Release 10.2.0.1.0 - Production on Fri Jan 2 15:24:55 2009 Copyright (c) 1982, 2005, Oracle. All rights reserved. Password: Connected to: Oracle Database 10g Express Edition Release 10.2.0.1.0 - Production Export file created by EXPORT:V10.02.01 via conventional path Warning: the objects were exported by TRAIN, not by you import done in US7ASCII character set and AL16UTF16 NCHAR character set import server uses AL32UTF8 character set (possible charset conversion) . importing TRAIN's objects into NEURONDEV . . importing table "TRAIN" 2 rows imported Import terminated successfully without warnings. oracle@linuxbox:~>
Semoga bermanfaat and have a lot of fun!




