Neuronworks Blog, WebMethods, Oracle, Bea, Java Dev2Dev, Arch2Arch Neuronworks Make IT Easy, Let’s Play the Game » backup

Archive

Posts Tagged ‘backup’

Membackup Schema Oracle Secara Otomatis

January 5th, 2009

The Intro

Sebelumnya kita sudah mencoba membackup schema oracle menggunakan exp. Kita bisa memanfaatkannya untuk membuat sebuah sistem penjadwalan yang secara otomatis membackup schema tersebut pada waktu yang telah ditentukan. Tool yang digunakan selain Shell dan exp juga crontab. Crontab merupakan aplikasi penjadwalan di mesin Unix dan Linux. Crontab bekerja sebagai daemon atau service cron yang setiap waktu akan mengecek apakah ada job atau tugas yang harus dilakukan pada waktu itu. Crontab menyimpan entri untuk setiap penjadwalan seperti ini:

* * * * * /home/alam/someScript.sh

Kolom pertama menunjukan menit dimana entri tersebut akan dijalankan. Kolom kedua untuk jam, kolom ketiga untuk tanggal, kolom keempat untuk bulan, kolom kelima untuk untuk hari, dan kolom terakhir menunjukan perintah apa yang akan dijalankan. Dari contoh diatas berarti setiap menit, setiap hari, setiap bulan cron akan menjalankan script tersebut.
Pertama-tama kita buat dulu script sederhana, train-backup.sh yang berfungsi untuk membackup schema tersebut. Script tersebut akan membackup schema-nya di directory yang sudah ditentukan dengan nama file yang berbeda berdasarkan kapan schema tersebut dibackup.

The Script

#!/bin/sh
#script to backup oracle schema based on time when it saved
#initializing variables
export PATH=$PATH:/app/oracle/product/10.2/bin
export ORACLE_BASE=/app/oracle
export ORACLE_HOME=/app/oracle/product/10.2
export ORACLE_SID=neuron
export BASEDIR=$HOME/train-backup
export DTM=`date +%Y%m%d-%H%M%S`
export UNAME=train
export PASSWD=train
export FILENAME=train
 
# command section
cd $BASEDIR
exp $UNAME/$PASSWD file=$FILENAME-$DTM.dmp
 
exit

Jangan lupa beri execute permission dengan:

alam@NeuronServer:~> chmod +x train-backup.sh

Script-nya cukup sederhana kan? Yang perlu dilakukan hanyalah inisialisasi variabel:

export PATH=$PATH:/app/oracle/product/10.2/bin
export ORACLE_BASE=/app/oracle
export ORACLE_HOME=/app/oracle/product/10.2
export ORACLE_SID=neuron
export BASEDIR=$HOME/train-backup
export DTM=`date +%Y%m%d-%H%M%S`
export UNAME=train
export PASSWD=train
export FILENAME=train

dan perintah backup itu sendiri:

cd $BASEDIR
exp $UNAME/$PASSWD file=$FILENAME-$DTM.dmp

Untuk sedikit memahami dasar dari shell scripting silakan lihat kembali postingan sebelumnya.
Yang mungkin perlu diperhatikan adalah variabel DTM yang akan menyimpan format waktu untuk digunakan di nama filenya. Variabel DTM menyimpan hasil output dari perintah date yang akan menampilkan waktu saat itu dengan format YYYYMMDD-HH:MI:SS. Untuk lebih jelasnya coba jalankan perintah date tersebut di command line:

alam@NeuronServer:~> date +%Y%m%d-%H%M%S
20090105-201620

Variabel ini akan kita tambahkan di nama file backup schema-nya untuk membedakan satu file dengan yang lain.

The Cron

Sekarang kita masuk ke bagian cron-nya sendiri. Kita tentukan kapan saja backup akan dilakukan. Misal kita ingin backup dilakukan setiap jam 12 malam setiap hari Senin dan Jum’at. Entri crontab-nya seperti ini:

* 00 * * 1,5 /home/alam/train-backup.sh

Untuk memasukkan entri diatas gunakan perintah crontab -e. Editor vi akan terbuka. Untuk masuk ke mode insert tekan tombol i. Ketikkan atau paste entri yang tadi kita buat. Untuk menyimpannya tekan tombol Esc, dilanjutkan dengan tombol :, dilanjutkan dengan tombol x, yang berarti menyimpan sekaligus keluar dari vi.
Untuk melihat apakah penjadwalan tersebut sudah masuk gunakan perintah crontab -l:

alam@NeuronServer:~> crontab -l
# DO NOT EDIT THIS FILE - edit the master and reinstall.
# (/tmp/crontab.XXXXLhlmgJ installed on Mon Jan  5 20:28:21 2009)
# (Cron version V5.0 -- $Id: crontab.c,v 1.12 2004/01/23 18:56:42 vixie Exp $)
* 00 * * 1,5 /home/alam/train-backup.sh

The… End?

Selesai! Untuk melihat berhasil atau tidaknya kita lihat lagi besok apakah file backup schema-nya sudah ada atau tidak. :)

The Update

Coba kita lihat hasilnya:

alam@NeuronServer:~> ll train-backup
total 0

Lho kok tidak ada? Ayo kita cek script-nya dengan cara dijalankan langsung:

alam@NeuronServer:~> ./train-backup.sh 
./train-backup.sh: line 16: exp: command not found

Hm? PATH-nya kan sudah masuk mestinya? Coba kita export dan tes lagi satu-satu variabelnya:

alam@NeuronServer:~> export ORACLE_BASE=/app/oracle
alam@NeuronServer:~> cd $ORACLE_BASE
alam@NeuronServer:/app/oracle> export ORACLE_HOME=/app/oracle/product/10.2
alam@NeuronServer:/app/oracle> cd $ORACLE_HOME
-bash: cd: /app/oracle/product/10.2: Permission denied

Ini dia masalahnya. User alam tidak punya permission untuk masuk ke direktori /app/oracle/product:

alam@NeuronServer:/app/oracle> ll /app/oracle/
total 20
drwxr-x--- 3 oracle oinstall 4096 2008-11-22 17:08 admin
drwxr-x--- 3 oracle oinstall 4096 2008-11-22 17:09 flash_recovery_area
drwxr-x--- 3 oracle oinstall 4096 2008-11-22 17:08 oradata
drwxrwx--- 6 oracle oinstall 4096 2008-11-22 17:07 oraInventory
drwxrwx--- 3 oracle oinstall 4096 2008-11-22 00:07 product

Jadi bagaimana solusinya? Kita jalankan script tersebut sebagai user yang punya permission untuk mengakses direktori tersebut, yaitu oracle:

oracle@NeuronServer:~> mkdir train-backup
oracle@NeuronServer:~> /home/alam/train-backup.sh 
 
Export: Release 10.2.0.1.0 - Production on Tue Jan 6 14:59:42 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
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@NeuronServer:~> ll train-backup
total 16
-rw-r--r-- 1 oracle oinstall 16384 2009-01-06 14:59 train-20090106-145942.dmp

Jadi masukkan cron job-nya yang tadi untuk user oracle dengan crontab -e juga.
Dan crontab yang di user alam dihapus saja:

alam@NeuronServer:~> crontab -r
alam@NeuronServer:~> crontab -l
no crontab for alam

Sekarang sudah pasti berhasil.

Semoga bermanfaat and have a lot of fun!

Linux, Oracle , , , , ,

Backup dan Restore Schema Oracle Menggunakan exp dan imp

December 31st, 2008

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!

Oracle , ,