yangtingkun
===========================================================
使用CRONTAB调用shell脚本执行EXP
===========================================================

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

yangtingkun 发表于:2006.01.25 23:33 ::分类: ( 操作系统 ) ::阅读:(4767次) :: 评论 (9)
居然用的cshell [回复]

Solaris 平台?

Fenng 评论于: 2006.01.26 11:19
[回复]

平台是solaris。忘了写了。
开始的脚本是根据TOM给出的修改的。

yangtingkun 评论于: 2006.01.26 13:47
其实不需要这么复杂 [回复]

如果你的平台支持source命令,在脚本前加一行类似:
source /用户主目录/.bash_profile
该命令作用是强迫读取当前用户Shell配置文件,并初始化变量。这样一来,只要你能在普通Shell下使用的环境变量就可以在crontab环境里指令。
其中.bash_profile是linux下的一个用户配置,其他系统都有一个类似的.开头的配置文件。
我自己就是这么用的。比如,我的:
source /home/starsail/.bash_profile
cd $ORACLE_HOME/
sqlplus ...(sqlplus也可以执行了)

starsail 评论于: 2006.04.13 11:30
[回复]

是个不错的方法

yangtingkun 评论于: 2006.04.13 13:27
re: 使用CRONTAB调用shell脚本执行EXP [回复]

不知这个脚本文件的权限需要怎么设置才可被调用?

hacker 评论于: 2006.06.12 15:41
re: 使用CRONTAB调用shell脚本执行EXP [回复]

EXP_FULL_DATABASE

yangtingkun 评论于: 2006.06.12 17:05
需要用分号么? [回复]

我在solaris9上测试 脚本里面的命令并不需要加分号

qq 评论于: 2006.10.19 18:12
re: 使用CRONTAB调用shell脚本执行EXP [回复]

我的测试版本是solaris8,9没有测试过

yangtingkun 评论于: 2006.10.19 21:24
re: 使用CRONTAB调用shell脚本执行EXP [回复]

我的那台HPUX服务器是支持source命令的,但在crontab中不支持,提示source: not found

用which source竟然也找不到source命令?奇怪!

xiao_fang 评论于: 2007.04.30 15:12

发表评论
标题

在此添加评论
表情符号: smile laughing tongue angry crying sad wassat wink

称呼

邮箱地址(可选)

个人主页(可选)

 authimage


切换风格
新闻聚合
博客日历
文章归档...
最新发表...
最新评论...
最多阅读文章...
最多评论文章...
博客统计...
Blog信息
网站链接...