手机版
你好,游客 登录 注册 搜索
背景:
阅读新闻

Linux/Unix shell 脚本跨服务器跨实例执行SQL

[日期:2013-08-23] 来源:Linux社区  作者:robinson [字体:大 中 小]

2、使用tnsnames方式轮巡多服务器及实例

  #下面是通过tnsnames方式进行轮巡的shell脚本,也就是仅仅需要从远程服务器获取/etc/oratab下的所有实例即可
  #需要注意的:
  # a. 确保本地环境存在到所有远程服务器的tnsnames连接字符串,如果没有也没有关系,会收到错误
  # b. 需要所有被轮巡数据库的用户及密码并具有相应的权限
  # c. 如果所需要执行的sql属于比较复杂的情形,我们可以将sql语句写入到一个单独的文件,然后调用
 
  oracle@linux1:~> more query_multi_inst_tns.sh
  # ------------------------------------------------------------------------------------+
  # Script Name: query_multi_inst_tns.sh                                                |
  # Desc:  This script login to different remote host where define in known_host file,  |
  #        after that look through oratab and execute SQL for each instance            |
  #        in local machine by tnsnames.                                                |
  # Req:  Configure a secure shell by ssh-keygen to all remote host                    |
  #        Add Oracle Net string to local tnsnames.ora for each remote instance        |
  # Author : Robinson                                                                  |
  # Blog  : http://www.linuxidc.com                                        |
  # ------------------------------------------------------------------------------------+
  #!/bin/bash
 
  # --------------------------------------------
  # Set environment vairable and define variable
  # --------------------------------------------
 
  if [ -f ~/.bash_profile ]; then
      . ~/.bash_profile
  fi
 
  ORATAB=/etc/oratab
  RHOST=~/.ssh/known_hosts
  LOGFILE=/users/robin/dba_scripts/custom/log/query_multi_inst.log
 
  # -------------------------------
  # take a loop in each hostname
  # -------------------------------
 
  {
  for host in `cat $RHOST | awk '{print $1}'`
  do
      echo "************************************"
      echo "Current host is $host."
      echo "************************************"
      echo ""
 
      # ---------------------------
      # take a loop in ORATAB file
      # ---------------------------
 
      for db in `ssh $host cat $ORATAB |egrep ':N|:Y'|grep -v \* | grep -v \# |cut -d ":" -f1`
      do
          echo "------------------------------------"
          echo "Current database is $db.            "
          echo "------------------------------------"
          $ORACLE_HOME/bin/sqlplus -S usr/passwd@$db <<EOF
          col name format a30
          col value format a20
          select name,value from v\$parameter where name='open_cursors';
          exit
  EOF
      done
  done
  }|tee -a ${LOGFILE} 2>&1
  exit

 

linux
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款