Linux script命令记录(数据库)操作步骤
Linux script命令记录(数据库)操作步骤
对DBA而言,经常碰到升级数据库或是apply patch,以及有些时候需要运行大量的脚本。对于这些操作我们希望现在在屏幕同时又输出的文件以备后续查询过程中曾经丢失的步骤或错误。Linux下的script命令就是解决这个问题的好帮手。
1、script命令描述
[python]
script命令会记录所有的操作到文件同时在屏幕上输出,直到终止登陆的会话,或使用CRTL+D,或使用exit退出则停止记录。
这个命令对于数据库的升级或是重要设置的情形下使用可以用于后续查询操作成功或失败。
用法: $ script [upgrade.log]
如果未指定日志文件名的情形,自动生成日志文件名为typescript。
如果需要输出到已经存在的日志文件,则使用 -a 参数,再接已经存在日志文件名。
如果需要在输出到日志文件的同时,也可以查看日志文件的内容,可以使用 -f 参数。
1 # script 帮助描述 2 robin@SZDB:~> man script 3 NAME 4 script - make typescript of terminal session 5 6 SYNOPSIS 7 script [-a] [-c COMMAND] [-f] [-q] [-t] [file] 8 9 DESCRIPTION 10 Script makes a typescript of everything printed on your terminal. It is useful for students who need a hardcopy record of 11 an interactive session as proof of an assignment, as the typescript file can be printed out later with lpr(1). 12 13 If the argument file is given, script saves all dialogue in file. If no file name is given, the typescript is saved in the 14 file typescript. 15 16 Options: 17 18 -a Append the output to file or typescript, retaining the prior contents. 19 20 -c COMMAND 21 Run the COMMAND rather than an interactive shell. This makes it easy for a script to capture the output of a pro- 22 gram that behaves differently when its stdout is not a tty. 23 24 -f Flush output after each write. This is nice for telecooperation: One person does `mkfifo foo; script -f foo' and 25 another can supervise real-time what is being done using `cat foo'. 26 27 -q Be quiet. 28 29 -t Output timeing data to standard error. This data contains two fields, separated by a space. The first field indi- 30 cates how much time elapsed since the previous output. The second field indicates how many characters were output 31 this time. This information can be used to replay typescripts with realistic typing and output delays. 32 2、script命令用法示例 33 [python] 34 a、script命令后接日志文件名 35 robin@SZDB:~> script test_logfile.log #启用script 36 Script started, file is test_logfile.log 37 38 .................#中间操作省略 39 40 robin@SZDB:~> exit #停止script 41 exit 42 Script done, file is test_logfile.log 43 44 #查看刚刚生成的日志文件 45 robin@SZDB:~> more test_logfile.log 46 Script started on Fri 26 Apr 2013 05:45:23 PM CST 47 robin@SZDB:~> sid 48 bash: sid: command not found 49 robin@SZDB:~> . ~/.bash_profile 50 robin@SZDB:~> sid 51 ORA_CRS_HOME=/opt/oracle/product/10gR2/crs 52 ORACLE_PATH=.:/users/robin/dba_scripts/custom/sql 53 ORA_ASM_HOME=/opt/oracle/product/10gR2/asm 54 ORACLE_SID=SYBO2SZ 55 ORACLE_BASE=/users/oracle 56 ORACLE_HOME=/users/oracle/OraHome10g 57 robin@SZDB:~> sql 58 59 SQL*Plus: Release 10.2.0.3.0 - Production on Fri Apr 26 17:46:12 2013 60 61 Copyright (c) 1982, 2006, Oracle. All Rights Reserved. 62 63 Error accessing PRODUCT_USER_PROFILE 64 Warning: Product user profile information not loaded! 65 You may need to run PUPBLD.SQL as SYSTEM 66 67 Connected to: 68 Oracle Database 10g Release 10.2.0.3.0 - 64bit Production 69 70 goex_admin@SYBO2SZ> alter session set current_schema=scott; 71 72 Session altered. 73 74 goex_admin@SYBO2SZ> select * from dept; 75 76 DEPTNO DNAME LOC 77 ---------- -------------- ------------- 78 10 ACCOUNTING NEW YORK 79 20 RESEARCH DALLAS 80 30 SALES CHICAGO 81 40 OPERATIONS BOSTON 82 83 goex_admin@SYBO2SZ> exit 84 Disconnected from Oracle Database 10g Release 10.2.0.3.0 - 64bit Production 85 robin@SZDB:~> exit 86 exit 87 88 Script done on Fri 26 Apr 2013 05:46:32 PM CST 89 90 b、script 命下直接跟脚本名的示例 91 robin@SZDB:~> script -c retval.sh 92 Script started, file is typescript 93 7788 SCOTT ANALYST 7566 19870419 00:00:00 3100 20 94 Script done, file is typescript 95 96 #被执行的shell脚本的内容 97 98 robin@SZDB:~> more ~/dba_scripts/custom/bin/retval.sh 99 #!/bin/bash 100 RETVAL=`sqlplus -silent scott/tiger <<EOF 101 SET PAGESIZE 0 FEEDBACK OFF VERIFY OFF HEADING OFF ECHO OFF 102 SELECT * FROM emp WHERE ename='SCOTT'; 103 EXIT; 104 EOF` 105 if [ -z "$RETVAL" ]; then 106 echo "No rows returned from database" 107 exit 0 108 else 109 echo $RETVAL 110 fi