#修订:查看日志,备份脚本执行报错,经检查,是因为缺少环境变量。日期:23:05 2012/2/19
记录所有操作日志,这是一个管理员做事的基本要求。调试脚本的时候,注意屏幕输出的内容,或者完善寻混语句,打印执行过程,多用“echo”“printf”便于纠错。
- [oracle@CMS2 data]$ cat dbbackyangkai.sh
- #!/bin/bash
- #author yangkai
- #time 2012-2-13
- cd /home/oracle/data/backup/
- #方便下面的if判断
- ttime=`date +%Y-%m-%d`
- #ddir=/home/oracle/data/backup
- #注意我变量的命名规则,上面的ddir最后面缺少"/",在描述文件路径的时候,不能缺,多了无所谓的,如/home//oracle/
- ddir=/home/oracle/data/backup/
- exp magicscreen/vnet_magic_screen file=$ddir/$ttime-magic.dmp
- exp zixunuser/zixunpassword file=$ddir/$ttime-zixun.dmp
- exp cscms/cscms file=$ddir/$ttime-cms.dmp
- exp lifehotline/lifehotline file=$ddir/$ttime-lifehotline.dmp
- ffile=$(ls ${ddir}${ttime}*)
- #导出的dmp文件赋予新变量
- tar -czvf dbmtouch${ttime}.tar.gz $ffile
- #压缩dmp文件
- #if [ -f "$dbmtouch${ttime}.tar.gz" ];then
- # only duo yi ge $.
- if [ -f "dbmtouch${ttime}.tar.gz" ];then
- echo "cun zai dbmtouch${ttime}.tar.gz,and delete the file of dmp."
- #rm -f ${ffile}
- # this action can not rm file!you can hava a try to "echo ${ffile}"
- rm -f ${ddir}${ttime}*
- else
- #use "else",that may be perfect!
- echo "can not find the dbmtouch${ttime}.tar.gz,db backup fail."
- fi
- HOST=192.168.40.240
- FTP_USER=ftpbak
- FTP_PD=zhang
- ############ftp###################
- ftp -i -n -v << !
- open $HOST
- user $FTP_USER $FTP_PD
- binary
- lcd /home/oracle/data/backup
- put dbmtouch${ttime}.tar.gz
- bye
- !
- echo "$(date) oracle db backup yangkai."
- #方便我查询下面重定向的日志,dbback.log。
- #ftp upload can not use ascii,must use binary.
- [oracle@CMS2 data]$ crontab -l
- 00 01 * * * /home/oracle/data/dbbackyangkai.sh 1>>/home/oracle/data/dbback.log 2>&1
- [oracle@CMS2 data]$
上面的脚本手动执行没任何问题,放到crontab里面就不行,经过我对日志的分析,发现是因为当前用户的系统环境变量未传递过来:
- [oracle@cms-db ~]$ cat data/dbback.log
- /home/oracle/important/dbbackyangkai.sh: line 6: exp: command not found
- /home/oracle/important/dbbackyangkai.sh: line 7: exp: command not found
- /home/oracle/important/dbbackyangkai.sh: line 8: exp: command not found
- /home/oracle/important/dbbackyangkai.sh: line 9: exp: command not found
- ls: /home/oracle/data/backup/2012-02-19*: No such file or directory
- tar: Cowardly refusing to create an empty archive
- Try `tar --help' or `tar --usage' for more information.
- can not find the dbmtouch2012-02-19.tar.gz,db backup fail.
- Connected to 192.168.40.240 (192.168.40.240).
- 220 (vsFTPd 2.0.5)
- 331 Please specify the password.
- 230 Login successful.
- 200 Switching to Binary mode.
- local: dbmtouch2012-02-19.tar.gz: No such file or directory
- Local directory now /home/oracle/data/backup
- local: dbmtouch2012-02-19.tar.gz remote: dbmtouch2012-02-19.tar.gz
- 221 Goodbye.
- Sun Feb 19 01:00:01 CST 2012 oracle db backup complete yk.
- [oracle@cms-db ~]$
cu网中人曾写过shell的几种执行方式,但是其实对于传递这些环境变量,无法通过修改执行方式而让脚本在crontab里面执行,故只有在脚本中重新声明一下环境变量,将~/.bash_profile里面的关于Oracle的变量都复制在脚本里的前面:
- [oracle@cms-db important]$ cat ~/.bash_profile
- # .bash_profile
- # Get the aliases and functions
- if [ -f ~/.bashrc ]; then
- . ~/.bashrc
- fi
- # User specific environment and startup programs
- PATH=$PATH:$HOME/bin
- export PATH
- TMP=/tmp; export TMP
- TMPDIR=$TMP; export TMPDIR
- ORACLE_BASE=/u01/app/oracle; export ORACLE_BASE
- ORACLE_HOME=$ORACLE_BASE/product/10.2/db1; export ORACLE_HOME
- ORACLE_SID=cmsdb; export ORACLE_SID
- ORACLE_TERM=xterm; export ORACLE_TERM
- PATH=/usr/sbin:$PATH; export PATH
- PATH=$ORACLE_HOME/bin:$PATH; export PATH
- NLS_DATE_FORMAT="yyyy-mm-dd HH24:MI:SS"; export NLS_DATE_FORMAT
- NLS_LANG=AMERICAN_AMERICA.ZHS16GBK;export NLS_LANG
- LD_LIBRARY_PATH=$ORACLE_HOME/lib:/lib:/usr/lib; export LD_LIBRARY_PATH
- CLASSPATH=$ORACLE_HOME/JRE:$ORACLE_HOME/jlib:$ORACLE_HOME/rdbms/jlib; export CLASSPATH
- alias sqlplus="rlwrap sqlplus"
- alias rman="rlwrap rman"
- alias lsnrctl="rlwrap lsnrctl"
- alias bbed="rlwrap bbed"
- if [ $USER = "oracle" ]; then
- if [ $SHELL = "/bin/ksh" ]; then
- ulimit -p 16384
- ulimit -n 65536
- else
- ulimit -u 16384 -n 65536
- fi
- fi
- [oracle@cms-db important]$
最终效果如下:
- [oracle@CMS2 data]$ cat dbbackyangkai.sh
- #!/bin/bash
- #author yangkai
- #time 2012-2-13
- PATH=$PATH:$HOME/bin
- export PATH
- TMP=/tmp; export TMP
- TMPDIR=$TMP; export TMPDIR
- ORACLE_BASE=/u01/app/oracle; export ORACLE_BASE
- ORACLE_HOME=$ORACLE_BASE/product/10.2/db1; export ORACLE_HOME
- ORACLE_SID=cmsdb; export ORACLE_SID
- ORACLE_TERM=xterm; export ORACLE_TERM
- PATH=/usr/sbin:$PATH; export PATH
- PATH=$ORACLE_HOME/bin:$PATH; export PATH
- NLS_DATE_FORMAT="yyyy-mm-dd HH24:MI:SS"; export NLS_DATE_FORMAT
- NLS_LANG=AMERICAN_AMERICA.ZHS16GBK;export NLS_LANG
- LD_LIBRARY_PATH=$ORACLE_HOME/lib:/lib:/usr/lib; export LD_LIBRARY_PATH
- CLASSPATH=$ORACLE_HOME/JRE:$ORACLE_HOME/jlib:$ORACLE_HOME/rdbms/jlib; export CLASSPATH
- cd /home/oracle/data/backup/
- #方便下面的if判断
- ttime=`date +%Y-%m-%d`
- #ddir=/home/oracle/data/backup
- #注意我变量的命名规则,上面的ddir最后面缺少"/",在描述文件路径的时候,不能缺,多了无所谓的,如/home//oracle/
- ddir=/home/oracle/data/backup/
- exp magicscreen/vnet_magic_screen file=$ddir/$ttime-magic.dmp
- exp zixunuser/zixunpassword file=$ddir/$ttime-zixun.dmp
- exp cscms/cscms file=$ddir/$ttime-cms.dmp
- exp lifehotline/lifehotline file=$ddir/$ttime-lifehotline.dmp
- ffile=$(ls ${ddir}${ttime}*)
- #导出的dmp文件赋予新变量
- tar -czvf dbmtouch${ttime}.tar.gz $ffile
- #压缩dmp文件
- #if [ -f "$dbmtouch${ttime}.tar.gz" ];then
- # only duo yi ge $.
- if [ -f "dbmtouch${ttime}.tar.gz" ];then
- echo "cun zai dbmtouch${ttime}.tar.gz,and delete the file of dmp."
- #rm -f ${ffile}
- # this action can not rm file!you can hava a try to "echo ${ffile}"
- rm -f ${ddir}${ttime}*
- else
- #use "else",that may be perfect!
- echo "can not find the dbmtouch${ttime}.tar.gz,db backup fail."
- fi
- HOST=192.168.40.240
- FTP_USER=ftpbak
- FTP_PD=zhang
- ############ftp###################
- ftp -i -n -v << !
- open $HOST
- user $FTP_USER $FTP_PD
- binary
- lcd /home/oracle/data/backup
- put dbmtouch${ttime}.tar.gz
- bye
- !
- echo "$(date) oracle db backup yangkai."
- #方便我查询下面重定向的日志,dbback.log。
- #ftp upload can not use ascii,must use binary.
- [oracle@CMS2 data]$ crontab -l
- 00 01 * * * /home/oracle/data/dbbackyangkai.sh 1>>/home/oracle/data/dbback.log 2>&1
- [oracle@CMS2 data]$