PgSQL dump 工具
#!/bin/bash
#Auther Sun Ying
##Copy left
##Version: Demo Version
##Basic Compare the datebase changes #ER=`whoami`
#if [ $USER != "postgres" ];then
# echo -e "\033[31mWrong User,please use postgres user\033[0m"
# exit 1
#fi
trap "Clear_Temp;exit" SIGINT SIGQUIT SIGHUP
cmd_help(){ echo -e "\033[36mUsage: \033[33mdump2sql [smgr|ipoc] [Options]\033[0m"
echo -e "\033[32m -d|--dump {-b|--before|-f|--after} Generate the dumpfile\033[0m"
echo -e "\033[32m -c|--compare {d|delete} Compare the dump file by Change|Delete Method\033[0m"
exit 5
} #check_dump(){
# l=1
# line2=0
# for i in `sed -n -e "/#Table/=" $table_file`
# do
# if [ $line2 -eq $l ];then
# c[$l]=$i
# break
# fi
# table_name=`sed -n "${i}p" $table_file|awk '{print $2}'`
# c[$l]=$i
# if [ $table_name == "$1" ];then
# line1=$i
# let line2=$l+1
# fi
#
# let l=$l+1
# done
# sed -n "$line1,${c[$line2]}p" $table_file
#} Clear_Temp(){
rm $FullTable &>/dev/null
rm $FullTable1 &>/dev/null
} generator() {
[ -e $TARGET ] && rm -f $TARGET
echo
echo -e "\033[33mGenrating the output files...Please wait...\033[0m"
j=0
for i in `cat $FullTable1`
do echo "#Table $i" >>$TARGET 2>/dev/null
export PGPASSWORD=$PGPASSWORD;psql -h $HOST -U $ACCOUNT $DATABASE -c "select * from $i">>$TARGET 2>/dev/null
echo >>$TARGET
echo >>$TARGET
echo >>$TARGET
printf "*"
done } get_all_tables(){
FullTable=`mktemp /tmp/full_table_XXXXXXX`
FullTable1=`mktemp /tmp/full_table_XXXXXX`
export PGPASSWORD=$PGPASSWORD;psql -h $HOST -U $ACCOUNT $DATABASE -c '\d' > $FullTable
cat $FullTable|awk -F "|" '{print $2}' > $FullTable1
} get_information(){
read -p "Please input your Host:" HOST
read -p "Please input your User Account:" ACCOUNT
read -p "Please input the database your want to get:" DATABASE
read -s -p "Please input your pgsql User password:" PGPASSWORD
# HOST="localhost"
# ACCOUNT="postgres"
# DATABASE="avmgmt"
# PGPASSWORD="postgres"
}
smgr(){
HOST="localhost"
ACCOUNT="postgres"
DATABASE="avmgmt"
PGPASSWORD="postgres"
} compare_change(){
file_before=/tmp/dump_before
file_after=/tmp/dump_after m=1
for i in `sed -n -e "/#Table/=" $file_after`
do
a[$m]=$i
#echo ${a[$m]}
let m=m+1
done echo -e "\033[33m-------------Compare change started, please wait-------------\033[0m"
n=1
for i in $(diff $file_before $file_after -c|egrep "\-\-\-.*\,.*\-\-\-"|awk -F "," '{print $2}'|awk '{print $1}')
do
let i=$i-3
b[$n]=$i
#echo ${b[$n]}
let n=n+1
done for((i=1;i<n;++i))
do
if [[ ${b[$i]} -le ${a[$mid]} && ${b[$i]} -ge ${a[$mid_1]} ]];then
if [ $mark1 -eq "0" ];then
continue
else
sed -n "${b[$i]}p" $file_after|egrep "^\\(.*row.*\\)|^$|#Table.*" &>/dev/null
if [ $? -eq 0 ];then
continue
else
echo -e "\033[33m====Below line also changed in this Table===\033[0m"
echo -e "\033[32m`sed -n "${b[$i]}p" $file_after`\033[0m"
continue
fi
fi
fi
let max=$m-1
let min=1
let mid=$max+$min
let mid=$mid/2
let mid_1=$mid-1
let Num=${b[$i]}
until [[ $Num -le ${a[$mid]} && $Num -ge ${a[$mid_1]} ]];
do
if [ $Num -gt ${a[$mid]} ];then
let min=$mid
let mid=$min+$max
let mid=$mid/2
let mid_1=$mid-1
else
let max=$mid
let mid=$min+$max
let mid=$mid/2
let mid_1=$mid-1
fi
done
table_name=$(sed -n "${a[$mid_1]}p" $file_after)
if [ "$table_name" == "#Table log_store" -o "$table_name" == "#Table sched_completed_job_status_view" -o "$table_name" == "#Table sched_job_status" -o "$table_name" == "#Table sched_jobs" -o "$table_name" == "#Table sched_pending_job_status_view" -o "$table_name" == "#Table sched_qrtz_simple_triggers" -o "$table_name" == "#Table sched_qrtz_triggers" -o "$table_name" == "#Table timer" -o "$table_name" == "#Table sched_qrtz_fired_triggers" -o "$table_name" == "#Table sched_job_parameter" ];then
mark1=0
continue
fi
mark1=1
let lastline=${a[$mid]}-1
let Point_line=$Num
sed -n "${Point_line}p" $file_after|egrep "^\\(.*row.*\\)" &>/dev/null
[ $? -eq 0 ] && let Point_line=$Point_line-1
sed -n "${Point_line}p" $file_after|grep "#Table" &>/dev/null
if [ $? -ne 0 ];then
let Point_line_1=$Point_line-1
let Point_line_2=$Point_line+1
# sed -n "${Point_line}p" $file_after
sed -n "${a[$mid_1]},${Point_line_1}p" $file_after
echo -e "\033[32m`sed -n "${Point_line}p" $file_after`\t\t(Insertd|Modified)\033[0m"
sed -n "${Point_line_2},${lastline}p" $file_after|grep -v "^$"
else
echo -e "\033[33m`sed -n "${a[$mid_1]},${lastline}p" $file_after`\033[0m"
fi
done
}
compare_delete(){
file_before=/tmp/dump_before
file_after=/tmp/dump_after m=1
for i in `sed -n -e "/#Table/=" $file_before`
do
a[$m]=$i
#echo ${a[$m]}
let m=m+1
done echo -e "\033[33m-------------Compare delete started, please wait-------------\033[0m"
n=1
for i in $(diff $file_after $file_before -c|egrep "\-\-\-.*\,.*\-\-\-"|awk -F "," '{print $2}'|awk '{print $1}')
do
let i=$i-3
b[$n]=$i
#echo ${b[$n]}
let n=n+1
done for((i=1;i<n;++i))
do
if [[ ${b[$i]} -le ${a[$mid]} && ${b[$i]} -ge ${a[$mid_1]} ]];then
if [ $mark1 -eq "0" ];then
continue
else
sed -n "${b[$i]}p" $file_after|egrep "^\\(.*row.*\\)|^$|#Table.*" &>/dev/null
if [ $? -eq 0 ];then
continue
else
echo -e "\033[33m====Below line also changed in this Table===\033[0m"
echo -e "\033[32m`sed -n "${b[$i]}p" $file_after`\033[0m"
continue
fi
fi
fi
let max=$m-1
let min=1
let mid=$max+$min
let mid=$mid/2
let mid_1=$mid-1
let Num=${b[$i]}
until [[ $Num -le ${a[$mid]} && $Num -ge ${a[$mid_1]} ]];
do
if [ $Num -gt ${a[$mid]} ];then
let min=$mid
let mid=$min+$max
let mid=$mid/2
let mid_1=$mid-1
else
let max=$mid
let mid=$min+$max
let mid=$mid/2
let mid_1=$mid-1
fi
done
table_name=$(sed -n "${a[$mid_1]}p" $file_before)
if [ "$table_name" == "#Table log_store" -o "$table_name" == "#Table sched_completed_job_status_view" -o "$table_name" == "#Table sched_job_status" -o "$table_name" == "#Table sched_jobs" -o "$table_name" == "#Table sched_pending_job_status_view" -o "$table_name" == "#Table sched_qrtz_simple_triggers" -o "$table_name" == "#Table sched_qrtz_triggers" -o "$table_name" == "#Table timer" -o "$table_name" == "#Table sched_qrtz_fired_triggers" -o "$table_name" == "#Table sched_job_parameter" ];then
mark1=0
continue
fi
mark1=1
let lastline=${a[$mid]}-1
let Point_line=$Num # sed -n "${Point_line}p" $file_after|grep "#Table" &>/dev/null
# if [ $? -ne 0 ];then
sed -n "${Point_line}p" $file_before|egrep "^\\(.*row.*\\)" &>/dev/null
[ $? -eq 0 ] && let Point_line=$Point_line-1
let Point_line_1=$Point_line-1
let Point_line_2=$Point_line+1
# sed -n "${Point_line}p" $file_after
sed -n "${a[$mid_1]},${Point_line_1}p" $file_before
echo -e "\033[31m`sed -n "${Point_line}p" $file_before`\t\t(Deleted|Modified)\033[0m"
sed -n "${Point_line_2},${lastline}p" $file_before|grep -v "^$"
# else
# echo -e "\033[33m`sed -n "${a[$mid_1]},${lastline}p" $file_after`\033[0m"
# fi
done
} #####main()#####
if [ $# -ne 0 ];then
if [ $1 == "SMGR" -o $1 == "smgr" ];then
smgr
shift
fi
if [ $1 == "IPOC" -o $1 == "ipoc" ];then
ipoc
shift
fi if [ $1 == "-c" -o $1 == "--compare" ];then
if [ $# -lt 2 ];then
compare_change
exit 0
elif [ $2 == "d" -o $2 == "delete" ];then
compare_delete
exit 0
else
cmd_help
fi
fi if [ $1 == "-d" -o $1 == "--dump" ];then if [ $# -ge 2 ];then
if [ $2 == "b" -o $2 == "before" ];then
TARGET="/tmp/dump_before"
elif [ $2 == "f" -o $2 == "after" ];then
TARGET="/tmp/dump_after"
else
cmd_help
fi
elif [ ! -e /tmp/dump_before ];then
TARGET="/tmp/dump_before"
elif [[ -e /tmp/dump_before && ! -e /tmp/dump_after ]];then
TARGET="/tmp/dump_after"
elif [[ -e /tmp/dump_before && -e /tmp/dump_after ]];then
mv -f /tmp/dump_after /tmp/dump_before
TARGET="/tmp/dump_after"
else
TARGET="/tmp/dump_before"
fi
[ ! $HOST ] && get_information
get_all_tables
generator
echo
echo -e "You can find your output file under \033[33m$TARGET\033[0m"
exit 0
fi
# if [ $1 == "-k" -o $1 == "--check" ];then
# if [ $# -lt 2 ];then
# cmd_help
# fi
# if [ $# -ge 3 ];then
# if [ $2 == "b" -o $2 == "before" ];then
# table_file=/tmp/dump_before
# shift
# elif [ $2 == "f" -o $2 == "after" ];then
# table_file=/tmp/dump_after
# shift
# else
# cmd_help
# fi
# fi
# shift
# table_file=/tmp/dump_after
# check_dump $1
# fi
Clear_Temp
cmd_help
else
cmd_help
fi
PgSQL dump 工具的更多相关文章
- 【原】tinker dex文件格式的dump工具tinker-dex-dump
序言 Tinker是微信推出的热更新开源项目,同其它热更新方案相比具有补丁包小,支持类,so,资源文件的替换等优点.其中在类替换的方案里自主研发了DexDiff算法,使得补丁包变的更小.DexDiff ...
- 分析JVM GC Dump 工具
GC 日志分析工具: http://gceasy.io/ JVM Dump 文件分析工具: IBM HeapAnalyzer
- qt 工具下的dump工具导出文档出现异常解决方案
今天一直认为qt环境下的dumpcpp 和dumpdoc两个导出工具很好用,可以今天在导出MSChart组件的类方法文档时,虽然导出成功了,但是导出的结果却是令人失望.自己也不知道如何能够正确导出,就 ...
- 分析 Java heap dump工具之IBM HeapAnalyzer
IBM HeapAnalyzer是一款免费的JVM内存堆的图形分析工具,它可以有效的列举堆的内存使用状况,帮助分析Java内存泄漏的原因. 功能与MAT类似. 1.下载 https://www.ibm ...
- 类似于Mimikatz的Linux Hash Dump工具
项目主页 https://github.com/huntergregal/mimipenguin 需要root权限 支持 Kali 4.3.0 (rolling) x64 (gdm3) Ubuntu ...
- .net工具
程序名称 作者 说明 文件结构与元数据查看看 AssemblyView1.0 可以查看.net平台下exe,dll源代码的类结构,比如变量,属性,函数,事件的定义. Anakrino 源代码开 ...
- windows下捕获dump
一般要捕获异常只需要两个函数:SetUnhandledExceptionFilter截获异常:MiniDumpWriteDump写dump文件.但是由于CRT函数可能会在内部调用SetUnh ...
- MongoDB工具介绍
在Windows下面,mongodb就只有一个bin目录以及bin目录以外的三个文件,相对bin目录中包括了如下文件: bsondump.exe 用于将导出的BSON文件格式转换为JSON格式 mon ...
- 小白日记20:kali渗透测试之后渗透测试阶段(一)--上传工具
后渗透测试阶段--上传工具 为防止管理员将漏洞补上后,我们无法再通过该漏洞控制对方主机,所以需要进行后渗透测试阶段 1.上传各种工具 2.提权:为了全面控制目标系统 3.擦除攻击痕迹:防止管理员通过日 ...
随机推荐
- javascript中call函数与apply
javascript中的call方法使当前对象可以调用另一个对象的方法,即改变this的指向内容 var first_object = { num: 42 }; var second_object = ...
- 如何将自己的windows设置为mysql服务器
1.安装mysql 服务器 2.创建超级用户,即 用户管理 mysql>use mysql; 查看 mysql> select host,user,password from user ; ...
- 《Matrix Computation 3rd》读书笔记——第3章 一般线性系统
- Web前端开发学习心得(一)
说快也快,说慢也慢.自学前端一个月了,进步不算大,不过仔细想来,倒也收获不少.码几行字,一是总结经验,二是继往开来. 刚开始是跟慕课网学的HTML+CSS,慕课网的课程虽然好,但好多知识都是特别基础的 ...
- Python数字,字符串
数字 支持整数,浮点数,和奇怪的类型,如复数. 特殊的运算符为**,表示次方操作,如2**100,表示2的100次方. len()可以得到一个字符串对象的长度,str()可以将数字转换为字符串. pr ...
- C#防止反编译
http://blog.csdn.net/wangpei421/article/details/42393095 http://www.cnblogs.com/tianguook/archive/20 ...
- Python Iterable Iterator Yield
可以直接作用于for循环的数据类型有以下几种: 一类是集合数据类型,如list / tuple / dict / set / str /等(对于这类iterable的对象,因为你可以按照你的意愿进行重 ...
- Linux下使用ping快速检测存活主机
该shell脚本主要是通过ping来批量检测网段内存活主机,执行结果保存在脚本当前目录下的IPinfor目录中,写的比较匆忙,希望大家留下更好的建议. #!/usr/bin/env bash##### ...
- JAVA访问权限
同一个类 同一个包 不同包的子类 不同包的非子类 Private √ Default √ √ Protected √ √ √ Public √ √ √ √
- SonarQube的使用入门
SonarQube的安装.配置与使用 详情请参照原博客:http://www.cnblogs.com/qiaoyeye/p/5249786.html SonarQube是管理代码质量一个开放平台,可以 ...