使用CRONTAB呼叫shell指令碼執行EXP

kingsql發表於2014-09-27

透過SHELL指令碼執行全庫匯出是很常見的。但是如果將這個SHELL指令碼透過CRONTAB定時呼叫,則需要修改很多地方。這裡簡單記錄一下。


由於對UNIX系統不是很熟悉。將一個已經可以執行的csh指令碼,改成一個可以在CRONTAB中呼叫的bash指令碼,花了我足足大半天的時間。又用了不少時間將整個CRONTAB環境從測試的LINUX環境移植到UNIX環境。

下面是根據Tom的expert one-on-one oracle上程式碼稍加修改後的初始版本:

#!/bin/csh -f

# Set this to the userid you want to perform the export as I always use OPS$ (os
# authenticated) accounts for all jobs that will be run in the background. In that
# way a password never appears in a script file or in the ps output.
setenv UID system/systempassword@bjdb01

# This is the name of the export file. SPLIT will use this to name the pieces of
# the compressed DMP file.
setenv FN bjdb01_full_`date +%y%m%d`.dmp

# This is the name of the named pipe we will use.
setenv PIPE /tmp/exp_tmp.dmp

# Here I limit the size of the compressed files to 500 MG each. Anything less
# than 2 GB would be fine.

# This is what we are going to export. By default I am doing a full database
# export.
setenv EXPORT_WHAT "full=y COMPRESS=n"


# This is where the export will go to.
cd /data/exp

# Clear out the last export.
#rm expbjdb01.log export.test exp.*.dmp* $PIPE

# Create the named pipe.
mknod $PIPE p

# Write the datetime to the log file.
date > expbjdb01.log

# Start a gzip process in the background. Gzip will read the pipe and put the
# compressed data out to split. Split will then create 500 MB files out of the
# input data adding .aa, .ab, .ac, .ad, ... file extensions to the template name
# found in $FN.
(gzip < $PIPE) > $FN.gz &

# Now, start up export. The Gzip above is waiting for export to start filling the
# pipe up.
exp userid=$UID buffer=204800000 file=$PIPE $EXPORT_WHAT >>& expbjdb01.log
date >> expbjdb01.log

# Now the export is done, this is how to IMP. We need to sort the filenames and
# then simply cat their contents into gunzip. We write that into the pipe. IMP
# will then read that pipe and write what it would do to stderr. The >>& in the
# csh redirects both stdout and stderr for us.

#date > export.test
#gunzip $FN.gz > $PIPE &
#imp userid=$UID file=$PIPE show=y full=y >>& export.test
#date >> export.test

# Clean up the pipe, we don't need it anymore.
rm -f $PIPE

這個是我修改以後可以被CRONTAB呼叫的指令碼:

ORACLE_HOME=/u1/oracle/product/9.2.0;
export ORACLE_HOME;
PATH=$ORACLE_HOME/bin:$ORACLE_HOME/Apache/Apache/bin:$PATH;
export PATH;
NLS_LANG='SIMPLIFIED CHINESE_CHINA.ZHS16GBK';
export NLS_LANG;
USERID=system/systempassword@bjdb01;
export USERID;
FN=/data/exp/bjdb01_full_`date +%y%m%d`.dmp;
export FN;
PIPE=/tmp/exp_tmp.dmp;
export PIPE;
EXPORT_WHAT="full=y COMPRESS=n";
export EXPORT_WHAT;
cd /data/exp;
rm expbjdb01.log;
/usr/sbin/mknod $PIPE p;
date > expbjdb01.log;
(gzip < $PIPE) > $FN.gz &
exp userid=$USERID buffer=204800000 file=$PIPE $EXPORT_WHAT >> expbjdb01.log 2>> expbjdb01.log;
date >> expbjdb01.log;
rm -f $PIPE
~

主要注意兩個地方:

透過CRONTAB呼叫和當前Oracle使用者呼叫並不一樣,透過CRONTAB呼叫不會包含當前使用者中的各種環境變數的設定。因此必須開始的就設定好ORACLE_HOME等環境變數;

在每條命令後都加上分號,而這在非CRONTAB呼叫的指令碼中是不需要的。

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/28389881/viewspace-1282547/,如需轉載,請註明出處,否則將追究法律責任。

相關文章