自用Postgres 数据库的导入导出脚本
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了自用Postgres 数据库的导入导出脚本相关的知识,希望对你有一定的参考价值。
工作中时常给开发和测试导入导出一些测试的数据库,于是写了一个脚本方便操作。
公司目前使用的是postgres9.3数据库。
#!/bin/bash #定义一些变量和操作命令 DBS="db1 db2 db3 db4" EXPDB_CMD=/usr/pgsql-9.3/bin/pg_dump DBCMD=/usr/pgsql-9.3/bin/psql DBUSER=postgres DBSVR=mydbhost1 #导出数据库 function exportdb() { read -p "Input database user name to export: " SRCDB read -p "Input export path: " EXPATH for db in `echo $DBS` do $EXPDB_CMD -U $DBUSER -h $DBSVR ${db}_${SRCDB} > $EXPATH/${db}_${SRCDB} done } #导入数据库 function importdb() { read -p "Input source database user name: " SRCDB read -p "Input destination database user name: " DSTDB read -p "Input source database path: " EXPATH read -p "Confirm the source DB: $SRCDB / destination DB: $DSTDB? [Y/N]" YorN if [ $YorN == "Y" ] || [ $YorN == "y" ]; then for db in `echo $DBS` do echo ${db}_$DSTDB $DBCMD -U $DBUSER -h $DBSVR -d ${db}_${DSTDB} < $EXPATH/${db}_${SRCDB} done else echo "Decline or Input incorrect! Re-type again" for i in `seq 3`; do sleep 1; echo -n ".";done scrn fi } #创建数据库用户 function createrole() { read -p "Input new database username: " NEWROLE $DBCMD -U $DBUSER -h $DBSVR -c "create role $NEWROLE;" $DBCMD -U $DBUSER -h $DBSVR -c "alter role $NEWROLE login;" } #创建新的数据库 function createdb() { read -p "Input new database username: " NEWDB for db in $(echo $DBS) do $DBCMD -U $DBUSER -h $DBSVR -c "create database ${db}_$NEWDB owner $NEWDB;" done } #删除不用的数据库 function dropdb() { read -p "Input database username you want to drop: " DRPDB read -p "Confirmed the database username $DRPDB you want to drop [Y/N]: " YorN if [ $YorN == "Y" ] || [ $YorN == "y" ]; then for db in $(echo $DBS) do $DBCMD -U $DBUSER -h $DBSVR -c "drop database ${db}_$DRPDB;" done else echo "Decline or Input incorrect! Re-type again" for i in `seq 3`; do sleep 1; echo -n ".";done scrn fi } #搜索数据库 function searchdb() { read -p "Input database username you want to search: " SERDB $DBCMD -U $DBUSER -h $DBSVR -c "\l" | grep $SERDB [ $? -ne 0 ] && echo "No results" } #查询数据库下的表 function shtbl() { read -p "Input full database name you want to search: " SERDB $DBCMD -U $DBUSER -h $DBSVR -d $SERDB -c "\dt" [ $? -ne 0 ] && echo "No results" } #导入其他用户后需要修改表的所有者 function chgowner() { read -p "Input database username you want to change owner: " NEWDB for db in $(echo $DBS) do for tbl in $($DBCMD -U $DBUSER -h $DBSVR -qAt -c "select tablename from pg_tables where schemaname = ‘public‘;" ${db}_$NEWDB) do $DBCMD -U $DBUSER -h $DBSVR -c "alter table $tbl owner to $NEWDB;" ${db}_$NEWDB done done } #显示菜单 function scrn() { clear echo "#########################" echo " Welcome to DB operation" echo "#########################" echo "1.Export Database" echo "2.Create Role" echo "3.Create Database" echo "4.Import Database" echo "5.Drop Database" echo "6.Search Database" echo "7.Change Database owner" echo "8.List tables of Database" echo "9.Exit" echo "0.Display Menu" } #开始执行菜单和选择要执行的操作 scrn while true do read -p "Input your choice: " cho case "$cho" in "1") exportdb ;; "2") createrole ;; "3") createdb ;; "4") importdb ;; "5") dropdb ;; "6") searchdb ;; "7") chgowner ;; "8") shtbl ;; "9") exit 0 ;; "0") scrn ;; *) #echo "Usage: Input your choice: {export|import|createrole|createdb|drop|changeowner|search}" echo "Usage: Press ‘0‘ to display menu or Press ‘9‘ to exit" esac [[ $cho == "9" ]] && exit 0 || continue done
本文出自 “努力为之” 博客,转载请与作者联系!
以上是关于自用Postgres 数据库的导入导出脚本的主要内容,如果未能解决你的问题,请参考以下文章