工做中時常給開發和測試導入導出一些測試的數據庫,因而寫了一個腳本方便操做。sql
公司目前使用的是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