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

简介:     对DBA而言,经常碰到升级数据库或是apply patch,以及有些时候需要运行大量的脚本。对于这些操作我们希望现在在屏幕同时又输出的文件以备后续查询过程中曾经丢失的步骤或错误。

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

 

1、script命令描述

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命令用法示例

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


更多参考

有关Oracle RAC请参考
     使用crs_setperm修改RAC资源的所有者及权限
     使用crs_profile管理RAC资源配置文件
     RAC 数据库的启动与关闭
     再说 Oracle RAC services
     Services in Oracle Database 10g
     Migrate datbase from single instance to Oracle RAC
     Oracle RAC 连接到指定实例
     Oracle RAC 负载均衡测试(结合服务器端与客户端)
     Oracle RAC 服务器端连接负载均衡(Load Balance)
     Oracle RAC 客户端连接负载均衡(Load Balance)
     ORACLE RAC 下非缺省端口监听配置(listener.ora tnsnames.ora)
     ORACLE RAC 监听配置 (listener.ora tnsnames.ora)
     配置 RAC 负载均衡与故障转移
     CRS-1006 , CRS-0215 故障一例 
     基于Linux (RHEL 5.5) 安装Oracle 10g RAC
     使用 runcluvfy 校验Oracle RAC安装环境

有关Oracle 网络配置相关基础以及概念性的问题请参考:
     配置非默认端口的动态服务注册
     配置sqlnet.ora限制IP访问Oracle
     Oracle 监听器日志配置与管理
     设置 Oracle 监听器密码(LISTENER)
     配置ORACLE 客户端连接到数据库

有关基于用户管理的备份和备份恢复的概念请参考
     Oracle 冷备份
     Oracle 热备份
     Oracle 备份恢复概念
     Oracle 实例恢复
     Oracle 基于用户管理恢复的处理
     SYSTEM 表空间管理及备份恢复
     SYSAUX表空间管理及恢复
     Oracle 基于备份控制文件的恢复(unsing backup controlfile)

有关RMAN的备份恢复与管理请参考
     RMAN 概述及其体系结构
     RMAN 配置、监控与管理
     RMAN 备份详解
     RMAN 还原与恢复
     RMAN catalog 的创建和使用
     基于catalog 创建RMAN存储脚本
     基于catalog 的RMAN 备份与恢复
     RMAN 备份路径困惑
     使用RMAN实现异机备份恢复(WIN平台)
     使用RMAN迁移文件系统数据库到ASM
     linux 下RMAN备份shell脚本
     使用RMAN迁移数据库到异机

有关ORACLE体系结构请参考
     Oracle 表空间与数据文件
     Oracle 密码文件
     Oracle 参数文件
     Oracle 联机重做日志文件(ONLINE LOG FILE)
     Oracle 控制文件(CONTROLFILE)
     Oracle 归档日志
     Oracle 回滚(ROLLBACK)和撤销(UNDO)
     Oracle 数据库实例启动关闭过程
     Oracle 10g SGA 的自动化管理
     Oracle 实例和Oracle数据库(Oracle体系结构) 

相关实践学习
部署高可用架构
本场景主要介绍如何使用云服务器ECS、负载均衡SLB、云数据库RDS和数据传输服务产品来部署多可用区高可用架构。
负载均衡入门与产品使用指南
负载均衡(Server Load Balancer)是对多台云服务器进行流量分发的负载均衡服务,可以通过流量分发扩展应用系统对外的服务能力,通过消除单点故障提升应用系统的可用性。 本课程主要介绍负载均衡的相关技术以及阿里云负载均衡产品的使用方法。
目录
相关文章
|
9天前
|
Web App开发 Linux 网络安全
工作中常用到的Linux命令
工作中常用到的Linux命令
|
9天前
|
Web App开发 Java Linux
Linux之Shell基本命令篇
Linux之Shell基本命令篇
Linux之Shell基本命令篇
|
6天前
|
NoSQL Linux Shell
常用的 Linux 命令
常用的 Linux 命令
28 9
|
1天前
|
域名解析 网络协议 Linux
Linux 中的 Nslookup 命令怎么使用?
【4月更文挑战第12天】
12 6
Linux 中的 Nslookup 命令怎么使用?
|
2天前
|
运维 网络协议 Unix
18.系统知识-Linux常用命令
18.系统知识-Linux常用命令
|
2天前
|
网络协议 Ubuntu Linux
Linux 下 TFTP 服务搭建及 U-Boot 中使用 tftp 命令实现文件下载
Linux 下 TFTP 服务搭建及 U-Boot 中使用 tftp 命令实现文件下载
|
2天前
|
Linux Go
Linux命令Top 100驱动人生! 面试必备
探索Linux命令不再迷茫!本文分10部分详解20个基础命令,带你由浅入深掌握文件、目录管理和文本处理。 [1]: <https://cloud.tencent.com/developer/article/2396114> [2]: <https://pan.quark.cn/s/865a0bbd5720> [3]: <https://yv4kfv1n3j.feishu.cn/docx/MRyxdaqz8ow5RjxyL1ucrvOYnnH>
46 0
|
5天前
|
缓存 运维 监控
Linux系统监控利器:探索常用命令及数据保存技巧
Linux系统监控利器:探索常用命令及数据保存技巧
23 4
Linux系统监控利器:探索常用命令及数据保存技巧
|
8天前
|
Linux 索引
linux 文件查找 和文件管理常用命令
linux 文件查找 和文件管理常用命令
18 0
|
8天前
|
负载均衡 Java 关系型数据库
linux 下amoeba实现数据库的负载均衡
linux 下amoeba实现数据库的负载均衡
8 1