通过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调用的脚本中是不需要的。