db2编目抽取

Posted 浮沉一梦

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了db2编目抽取相关的知识,希望对你有一定的参考价值。

最近迁移DB2数据库从AIX到Linux平台:需要整理所有服务器编目,服务器有aix和linux,手动整理肯定较慢切容易出错,下面写脚本解决。

 

策略:

  1、编写脚本提取服务器编目的脚本,编目信息保存到文件。

  2、ansible分发到各台Linux服务器,aix没有ssh认证的需手动完成。

  3、ansible批量执行脚本,提取信息到文件。

  4、ansible拉取各台服务器编目信息文件到本机。

  5、本机编写脚本批处理,生成csv格式。

 

下面给出关键处理脚本:

获取编目信息到文件:

#!/bin/bash
#linux平台
db_host=`hostname`
db_ip=$(ifconfig -a | grep "inet addr" | grep -v "127.0.0.1" | awk ‘{print $2}‘ | awk -F‘:‘ ‘{print $2}‘)
who_am=`whoami`

db2 list db directory > ${db_ip}_${who_am}_${db_host}.db
db2 list node directory > ${db_ip}_${who_am}_${db_host}.node


#!/usr/bin/ksh
#aix平台
db_host=`hostname`
db_ip=`ifconfig -a | sed -n "5,5p" | awk ‘{print $2}‘`
who_am=`whoami`

db2 list db directory > ${db_ip}_${who_am}_${db_host}.db
db2 list node directory > ${db_ip}_${who_am}_${db_host}.node

 批处理各台服务器编目:

#!/bin/bash
#bash $0 catadir linux|aix db|node
#脚本运行时有三个参数:
#	1、编目信息文件所在目录,
#	2、平台linux或aix
#	3、所要生成的编目类型,node或db

if [ $# -ne 3 ]; then
  echo "bash $0 catadir linux|aix db|node"
  exit 1
fi

cata_dir="$1"
os_sys="$2"
db_node="$3"
os_linux="linux"
os_aix="aix"
db="db"
node="node"

catalog_node="catalog_node_`date +%F`"
catalog_db="catalog_db_`date +%F`"

for file in $(find ${cata_dir} -name "*.${db_node}" | awk -F‘/‘ ‘{print $2}‘)
  do
    #echo filename:$file
    ip_user="${file%_*}"
    db_host="${ip_user%_*}"
    db_user="${ip_user#*_}"
    #linux node
    if [[ "${os_sys}"x = "${os_linux}"x && "${db_node}"x = "${node}"x ]]; then
      cat ${cata_dir}/${file} | egrep ‘Node name|entry type|Protocol|Hostname|Service name‘ | awk ‘{print $NF}‘ | awk ‘{printf $0","} NR%5==0 {print db_host","db_user}‘ db_host="$db_host" db_user="$db_user" >> ${catalog_node}
    fi
    #aix node
    if [[ "${os_sys}"x = "${os_aix}"x && "${db_node}"x = "${node}"x  ]]; then
      cat ${cata_dir}/${file} | grep -e "节点名" -e "目录条目类型" -e "协议" -e "主机名" -e "服务名称" | awk ‘{print $NF}‘ | awk ‘{printf $0","} NR%5==0 {print db_host","db_user}‘ db_host="$db_host" db_user="$db_user" >> ${catalog_node}
    fi
    #linux db
    if [[ "${os_sys}"x = "${os_linux}"x && "${db_node}"x = "${db}"x  ]]; then
      #间接db
      cat ${cata_dir}/${file} | egrep ‘alias|Database name|entry type|partition‘ | awk ‘{print $NF}‘ | grep -A 1 -B 2 "Indirect" | grep -v "\-\-" | awk ‘{printf $0","} NR%2==0 && NR%4!=0 {printf ","} NR%4==0 {print db_host","db_user}‘ db_host="$db_host" db_user="$db_user" >> ${catalog_db}
      #远程db
      cat ${cata_dir}/${file} | egrep ‘alias|Database name|Node name|entry type|partition‘ | grep -A 1 -B 3 "Remote" | grep -v "\-\-" | awk ‘{print $NF}‘ |awk ‘{printf $0","} NR%5==0 {print db_host","db_user}‘ db_host="$db_host" db_user="$db_user" >> ${catalog_db}
    fi
    #aix db
    if [[ "${os_sys}"x = "${os_aix}"x && "${db_node}"x = "${db}"x  ]]; then
      #间接db 
      cat ${cata_dir}/${file} | egrep ‘数据库别名|数据库名称|目录条目类型|目录数据库分区号‘ | awk ‘{print $NF}‘ | grep -A 1 -B 2 "间接" | grep -v "\-\-" | awk ‘{printf $0","} NR%2==0 && NR%4!=0 {printf ","} NR%4==0 {print db_host","db_user}‘ db_host="$db_host" db_user="$db_user" >> ${catalog_db}
      #远程db
      cat ${cata_dir}/${file} | egrep ‘数据库别名|数据库名称|节点名|目录条目类型|目录数据库分区号‘ | grep -A 1 -B 3 "远程"| grep -v "\-\-" | awk ‘{print $NF}‘ |awk ‘{printf $0","} NR%5==0 {print db_host","db_user}‘ db_host="$db_host" db_user="$db_user" >> ${catalog_db}
    fi
done

exit $?

 

  

以上是关于db2编目抽取的主要内容,如果未能解决你的问题,请参考以下文章

DB2导入导出编目配置

DB2——DB2的字典视图

通过SSL将IBM DB2 JDBC连接到编目数据库

db2联邦数据库

如何在我的本地机器中对“SQL 数据库/Bluemix”进行编目?

db2常用命令行命令