前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Linux script 命令记录(数据库)操作步骤

Linux script 命令记录(数据库)操作步骤

作者头像
Leshami
发布2018-08-14 10:10:44
1.4K0
发布2018-08-14 10:10:44
举报
文章被收录于专栏:乐沙弥的世界

    对DBA而言,经常碰到升级数据库或是apply patch,以及有些时候需要运行大量的脚本。对于这些操作我们希望现在在屏幕同时又输出的文件以备后续查询过程中曾经丢失的步骤或错误。Linux下的script命令就是解决这个问题的好帮手。

1、script命令描述

代码语言:javascript
复制
script命令会记录所有的操作到文件同时在屏幕上输出,直到终止登陆的会话,或使用CRTL+D,或使用exit退出则停止记录。
这个命令对于数据库的升级或是重要设置的情形下使用可以用于后续查询操作成功或失败。
用法: $ script [upgrade.log]
如果未指定日志文件名的情形,自动生成日志文件名为typescript。
如果需要输出到已经存在的日志文件,则使用 -a 参数,再接已经存在日志文件名。
如果需要在输出到日志文件的同时,也可以查看日志文件的内容,可以使用 -f 参数。

# script 帮助描述
robin@SZDB:~> man script
NAME
     script - make typescript of terminal session

SYNOPSIS
     script [-a] [-c COMMAND] [-f] [-q] [-t] [file]

DESCRIPTION
     Script makes a typescript of everything printed on your terminal.  It is useful for students who need a hardcopy record of
     an interactive session as proof of an assignment, as the typescript file can be printed out later with lpr(1).

     If the argument file is given, script saves all dialogue in file.  If no file name is given, the typescript is saved in the
     file typescript.

     Options:

     -a      Append the output to file or typescript, retaining the prior contents.

     -c COMMAND
             Run the COMMAND rather than an interactive shell.  This makes it easy for a script to capture the output of a pro-
             gram that behaves differently when its stdout is not a tty.

     -f      Flush output after each write. This is nice for telecooperation: One person does `mkfifo foo; script -f foo' and
             another can supervise real-time what is being done using `cat foo'.

     -q      Be quiet.

     -t      Output timeing data to standard error. This data contains two fields, separated by a space. The first field indi-
             cates how much time elapsed since the previous output. The second field indicates how many characters were output
             this time. This information can be used to replay typescripts with realistic typing and output delays.

2、script命令用法示例

代码语言:javascript
复制
a、script命令后接日志文件名
robin@SZDB:~> script test_logfile.log        #启用script
Script started, file is test_logfile.log

  .................#中间操作省略
  
robin@SZDB:~> exit    #停止script
exit
Script done, file is test_logfile.log

#查看刚刚生成的日志文件
robin@SZDB:~> more test_logfile.log 
Script started on Fri 26 Apr 2013 05:45:23 PM CST
robin@SZDB:~> sid  
bash: sid: command not found
robin@SZDB:~> . ~/.bash_profile
robin@SZDB:~> sid
ORA_CRS_HOME=/opt/oracle/product/10gR2/crs
ORACLE_PATH=.:/users/robin/dba_scripts/custom/sql
ORA_ASM_HOME=/opt/oracle/product/10gR2/asm
ORACLE_SID=SYBO2SZ
ORACLE_BASE=/users/oracle
ORACLE_HOME=/users/oracle/OraHome10g
robin@SZDB:~> sql     

SQL*Plus: Release 10.2.0.3.0 - Production on Fri Apr 26 17:46:12 2013

Copyright (c) 1982, 2006, Oracle.  All Rights Reserved.

Error accessing PRODUCT_USER_PROFILE
Warning:  Product user profile information not loaded!
You may need to run PUPBLD.SQL as SYSTEM

Connected to:
Oracle Database 10g Release 10.2.0.3.0 - 64bit Production

goex_admin@SYBO2SZ> alter session set current_schema=scott;

Session altered.

goex_admin@SYBO2SZ> select * from dept;

    DEPTNO DNAME          LOC
---------- -------------- -------------
        10 ACCOUNTING     NEW YORK
        20 RESEARCH       DALLAS
        30 SALES          CHICAGO
        40 OPERATIONS     BOSTON

goex_admin@SYBO2SZ> exit
Disconnected from Oracle Database 10g Release 10.2.0.3.0 - 64bit Production
robin@SZDB:~> exit
exit

Script done on Fri 26 Apr 2013 05:46:32 PM CST  

b、script 命下直接跟脚本名的示例
robin@SZDB:~> script -c retval.sh
Script started, file is typescript
7788 SCOTT ANALYST 7566 19870419 00:00:00 3100 20
Script done, file is typescript

#被执行的shell脚本的内容
# Author : Robinson
# Blog   : http://blog.csdn.ner/robinson_0612

robin@SZDB:~> more ~/dba_scripts/custom/bin/retval.sh
#!/bin/bash
RETVAL=`sqlplus -silent scott/tiger <<EOF
SET PAGESIZE 0 FEEDBACK OFF VERIFY OFF HEADING OFF ECHO OFF
SELECT * FROM emp WHERE ename='SCOTT';
EXIT;
EOF`
if [ -z "$RETVAL" ]; then
  echo "No rows returned from database"
  exit 0
else
  echo $RETVAL
fi
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2013年04月28日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
数据库
云数据库为企业提供了完善的关系型数据库、非关系型数据库、分析型数据库和数据库生态工具。您可以通过产品选择和组合搭建,轻松实现高可靠、高可用性、高性能等数据库需求。云数据库服务也可大幅减少您的运维工作量,更专注于业务发展,让企业一站式享受数据上云及分布式架构的技术红利!
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档