首页 > 其他分享 >【PG】对比两个数据中对象

【PG】对比两个数据中对象

时间:2024-03-14 21:37:10浏览次数:18  
标签:temp 对象 DB echo host PG file inpDB1 对比


#!/bin/bash

##################################################################################################################################
#
# Name: Compare 2 Postgres DBs
#
# Description: Compare 2 Postgres DBs Tables, Indexes, and Functions. In case no matching, print out the details.
#
# Author: Dmitry
#
# Date Created: 26-Dec-2020
#
# Usage Example: 
#
#     ./compare_2_dbs_postgres.sh -h localhost -p 5432 -d some_db_1 -l user_db_1 -g localhost -q 5433 -e some_db_2 -m user_db_2 -w pwdSrc -z pwdDest
#     ./compare_2_dbs_postgres.sh -h localhost -p 5432 -d some_db_1 -l user_db_1 -g localhost -q 5433 -e some_db_2 -m user_db_2 -w pwdSrc -z pwdDest -v
#
####################################################################################################################################

helpFunction()
{
   echo ""
   echo "Usage: $0 -h hostname1 -p port1 -d dbname1 -l dbuser1 -g hostname2 -q port2 -e dbname2 -m dbuser2 -w srcPwd -z destPwd"
   echo -e "\t-h Postgres hostname1"
   echo -e "\t-p Postgers port1"
   echo -e "\t-d Postgres db1 to compare"
   echo -e "\t-l Postgres dbuser1"
   echo -e "\t-g Postgres hostname2"
   echo -e "\t-q Postgers port2"
   echo -e "\t-e Postgres db2 to compare"
   echo -e "\t-m Postgres dbuser2"
   echo -e "\t-w Postgres SrcDB pwd"
   echo -e "\t-z Postgres DestDB pwd"
   echo -e "\t-v Verbose"
   exit 1 # Exit script after printing help
}
echo " --- start of compare 2 DBs script ---"
echo " "
inpVerbose=0
while getopts "h:p:d:l:g:q:e:m:w:z:v" opt
do
   case "$opt" in
      h ) inpHost1="$OPTARG" ;;
      p ) inpPort1="$OPTARG" ;;
      d ) inpDB1="$OPTARG" ;;
      l ) inpUser1="$OPTARG" ;;
      g ) inpHost2="$OPTARG" ;;
      q ) inpPort2="$OPTARG" ;;
      e ) inpDB2="$OPTARG" ;;
      m ) inpUser2="$OPTARG" ;;
      w ) inpSrcPwd="$OPTARG" ;;
      z ) inpDestPwd="$OPTARG" ;;
      v ) inpVerbose=1 ;;
      ? ) helpFunction ;; # Print helpFunction in case parameter is non-existent
   esac
done
# Print helpFunction in case parameters are empty
if [ -z "$inpHost1" ] || [ -z "$inpPort1" ] || [ -z "$inpDB1" ] || [ -z "$inpHost2" ] || [ -z "$inpPort2" ] || [ -z "$inpDB2" ] || [ -z "$inpSrcPwd" ] || [ -z "$inpDestPwd" ] || [ -z "$inpUser1" ] || [ -z "$inpUser2" ]
then
   echo "Some or all of the parameters are empty";
   helpFunction
fi
# Begin script in case all parameters are correct
echo " "
echo "Compare 2 Postgres DBs"
echo " "
echo "DB1"
echo "inpHost1=$inpHost1"
echo "inpPort1=$inpPort1"
echo "inpDB1=$inpDB1"
echo "inpUser1=$inpUser1"
echo " "
echo "DB2"
echo "inpHost2=$inpHost2"
echo "inpPort2=$inpPort2"
echo "inpDB2=$inpDB2"
echo "inpUser2=$inpUser2"
echo " "
echo "inpSrcPwd=*************"
echo "inpDestPwd=*************"
echo " "
#
# Compare Tables
#
echo "Compare Tables"
export PGPASSWORD="${inpSrcPwd}"
data_set_1=$(psql -h $inpHost1 -p $inpPort1 -U $inpUser1 -d $inpDB1 -t << EOF
select 
       n.nspname as table_schema,
       c.relname as table_name
 from pg_class c
 join pg_namespace n on n.oid = c.relnamespace
 where c.relkind = 'r'
       and n.nspname not in ('information_schema','pg_catalog')
 order by 2;
EOF
)
export PGPASSWORD="${inpDestPwd}"
data_set_2=$(psql -h $inpHost2 -p $inpPort2 -U $inpUser2 -d $inpDB2 -t << EOF
 select 
        n.nspname as table_schema,
        c.relname as table_name
 from pg_class c
 join pg_namespace n on n.oid = c.relnamespace
 where c.relkind = 'r'
       and n.nspname not in ('information_schema','pg_catalog')
 order by 2;
EOF
)
temp_file_1="/tmp/tmp_tables_db1.tmp"
temp_file_2="/tmp/tmp_tables_db2.tmp"
echo "$data_set_1" > ${temp_file_1}
echo "$data_set_2" > ${temp_file_2}
if [ $inpVerbose -eq 1 ]
then
  echo " "
  echo "Tables in the DB ${inpDB1} on host ${inpHost1}"
  cat ${temp_file_1}
  echo " "
  echo "Tables in the DB ${inpDB2} on host ${inpHost2}"
  cat ${temp_file_2}
  echo " "
fi
echo "Not matching tables:"
echo " "
echo "Exists in the DB ${inpDB1} on host ${inpHost1} and DOES NOT exist in the DB ${inpDB2} on host ${inpHost2}"
grep -vf ${temp_file_2} ${temp_file_1}
chk=`grep -vf ${temp_file_2} ${temp_file_1}`
if [ ${#chk} -ne 0 ]
then
    echo "Error! Not matching tables! Exists in the DB ${inpDB1} on host ${inpHost1} and DOES NOT exist in the DB ${inpDB2} on host ${inpHost2}"
    exit 1
fi
echo " "
echo "Exists in the DB ${inpDB2} on host ${inpHost2} and DOES NOT exist in the DB ${inpDB1} on host ${inpHost1}"
grep -vf ${temp_file_1} ${temp_file_2}
chk=`grep -vf ${temp_file_1} ${temp_file_2}`
if [ ${#chk} -ne 0 ]
then
    echo "Error! Not matching tables! Exists in the DB ${inpDB2} on host ${inpHost2} and DOES NOT exist in the DB ${inpDB1} on host ${inpHost1}"
    exit 1
fi
echo " "
#
# Compare Tables Columns, Types, Defaults
#
echo "Compare Tables Columns, Types, Defaults"
export PGPASSWORD="${inpSrcPwd}"
data_set_11=$(psql -h $inpHost1 -p $inpPort1 -U $inpUser1 -d $inpDB1 -qAtX -F ' ' << EOF
SELECT table_name, column_name, data_type, is_nullable, character_maximum_length, column_default
FROM information_schema.columns
WHERE table_schema = 'public' and table_name != 'pg_stat_statements'
ORDER BY table_name, ordinal_position;
EOF
)
export PGPASSWORD="${inpDestPwd}"
data_set_22=$(psql -h $inpHost2 -p $inpPort2 -U $inpUser2 -d $inpDB2 -qAtX -F ' ' << EOF
SELECT table_name, column_name, data_type, is_nullable, character_maximum_length, column_default
FROM information_schema.columns
WHERE table_schema = 'public' and table_name != 'pg_stat_statements'
ORDER BY table_name, ordinal_position;
EOF
)
temp_file_11="/tmp/tmp_tables_db11.tmp"
temp_file_22="/tmp/tmp_tables_db22.tmp"
echo "$data_set_11" > ${temp_file_11}
echo "$data_set_22" > ${temp_file_22}
if [ $inpVerbose -eq 1 ]
then
  echo " "
  echo "Tables Columns, Types, Defaults in the DB ${inpDB1} on host ${inpHost1}"
  cat ${temp_file_11}
  echo " "
  echo "Tables Columns, Types, Defaults in the DB ${inpDB2} on host ${inpHost2}"
  cat ${temp_file_22}
  echo " "
fi
echo "Not matching tables column_names, data_types, defaults"
echo " "
echo "Exists column, type, default in the DB ${inpDB1} on host ${inpHost1} and DOES NOT exist in the DB ${inpDB2} on host ${inpHost2}"
grep -vf ${temp_file_22} ${temp_file_11}
chk=`grep -vf ${temp_file_22} ${temp_file_11}`
if [ ${#chk} -ne 0 ]
then
    echo "Error! Not matching tables columns, types, defaults! Exists in the DB ${inpDB1} on host ${inpHost1} and DOES NOT exist in the DB ${inpDB2} on host ${inpHost2}"
    exit 1
fi
echo " "
echo "Exists column, type, default in the DB ${inpDB2} on host ${inpHost2} and DOES NOT exist in the DB ${inpDB1} on host ${inpHost1}"
grep -vf ${temp_file_11} ${temp_file_22}
chk=`grep -vf ${temp_file_11} ${temp_file_22}`
if [ ${#chk} -ne 0 ]
then
    echo "Error! Not matching tables columns, types, defaults! Exists in the DB ${inpDB2} on host ${inpHost2} and DOES NOT exist in the DB ${inpDB1} on host ${inpHost1}"
    exit 1
fi
echo " "
#
# Compare Indexes
#
echo "Compare Indexes"
export PGPASSWORD="${inpSrcPwd}"
data_set_3=$(psql -h $inpHost1 -p $inpPort1 -U $inpUser1 -d $inpDB1 -t << EOF
SELECT
     n.nspname  as "schema"
    ,t.relname  as "table"
    ,c.relname  as "index"
    ,pg_get_indexdef(indexrelid) as "def"
FROM pg_catalog.pg_class c
    JOIN pg_catalog.pg_namespace n ON n.oid        = c.relnamespace
    JOIN pg_catalog.pg_index i ON i.indexrelid = c.oid
    JOIN pg_catalog.pg_class t ON i.indrelid   = t.oid
WHERE c.relkind = 'i'
    and n.nspname not in ('pg_catalog', 'pg_toast')
    and pg_catalog.pg_table_is_visible(c.oid)
ORDER BY
     n.nspname
    ,t.relname
    ,c.relname
EOF
)
export PGPASSWORD="${inpDestPwd}"
data_set_4=$(psql -h $inpHost2 -p $inpPort2 -U $inpUser2 -d $inpDB2 -t << EOF
SELECT
     n.nspname  as "schema"
    ,t.relname  as "table"
    ,c.relname  as "index"
    ,pg_get_indexdef(indexrelid) as "def"
FROM pg_catalog.pg_class c
    JOIN pg_catalog.pg_namespace n ON n.oid        = c.relnamespace
    JOIN pg_catalog.pg_index i ON i.indexrelid = c.oid
    JOIN pg_catalog.pg_class t ON i.indrelid   = t.oid
WHERE c.relkind = 'i'
    and n.nspname not in ('pg_catalog', 'pg_toast')
    and pg_catalog.pg_table_is_visible(c.oid)
ORDER BY
     n.nspname
    ,t.relname
    ,c.relname
EOF
)
temp_file_3="/tmp/tmp_indexes_db1.tmp"
temp_file_4="/tmp/tmp_indexes_db2.tmp"
echo "$data_set_3" > ${temp_file_3}
echo "$data_set_4" > ${temp_file_4}
if [ $inpVerbose -eq 1 ]
then
  echo " "
  echo "Indexes in the DB ${inpDB1} on host ${inpHost1}"
  cat ${temp_file_3}
  echo " "
  echo "Indexes in the DB ${inpDB2} on host ${inpHost2}"
  cat ${temp_file_4}
  echo " "
fi
echo "Not matching indexes:"
echo " "
echo "Exists in the DB ${inpDB1} on host ${inpHost1} and DOES NOT exist in the DB ${inpDB2} on host ${inpHost2}"
grep -vf ${temp_file_4} ${temp_file_3}
chk=`grep -vf ${temp_file_4} ${temp_file_3}`
if [ ${#chk} -ne 0 ] 
then
    echo "Error! Not matching indexes! Exists in the DB ${inpDB1} on host ${inpHost1} and DOES NOT exist in the DB ${inpDB2} on host ${inpHost2}"
    exit 1
fi
echo " "
echo "Exists in the DB ${inpDB2} on host ${inpHost2} and DOES NOT exist in the DB ${inpDB1} on host ${inpHost1}"
grep -vf ${temp_file_3} ${temp_file_4}
chk=`grep -vf ${temp_file_3} ${temp_file_4}`
if [ ${#chk} -ne 0 ] 
then
    echo "Error! Not matching indexes! Exists in the DB ${inpDB2} on host ${inpHost2} and DOES NOT exist in the DB ${inpDB1} on host ${inpHost1}"
    exit 1
fi
#
# Compare Functions
#
echo "Compare Functions"
export PGPASSWORD="${inpSrcPwd}"
data_set_111=$(psql -h $inpHost1 -p $inpPort1 -U $inpUser1 -d $inpDB1 -t << EOF
select n.nspname as function_schema,
       p.proname as function_name,
       l.lanname as function_language,
       case when l.lanname = 'internal' then p.prosrc
            else pg_get_functiondef(p.oid)
            end as definition,
       pg_get_function_arguments(p.oid) as function_arguments,
       t.typname as return_type
from pg_proc p
left join pg_namespace n on p.pronamespace = n.oid
left join pg_language l on p.prolang = l.oid
left join pg_type t on t.oid = p.prorettype
where n.nspname not in ('pg_catalog', 'information_schema') and p.proname not like '%pg_stat_statements%'
order by function_schema,
         function_name;
EOF
)
export PGPASSWORD="${inpDestPwd}"
data_set_222=$(psql -h $inpHost2 -p $inpPort2 -U $inpUser2 -d $inpDB2 -t << EOF
select n.nspname as function_schema,
       p.proname as function_name,
       l.lanname as function_language,
       case when l.lanname = 'internal' then p.prosrc
            else pg_get_functiondef(p.oid)
            end as definition,
       pg_get_function_arguments(p.oid) as function_arguments,
       t.typname as return_type
from pg_proc p
left join pg_namespace n on p.pronamespace = n.oid
left join pg_language l on p.prolang = l.oid
left join pg_type t on t.oid = p.prorettype
where n.nspname not in ('pg_catalog', 'information_schema') and p.proname not like '%pg_stat_statements%'
order by function_schema,
         function_name;
EOF
)
temp_file_111="/tmp/tmp_tables_func1.tmp"
temp_file_222="/tmp/tmp_tables_func2.tmp"
echo "$data_set_111" > ${temp_file_111}
echo "$data_set_222" > ${temp_file_222}
if [ $inpVerbose -eq 1 ]
then
  echo " "
  echo "Functions in the DB ${inpDB1} on host ${inpHost1}"
  cat ${temp_file_111}
  echo " "
  echo "Functions in the DB ${inpDB2} on host ${inpHost2}"
  cat ${temp_file_222}
  echo " "
fi
echo "Not matching functions:"
echo " "
echo "Exists in the DB ${inpDB1} on host ${inpHost1} and DOES NOT exist in the DB ${inpDB2} on host ${inpHost2}"
diff ${temp_file_222} ${temp_file_111}
chk=`diff ${temp_file_222} ${temp_file_111}`
if [ ${#chk} -ne 0 ]
then
    echo "Error! Not matching functions! Exists in the DB ${inpDB1} on host ${inpHost1} and DOES NOT exist in the DB ${inpDB2} on host ${inpHost2}"
    exit 1
fi
echo " "
echo "Exists in the DB ${inpDB2} on host ${inpHost2} and DOES NOT exist in the DB ${inpDB1} on host ${inpHost1}"
diff ${temp_file_111} ${temp_file_222}
chk=`diff ${temp_file_111} ${temp_file_222}`
if [ ${#chk} -ne 0 ]
then
    echo "Error! Not matching functions! Exists in the DB ${inpDB2} on host ${inpHost2} and DOES NOT exist in the DB ${inpDB1} on host ${inpHost1}"
    exit 1
fi
echo " "
echo " --- end of compare 2 DBs script ---"
echo " "

标签:temp,对象,DB,echo,host,PG,file,inpDB1,对比
From: https://www.cnblogs.com/Jeona/p/18074017

相关文章

  • 【PG】不同PG中迁移表
    #!/bin/bash#SourcedatabasecredentialsSRC_DB_HOST="localhost"SRC_DB_PORT="5442"SRC_DB_NAME="postgres"SRC_DB_USER="myuser"SRC_DB_PASS='mypwd'#DestinationdatabasecredentialsDEST_DB_HOST="l......
  • 【PG】列出表以及大小
    ThispostdemonstratesanexampleofaBashscriptthatconnectstoaDBandprintsalistoftables,theirrecordsnumber,andsize.Theresultsetissortedindescendingorderbysizeandthenbynumberofrecords.Inaddition,theoutputofthescript......
  • 【PG】查询数据大小
    #!/bin/bash##################################################################calculate_a_pure_size_of_the_postgres_db.sh##ThisscriptcalculatesapuresizeofthePostgresDB##DB_Pure_Size=DB_Size-DB_Tables_Bloat-DB_Indexes_Bloat##Aut......
  • 【PG】查看PG对象大小增长情况
    #!/bin/bash####################################################check_postgresql_db_table_rowsnum_and_sizing.sh##Thisscriptsiteratesartidatabasesand#populatessometargetdbtablewith#infoabouttablesrownumandsizing##Date:10-Aug-......
  • Go语言中的面向对象编程(OOP)
    在Go语言中,虽然没有像面向对象语言那样的类,但通过结构体类型和方法,仍然支持部分面向对象编程(OOP)的概念。封装(Encapsulation)封装是一种将一个对象的实现细节隐藏起来,使其对其他对象不可见的做法,这样可以实现解耦。例如,考虑以下结构体:typeStudentstruct{namestring......
  • 面向对象设计的六大原则(SOLID原则)-——里氏替换原则
    里氏替换原则(LiskovSubstitutionPrinciple,LSP)是面向对象设计的基本原则之一,由BarbaraLiskov提出。它表明,如果程序中的对象使用的是基类型的话,那么无论它实际上使用的是哪一个子类的对象,程序的行为都不会发生改变。简单来说,子类型必须能够替换它们的基类型,而且替换后程序的行......
  • PG14:auth_delay 插件源码分析
    auth_delay让服务器在报告身份验证失败前短暂暂停,以增加对数据库密码进行暴力破解的难度。需要注意的是,这对阻止拒绝服务攻击毫无帮助,甚至可能加剧攻击,因为在报告身份验证失败前等待的进程仍会占用连接。要使用这个模块必须要在postgresql.conf中配置参数shared_preload_libr......
  • 【你也能从零基础学会网站开发】Web建站之javascript入门篇 History对象与Location对
    ......
  • JAVA学习日记五(面向对象-高级)
    1.关键字:static如果想让一个成员变量被类的所有实例所共享,就用static修饰即可,称为类变量(或类属性)!使用范围:在Java类中,可用static修饰属性、方法、代码块、内部类被修饰后的成员具备以下特点:随着类的加载而加载优先于对象存在修饰的成员,被所有对象所共享访问权限......
  • JAVA学习日记四(面向对象-基础)
    1.面向对象概述面向对象可以帮助我们从宏观上把握、从整体上分析整个系统。我们千万不要把面向过程和面向对象对立起来。他们是相辅相成的。面向对象离不开面向过程!2.Java的基本元素:类和对象类:具有相同特征的事物的抽象描述,是抽象的、概念上的定义。对象:实际存在的该类事......