磨屏终极备份脚本

3138阅读 0评论2012-02-16 linux_kaige
分类:Oracle

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



上一篇:ftp传输模式
下一篇:关于cu网中人